opencart-manager
Version:
Node.js package helping to manage your opencart store data easily.
80 lines (79 loc) • 4.48 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 attribute_1 = __importDefault(require("../entities/attribute"));
const initial_1 = require("../initial");
const model_1 = __importDefault(require("../model"));
const attribute_2 = __importDefault(require("./attribute"));
const attributeId = 1;
const attributeGroupId = 123;
const entityRowsData = [
Object.assign(Object.assign({}, initial_1.initialAttribute), { attributeId,
attributeGroupId, sortOrder: 123 }),
];
const entityDescriptionRowsData = [
Object.assign(Object.assign({}, initial_1.initialAttributeDescription), { attributeId, languageId: 1, name: "test1" }),
Object.assign(Object.assign({}, initial_1.initialAttributeDescription), { attributeId, languageId: 2, name: "test2" }),
];
jest.mock("../entities/attribute", () => ({
__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.attribute.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return entityRowsData; }));
model.attribute.description.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return entityDescriptionRowsData; }));
const factory = new attribute_2.default(model);
test("creates a new entity", () => {
const entity = factory.create({ id: attributeGroupId });
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({ attributeId });
expect(entity).toBeInstanceOf(entity_1.default);
expect(model.attribute.select).toHaveBeenNthCalledWith(1, { attributeId });
expect(model.attribute.description.select).toHaveBeenNthCalledWith(1, {
attributeId,
});
expect(attribute_1.default).toHaveBeenNthCalledWith(2, model, attributeGroupId, attributeId);
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({ attributeId });
expect(entities).toBeArray();
const entity = entities[0];
expect(entity).toBeInstanceOf(entity_1.default);
expect(model.attribute.select).toHaveBeenNthCalledWith(2, { attributeId });
expect(model.attribute.description.select).toHaveBeenNthCalledWith(2, {
attributeId,
});
expect(attribute_1.default).toHaveBeenNthCalledWith(3, model, attributeGroupId, attributeId);
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.attribute.select = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () { return []; }));
const entity = yield factory.extract({ attributeId: 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({ attributeId: 9999 });
expect(entities).toBeArray();
expect(entities.length).toBe(0);
}));