opencart-manager
Version:
Node.js package helping to manage your opencart store data easily.
84 lines (83 loc) • 4.55 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 entity_1 = __importDefault(require("../__mocks__/entity"));
const option_value_1 = __importDefault(require("../entities/option-value"));
const initial_1 = require("../initial");
const model_1 = __importDefault(require("../model"));
const option_value_2 = __importDefault(require("./option-value"));
const optionValueId = 23;
const optionId = 123;
const entityRowsData = [
Object.assign(Object.assign({}, initial_1.initialOptionValue), { optionValueId,
optionId, sortOrder: 123 }),
];
const entityDescriptionRowsData = [
Object.assign(Object.assign({}, initial_1.initialOptionValueDescription), { optionValueId, languageId: 1, name: "test1" }),
Object.assign(Object.assign({}, initial_1.initialOptionValueDescription), { optionValueId, languageId: 2, name: "test2" }),
];
jest.mock("../entities/option-value", () => ({
__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.value.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return entityRowsData; }));
model.option.value.description.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return entityDescriptionRowsData; }));
const factory = new option_value_2.default(model);
test("creates a new entity", () => {
const entity = factory.create({ id: optionId });
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({ optionValueId });
expect(entity).toBeInstanceOf(entity_1.default);
expect(model.option.value.select).toHaveBeenNthCalledWith(1, {
optionValueId,
});
expect(model.option.value.description.select).toHaveBeenNthCalledWith(1, {
optionValueId,
});
expect(option_value_1.default).toHaveBeenNthCalledWith(2, model, optionId, optionValueId);
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({ optionValueId });
expect(entities).toBeArray();
const entity = entities[0];
expect(entity).toBeInstanceOf(entity_1.default);
expect(model.option.value.select).toHaveBeenNthCalledWith(2, {
optionValueId,
});
expect(model.option.value.description.select).toHaveBeenNthCalledWith(2, {
optionValueId,
});
expect(option_value_1.default).toHaveBeenNthCalledWith(3, model, optionId, optionValueId);
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.value.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return []; }));
const entity = yield factory.extract({ optionValueId: 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({ optionValueId: 9999 });
expect(entities).toBeArray();
expect(entities.length).toBe(0);
}));