UNPKG

opencart-manager

Version:

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

78 lines (77 loc) 4.27 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 entity_1 = __importDefault(require("../__mocks__/entity")); const option_1 = __importDefault(require("../entities/option")); const initial_1 = require("../initial"); const model_1 = __importDefault(require("../model")); const option_2 = __importDefault(require("./option")); const optionId = 12; const entityRowsData = [ Object.assign(Object.assign({}, initial_1.initialOption), { optionId, sortOrder: 123 }), ]; const entityDescriptionRowsData = [ Object.assign(Object.assign({}, initial_1.initialOptionDescription), { optionId, languageId: 1, name: "test1" }), Object.assign(Object.assign({}, initial_1.initialOptionDescription), { optionId, languageId: 2, name: "test2" }), ]; jest.mock("../entities/option", () => ({ __esModule: true, default: jest.fn((model, ...dependencies) => new entity_1.default(model, ...dependencies)), })); const database = new database_1.default(); const model = new model_1.default(database); model.option.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return entityRowsData; })); model.option.description.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return entityDescriptionRowsData; })); const factory = new option_2.default(model); test("creates a new entity", () => { const entity = factory.create(); expect(entity).toBeInstanceOf(entity_1.default); }); test("extracts and returns an entity", () => __awaiter(void 0, void 0, void 0, function* () { const entity = yield factory.extract({ optionId }); expect(entity).toBeInstanceOf(entity_1.default); expect(model.option.select).toHaveBeenNthCalledWith(1, { optionId }); expect(model.option.description.select).toHaveBeenNthCalledWith(1, { optionId, }); expect(option_1.default).toHaveBeenNthCalledWith(2, model, optionId); expect(entity.data.sortOrder).toBe(123); expect(entity.description[1].name).toBe("test1"); expect(entity.description[2].name).toBe("test2"); })); test("extracts all and returns an entities array", () => __awaiter(void 0, void 0, void 0, function* () { const entities = yield factory.extractAll({ optionId }); expect(entities).toBeArray(); const entity = entities[0]; expect(entity).toBeInstanceOf(entity_1.default); expect(model.option.select).toHaveBeenNthCalledWith(2, { optionId }); expect(model.option.description.select).toHaveBeenNthCalledWith(2, { optionId, }); expect(option_1.default).toHaveBeenNthCalledWith(3, model, optionId); expect(entity.data.sortOrder).toBe(123); expect(entity.description[1].name).toBe("test1"); expect(entity.description[2].name).toBe("test2"); })); test("extracts and returns an undefined", () => __awaiter(void 0, void 0, void 0, function* () { model.option.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return []; })); const entity = yield factory.extract({ optionId: 9999 }); expect(entity).toBeUndefined(); })); test("extracts all and returns an empty array", () => __awaiter(void 0, void 0, void 0, function* () { const entities = yield factory.extractAll({ optionId: 9999 }); expect(entities).toBeArray(); expect(entities.length).toBe(0); }));