opencart-manager
Version:
Node.js package helping to manage your opencart store data easily.
87 lines (86 loc) • 4.95 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 product_option_value_1 = __importDefault(require("./product-option-value"));
let entity;
const entityId = 12;
const productId = 55;
const optionId = 66;
const productOptionId = 145;
const optionValueId = 130;
const entityData = { price: 1111 };
const entityDataUpdated = { price: 2222 };
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.product.option.value.insert = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.product.option.value.update = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.product.option.value.delete = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return headers; }));
model.product.option.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return [{ productId }]; }));
model.option.value.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return [{ optionId }]; }));
test("Instantiate", () => {
entity = new product_option_value_1.default(model, productOptionId, optionValueId).setData(entityData);
expect(entity.id).toBeUndefined();
expect(entity.data).toStrictEqual(entityData);
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(model.product.option.value.insert).toHaveBeenNthCalledWith(1, Object.assign(Object.assign({}, entityData), { productOptionId,
productId,
optionId,
optionValueId }));
expect(model.product.option.select).toHaveBeenNthCalledWith(1, {
productOptionId,
});
expect(model.option.value.select).toHaveBeenNthCalledWith(1, {
optionValueId,
});
}));
test("Update", () => __awaiter(void 0, void 0, void 0, function* () {
entity.setData(entityDataUpdated);
yield entity.update();
expect(entity.id).toBe(headers.insertId);
expect(entity.data).toStrictEqual(entityDataUpdated);
expect(model.product.option.select).toHaveBeenNthCalledWith(2, {
productOptionId,
});
expect(model.option.value.select).toHaveBeenNthCalledWith(2, {
optionValueId,
});
expect(model.product.option.value.update).toHaveBeenNthCalledWith(1, {
productOptionValueId: entityId,
}, Object.assign(Object.assign({}, entityDataUpdated), { productOptionValueId: entityId, productOptionId,
productId,
optionId,
optionValueId }));
}));
test("Delete", () => __awaiter(void 0, void 0, void 0, function* () {
yield entity.delete();
expect(model.product.option.value.delete).toHaveBeenNthCalledWith(1, {
productOptionValueId: entityId,
});
expect(entity.id).toBeUndefined();
expect(entity.data).toStrictEqual(entityDataUpdated);
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);
}));