UNPKG

opencart-manager

Version:

Node.js package helping to manage your opencart store data easily.

108 lines (107 loc) 7.21 kB
"use strict"; 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 category_1 = __importDefault(require("./category")); let entity; const entityId = 12; const entityData = { image: "test/image.png" }; const entityDescriptionRus = { languageId: 1, name: "RuName" }; const entityDescriptionEng = { languageId: 2, name: "EngName" }; const entityPath = { categoryId: entityId, pathId: entityId, level: 0 }; const entityDataUpdated = Object.assign(Object.assign({}, entityData), { image: "test/image2.png" }); 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.category.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.description.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.description.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.description.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.toLayout = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.toStore = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.category.path.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); test("Instantiate", () => { entity = new category_1.default(model) .setData(entityData) .setDescription(entityDescriptionRus) .setDescription(entityDescriptionEng); entityData.dateAdded = entity.data.dateAdded; entityData.dateModified = entity.data.dateModified; entityDataUpdated.dateAdded = entity.data.dateAdded; entityDataUpdated.dateModified = entity.data.dateModified; 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.category.insert).toHaveBeenNthCalledWith(1, entityData); expect(model.category.description.insert).toHaveBeenNthCalledWith(1, Object.assign(Object.assign({}, entityDescriptionRus), { categoryId: entityId })); expect(model.category.description.insert).toHaveBeenNthCalledWith(2, Object.assign(Object.assign({}, entityDescriptionEng), { categoryId: entityId })); expect(model.category.path.insert).toHaveBeenNthCalledWith(1, entityPath); expect(model.category.toStore).toHaveBeenNthCalledWith(1, entityId, 0); expect(model.category.toLayout).toHaveBeenNthCalledWith(1, entityId, 0, 0); })); 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.category.update).toHaveBeenNthCalledWith(1, { categoryId: entityId, }, Object.assign(Object.assign({}, entityDataUpdated), { categoryId: entityId })); expect(model.category.description.update).toHaveBeenNthCalledWith(1, { categoryId: entityId, languageId: 1, }, Object.assign(Object.assign({}, entityDescriptionRusUpdated), { categoryId: entityId })); expect(model.category.description.update).toHaveBeenNthCalledWith(2, { categoryId: entityId, languageId: 2, }, Object.assign(Object.assign({}, entityDescriptionEngUpdated), { categoryId: entityId })); })); test("Delete", () => __awaiter(void 0, void 0, void 0, function* () { yield entity.delete(); expect(model.category.delete).toHaveBeenNthCalledWith(1, { categoryId: entityId, }); expect(model.category.description.delete).toHaveBeenNthCalledWith(1, { categoryId: 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); }));