UNPKG

opencart-manager

Version:

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

140 lines (139 loc) 9.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 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")); const category_1 = __importDefault(require("./category")); const product_1 = __importDefault(require("./product")); let entity; const entityId = 12; const entityData = { quantity: 2, price: 1111 }; const entityDescriptionRus = { languageId: 1, name: "RuName" }; const entityDescriptionEng = { languageId: 2, name: "EngName" }; const entityDataUpdated = Object.assign(Object.assign({}, entityData), { price: 2222 }); 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); const categoryNotInserted = new category_1.default(model); const category = new category_1.default(model, 6); const attributeNotInserted = new attribute_1.default(model, 321); const attribute = new attribute_1.default(model, 321, 5); model.product.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.description.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.description.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.description.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.toStore = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.toCategory = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.attribute.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.attribute.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.special.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); model.product.image.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; })); test("Instantiate", () => { entity = new product_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); expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.toCategory(categoryNotInserted); })).rejects.toThrow(errors_1.default.ENTITY_NOT_INSERTED); expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.setAttribute(attributeNotInserted, {}); })).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.product.insert).toHaveBeenNthCalledWith(1, entityData); expect(model.product.toStore).toHaveBeenNthCalledWith(1, entityId, 0); expect(model.product.description.insert).toHaveBeenNthCalledWith(1, Object.assign(Object.assign({}, entityDescriptionRus), { productId: entityId })); expect(model.product.description.insert).toHaveBeenNthCalledWith(2, Object.assign(Object.assign({}, entityDescriptionEng), { productId: entityId })); expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.toCategory(categoryNotInserted); })).rejects.toThrow(errors_1.default.NOT_SPECIFIED("category.id")); expect(() => __awaiter(void 0, void 0, void 0, function* () { return entity.setAttribute(attributeNotInserted, {}); })).rejects.toThrow(errors_1.default.NOT_SPECIFIED("attribute.id")); })); 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.product.update).toHaveBeenNthCalledWith(1, { productId: entityId, }, Object.assign(Object.assign({}, entityDataUpdated), { productId: entityId })); expect(model.product.description.update).toHaveBeenNthCalledWith(1, { productId: entityId, languageId: 1, }, Object.assign(Object.assign({}, entityDescriptionRusUpdated), { productId: entityId })); expect(model.product.description.update).toHaveBeenNthCalledWith(2, { productId: entityId, languageId: 2, }, Object.assign(Object.assign({}, entityDescriptionEngUpdated), { productId: entityId })); })); test("toCategory", () => __awaiter(void 0, void 0, void 0, function* () { yield entity.toCategory(category); expect(model.product.toCategory).toHaveBeenNthCalledWith(1, entityId, category.id, 0); })); test("setAttribute", () => __awaiter(void 0, void 0, void 0, function* () { yield entity.setAttribute(attribute, { languageId: 1, text: "asd" }); expect(model.product.attribute.insert).toHaveBeenNthCalledWith(1, { productId: entityId, attributeId: attribute.id, languageId: 1, text: "asd", }); })); test("Delete", () => __awaiter(void 0, void 0, void 0, function* () { yield entity.delete(); expect(model.product.delete).toHaveBeenNthCalledWith(1, { productId: entityId, }); expect(model.product.description.delete).toHaveBeenNthCalledWith(1, { productId: entityId, }); expect(model.product.attribute.delete).toHaveBeenNthCalledWith(1, { productId: entityId, }); expect(model.product.special.delete).toHaveBeenNthCalledWith(1, { productId: entityId, }); expect(model.product.image.delete).toHaveBeenNthCalledWith(1, { productId: 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); }));