UNPKG

opencart-manager

Version:

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

97 lines (96 loc) 6.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 option_1 = __importDefault(require("./option")); let entity; const entityId = 12; const entityData = { type: "select" }; const entityDescriptionRus = { languageId: 1, name: "RuName" }; const entityDescriptionEng = { languageId: 2, name: "EngName" }; const entityDataUpdated = { type: "radio" }; 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.option.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.option.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.option.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.option.description.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.option.description.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.option.description.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); test("Instantiate", () => { entity = new option_1.default(model) .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.option.insert).toHaveBeenNthCalledWith(1, entityData); expect(model.option.description.insert).toHaveBeenNthCalledWith(1, Object.assign(Object.assign({}, entityDescriptionRus), { optionId: entityId })); expect(model.option.description.insert).toHaveBeenNthCalledWith(2, Object.assign(Object.assign({}, entityDescriptionEng), { optionId: 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.option.update).toHaveBeenNthCalledWith(1, { optionId: entityId, }, Object.assign(Object.assign({}, entityDataUpdated), { optionId: entityId })); expect(model.option.description.update).toHaveBeenNthCalledWith(1, { optionId: entityId, languageId: 1, }, Object.assign(Object.assign({}, entityDescriptionRusUpdated), { optionId: entityId })); expect(model.option.description.update).toHaveBeenNthCalledWith(2, { optionId: entityId, languageId: 2, }, Object.assign(Object.assign({}, entityDescriptionEngUpdated), { optionId: entityId })); })); test("Delete", () => __awaiter(void 0, void 0, void 0, function* () { yield entity.delete(); expect(model.option.delete).toHaveBeenNthCalledWith(1, { optionId: entityId, }); expect(model.option.description.delete).toHaveBeenNthCalledWith(1, { optionId: 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); }));