opencart-manager
Version:
Node.js package helping to manage your opencart store data easily.
98 lines (97 loc) • 6.41 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const database_1 = __importDefault(require("../__mocks__/database"));
const fake_mysql_driver_1 = require("../__mocks__/fake-mysql-driver");
const errors_1 = __importDefault(require("../errors"));
const model_1 = __importDefault(require("../model"));
const attribute_1 = __importDefault(require("./attribute"));
let entity;
const entityId = 12;
const attributeGroupId = 123;
const entityData = { sortOrder: 0 };
const entityDescriptionRus = { languageId: 1, name: "RuName" };
const entityDescriptionEng = { languageId: 2, name: "EngName" };
const entityDataUpdated = { sortOrder: 1 };
const entityDescriptionRusUpdated = Object.assign(Object.assign({}, entityDescriptionRus), { name: "RuNameNew" });
const entityDescriptionEngUpdated = Object.assign(Object.assign({}, entityDescriptionEng), { name: "EngNameNew" });
const headers = Object.assign(Object.assign({}, fake_mysql_driver_1.fakeResultSetHeader), { insertId: entityId });
const db = new database_1.default();
const model = new model_1.default(db);
model.attribute.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.attribute.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.attribute.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.attribute.description.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.attribute.description.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.attribute.description.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
test("Instantiate", () => {
entity = new attribute_1.default(model, attributeGroupId)
.setData(entityData)
.setDescription(entityDescriptionRus)
.setDescription(entityDescriptionEng);
expect(entity.id).toBeUndefined();
expect(entity.data).toStrictEqual(entityData);
expect(entity.description[1]).toStrictEqual(entityDescriptionRus);
expect(entity.description[2]).toStrictEqual(entityDescriptionEng);
expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.update(); })).rejects.toThrow(errors_1.default.ENTITY_NOT_INSERTED);
expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.delete(); })).rejects.toThrow(errors_1.default.ENTITY_NOT_INSERTED);
});
test("Insert", () => __awaiter(void 0, void 0, void 0, function* () {
yield entity.insert();
expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.insert(); })).rejects.toThrow(errors_1.default.ENTITY_INSERTED);
expect(entity.id).toBe(headers.insertId);
expect(entity.data).toStrictEqual(entityData);
expect(entity.description[1]).toStrictEqual(entityDescriptionRus);
expect(entity.description[2]).toStrictEqual(entityDescriptionEng);
expect(model.attribute.insert).toHaveBeenNthCalledWith(1, Object.assign(Object.assign({}, entityData), { attributeGroupId }));
expect(model.attribute.description.insert).toHaveBeenNthCalledWith(1, Object.assign(Object.assign({}, entityDescriptionRus), { attributeId: entityId }));
expect(model.attribute.description.insert).toHaveBeenNthCalledWith(2, Object.assign(Object.assign({}, entityDescriptionEng), { attributeId: entityId }));
}));
test("Update", () => __awaiter(void 0, void 0, void 0, function* () {
entity
.setData(entityDataUpdated)
.setDescription(entityDescriptionRusUpdated)
.setDescription(entityDescriptionEngUpdated);
yield entity.update();
expect(entity.id).toBe(headers.insertId);
expect(entity.data).toStrictEqual(entityDataUpdated);
expect(entity.description[1]).toStrictEqual(entityDescriptionRusUpdated);
expect(entity.description[2]).toStrictEqual(entityDescriptionEngUpdated);
expect(model.attribute.update).toHaveBeenNthCalledWith(1, {
attributeId: entityId,
}, Object.assign(Object.assign({}, entityDataUpdated), { attributeGroupId, attributeId: entityId }));
expect(model.attribute.description.update).toHaveBeenNthCalledWith(1, {
attributeId: entityId,
languageId: 1,
}, Object.assign(Object.assign({}, entityDescriptionRusUpdated), { attributeId: entityId }));
expect(model.attribute.description.update).toHaveBeenNthCalledWith(2, {
attributeId: entityId,
languageId: 2,
}, Object.assign(Object.assign({}, entityDescriptionEngUpdated), { attributeId: entityId }));
}));
test("Delete", () => __awaiter(void 0, void 0, void 0, function* () {
yield entity.delete();
expect(model.attribute.delete).toHaveBeenNthCalledWith(1, {
attributeId: entityId,
});
expect(model.attribute.description.delete).toHaveBeenNthCalledWith(1, {
attributeId: entityId,
});
expect(entity.id).toBeUndefined();
expect(entity.data).toStrictEqual(entityDataUpdated);
expect(entity.description[1]).toStrictEqual(entityDescriptionRusUpdated);
expect(entity.description[2]).toStrictEqual(entityDescriptionEngUpdated);
expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.update(); })).rejects.toThrow(errors_1.default.ENTITY_NOT_INSERTED);
expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.delete(); })).rejects.toThrow(errors_1.default.ENTITY_NOT_INSERTED);
}));