@tomei/product
Version:
NestJS package for product module
242 lines • 9.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const sso_1 = require("@tomei/sso");
const product_repository_1 = require("../src/base/product/product.repository");
const product_with_inventory_repository_1 = require("../src/base/product-with-inventory/product-with-inventory.repository");
const config_1 = require("@tomei/config");
const activity_history_1 = require("@tomei/activity-history");
const jewellery_product_1 = require("../src/components/jewellery-product/jewellery-product");
const jewellery_product_repository_1 = require("../src/components/jewellery-product/jewellery-product.repository");
const stock_1 = require("@tomei/stock");
describe('JewelleryProduct', () => {
const data = {
ProductId: '123',
Name: 'Test',
Description: 'Test',
SKU: 'Test',
Type: 'Test',
Remark: 'Test',
IsTaxableYN: 'N',
TaxCode: 'Test',
IsPriceInclusiveTaxYN: 'N',
Status: 'Active',
VerifiedYN: 'Y',
VerifiedById: '123',
VerifiedAt: new Date(),
VariantLevels: 0,
VariantTypeLevel1: '',
VariantTypeLevel2: '',
VariantTypeLevel3: '',
CreatedById: '123',
CreatedAt: new Date(),
UpdatedById: '123',
UpdatedAt: new Date(),
UpdatedSSYN: 'Y',
UOM: 'Test',
TotalUnits: 1,
TotalUnitsAvailable: 2,
TotalUnitsInCurrentOrder: 3,
TotalUnitsSold: 4,
TotalUnitsReserved: 5,
TotalUnitsOnConsignment: 6,
TotalUnitsUnderMaintenance: 7,
TotalUnitsVoid: 8,
TotalUnitsInTransit: 9,
TotalUnitsBackOrdered: 10,
TotalUnitsPreOrdered: 11,
StockLowAlertLevel: 12,
StockReorderLevel: 13,
BufferStockLevel: 14,
Surface: 'test',
Uniqueness: 'test',
ProfileId: '456',
HasCertificateYN: 'N',
};
const data2 = {
ProductId: '123',
Surface: 'test',
Uniqueness: 'test',
ProfileId: '456',
HasCertificateYN: 'N',
ProductWithInventory: {
ProductId: '123',
Name: 'Test',
Description: 'Test',
SKU: 'Test',
Type: 'Test',
Remark: 'Test',
IsTaxableYN: 'N',
TaxCode: 'Test',
IsPriceInclusiveTaxYN: 'N',
Status: 'Active',
VerifiedYN: 'Y',
VerifiedById: '123',
VerifiedAt: new Date(),
VariantLevels: 0,
VariantTypeLevel1: '',
VariantTypeLevel2: '',
VariantTypeLevel3: '',
CreatedById: '123',
CreatedAt: new Date(),
UpdatedById: '123',
UpdatedAt: new Date(),
UpdatedSSYN: 'Y',
UOM: 'Test',
TotalUnits: 1,
TotalUnitsAvailable: 2,
TotalUnitsInCurrentOrder: 3,
TotalUnitsSold: 4,
TotalUnitsReserved: 5,
TotalUnitsOnConsignment: 6,
TotalUnitsUnderMaintenance: 7,
TotalUnitsVoid: 8,
TotalUnitsInTransit: 9,
TotalUnitsBackOrdered: 10,
TotalUnitsPreOrdered: 11,
StockLowAlertLevel: 12,
StockReorderLevel: 13,
BufferStockLevel: 14,
Product: {
ProductId: '123',
Name: 'Test',
Description: 'Test',
SKU: 'Test',
Type: 'Test',
Remark: 'Test',
IsTaxableYN: 'N',
TaxCode: 'Test',
IsPriceInclusiveTaxYN: 'N',
Status: 'Active',
VerifiedYN: 'Y',
VerifiedById: '123',
VerifiedAt: new Date(),
VariantLevels: 0,
VariantTypeLevel1: '',
VariantTypeLevel2: '',
VariantTypeLevel3: '',
CreatedById: '123',
CreatedAt: new Date(),
UpdatedById: '123',
UpdatedAt: new Date(),
UpdatedSSYN: 'Y',
},
},
};
const mockStockProfile = new stock_1.StockProfile.prototype.constructor({
ProfileId: '123',
Name: 'test',
Description: 'test',
InventoryType: 'test',
UOM: 'test',
Category: 'test',
CategoryType: 'test',
TotalUnits: 1,
TotalUnitsAvailable: 1,
TotalUnitsInCurrentOrder: 1,
TotalUnitsSold: 1,
TotalUnitsReserved: 1,
TotalUnitsOnConsignment: 1,
TotalUnitsVoid: 1,
TotalUnitsInTransit: 1,
TotalUnitsBackOrdered: 1,
CreatedById: '123',
CreatedAt: new Date(),
UpdatedById: '123',
UpdatedAt: new Date(),
});
const pRepoCreate = jest
.spyOn(product_repository_1.ProductRepository.prototype, 'create')
.mockResolvedValue(Object.assign(Object.assign({}, data), { get: () => data }));
const pwiRepoCreate = jest
.spyOn(product_with_inventory_repository_1.ProductWithInventoryRepository.prototype, 'create')
.mockResolvedValue(Object.assign(Object.assign({}, data), { get: () => data }));
const jpRepoCreate = jest
.spyOn(jewellery_product_repository_1.ProductJewelleryRepository.prototype, 'create')
.mockResolvedValue(Object.assign(Object.assign({}, data2), { get: () => data2 }));
jest.spyOn(stock_1.StockProfile, 'init').mockResolvedValue(mockStockProfile);
jest.spyOn(product_repository_1.ProductRepository.prototype, 'findByPk').mockResolvedValue(Object.assign(Object.assign({}, data), { get: () => data }));
jest
.spyOn(product_with_inventory_repository_1.ProductWithInventoryRepository.prototype, 'findByPk')
.mockResolvedValue(Object.assign(Object.assign({}, data), { get: () => data }));
const jpfMock = jest
.spyOn(jewellery_product_repository_1.ProductJewelleryRepository.prototype, 'findOne')
.mockResolvedValue(Object.assign(Object.assign({}, data2), { get: () => data2 }));
jest.spyOn(sso_1.SessionService, 'init').mockResolvedValue({
setUserSession: jest.fn(),
retrieveUserSession: jest.fn(),
refreshDuration: jest.fn(),
});
jest
.spyOn(config_1.ApplicationConfig, 'getComponentConfigValue')
.mockResolvedValue('ezc');
jest
.spyOn(sso_1.LoginUser.prototype, 'checkPrivileges')
.mockResolvedValue(true);
const activityMock = jest
.spyOn(activity_history_1.Activity.prototype, 'create')
.mockResolvedValue(undefined);
jest.spyOn(product_repository_1.ProductRepository.prototype, 'findByPk').mockResolvedValue(Object.assign(Object.assign({}, data), { get: () => data }));
beforeEach(() => {
jest.clearAllMocks();
});
describe('init', () => {
it('should be able to create a new jewellery product', async () => {
const product = await jewellery_product_1.ProductJewellery.init({
productInfo: data,
});
expect(product).toBeDefined();
expect(product).toBeInstanceOf(jewellery_product_1.ProductJewellery);
});
it('should initialize the product base with existing data', async () => {
const product = await jewellery_product_1.ProductJewellery.init({
productId: data.ProductId,
});
expect(product.ProductId).toBe('123');
expect(product.Surface).toBe('test');
expect(product.Uniqueness).toBe('test');
expect(product.ProfileId).toBe('456');
expect(product.HasCertificateYN).toBe('N');
});
});
describe('create', () => {
it('should be able to create a new jewellery product', async () => {
jest
.spyOn(product_repository_1.ProductRepository.prototype, 'findOne')
.mockReturnValueOnce(null);
const product = await jewellery_product_1.ProductJewellery.init({
productId: data.ProductId,
});
const sessionService = await sso_1.SessionService.init();
const loginUser = await sso_1.LoginUser.init(sessionService);
loginUser.ObjectId = '1234567890';
await product.create(loginUser, {}, data.ProfileId);
expect(pRepoCreate).toBeCalledTimes(1);
expect(pwiRepoCreate).toBeCalledTimes(1);
expect(jpRepoCreate).toBeCalledTimes(1);
expect(activityMock).toBeCalledTimes(3);
});
it('should throw error if stock profile not found', async () => {
jest.spyOn(stock_1.StockProfile, 'init').mockImplementationOnce(() => {
throw new Error('ProfileId not found');
});
jest
.spyOn(product_repository_1.ProductRepository.prototype, 'findOne')
.mockReturnValueOnce(null);
const product = await jewellery_product_1.ProductJewellery.init({
productId: data.ProductId,
});
try {
const sessionService = await sso_1.SessionService.init();
const loginUser = await sso_1.LoginUser.init(sessionService);
loginUser.ObjectId = '1234567890';
loginUser.ObjectId = '1234567890';
await product.create(loginUser, {}, data.ProfileId);
}
catch (error) {
expect(error.message).toEqual('Stock Profile not found. Please create Stock Profile first.');
expect(error.code).toEqual('ProductJewelleryErrMsg01');
}
});
});
});
//# sourceMappingURL=jewellery-product.spec.js.map