UNPKG

@tomei/product

Version:

NestJS package for product module

412 lines 20.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const product_repository_1 = require("../src/base/product/product.repository"); const globals_1 = require("@jest/globals"); const product_base_1 = require("../src/base/product/product.base"); const sso_1 = require("@tomei/sso"); const config_1 = require("@tomei/config"); const activity_history_1 = require("@tomei/activity-history"); (0, globals_1.describe)('ProductBase', () => { class TestProduct extends product_base_1.ProductBase { constructor(product) { super(product); } } let 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: 0, TotalUnitsAvailable: 0, TotalUnitsInCurrentOrder: 0, TotalUnitsSold: 0, TotalUnitsReserved: 0, TotalUnitsOnConsignment: 0, TotalUnitsUnderMaintenance: 0, TotalUnitsVoid: 0, TotalUnitsInTransit: 0, TotalUnitsBackOrdered: 0, TotalUnitsPreOrdered: 0, StockLowAlertLevel: 0, StockReorderLevel: 0, BufferStockLevel: 0, }; (0, globals_1.beforeAll)(() => { }); const pRepoCreate = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'create') .mockResolvedValue(Object.assign(Object.assign({}, data), { get: () => data })); globals_1.jest.spyOn(product_repository_1.ProductRepository.prototype, 'findByPk').mockResolvedValue(Object.assign(Object.assign({}, data), { get: () => data })); globals_1.jest.spyOn(sso_1.SessionService, 'init').mockResolvedValue({ setUserSession: globals_1.jest.fn(), retrieveUserSession: globals_1.jest.fn(), refreshDuration: globals_1.jest.fn(), }); globals_1.jest .spyOn(config_1.ApplicationConfig, 'getComponentConfigValue') .mockResolvedValue('ezc'); globals_1.jest .spyOn(sso_1.LoginUser.prototype, 'checkPrivileges') .mockResolvedValue(true); globals_1.jest.spyOn(activity_history_1.Activity.prototype, 'create').mockResolvedValue(undefined); (0, globals_1.afterEach)(() => { globals_1.jest.clearAllMocks(); }); (0, globals_1.describe)('init', () => { (0, globals_1.it)('should initialize the product base with existing data', async () => { const product = new TestProduct(Object.assign({}, data)); (0, globals_1.expect)(product.ProductId).toBe('123'); (0, globals_1.expect)(product.Name).toBe('Test'); (0, globals_1.expect)(product.Description).toBe('Test'); (0, globals_1.expect)(product.SKU).toBe('Test'); (0, globals_1.expect)(product.Type).toBe('Test'); (0, globals_1.expect)(product.Remark).toBe('Test'); (0, globals_1.expect)(product.IsTaxableYN).toBe('N'); (0, globals_1.expect)(product.TaxCode).toBe('Test'); (0, globals_1.expect)(product.IsPriceInclusiveTaxYN).toBe('N'); (0, globals_1.expect)(product.Status).toBe('Active'); (0, globals_1.expect)(product.VerifiedYN).toBe('Y'); (0, globals_1.expect)(product.VerifiedById).toBe('123'); (0, globals_1.expect)(product.VerifiedAt).toBeInstanceOf(Date); (0, globals_1.expect)(product.VariantLevels).toBe(0); (0, globals_1.expect)(product.VariantTypeLevel1).toBe(''); (0, globals_1.expect)(product.VariantTypeLevel2).toBe(''); (0, globals_1.expect)(product.VariantTypeLevel3).toBe(''); (0, globals_1.expect)(product.CreatedById).toBe('123'); (0, globals_1.expect)(product.CreatedAt).toBeInstanceOf(Date); (0, globals_1.expect)(product.UpdatedById).toBe('123'); (0, globals_1.expect)(product.UpdatedAt).toBeInstanceOf(Date); (0, globals_1.expect)(product.UpdatedSSYN).toBe('Y'); }); }); (0, globals_1.describe)('delete', () => { (0, globals_1.it)('should delete product', async () => { let called = false; const findMock = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(Object.assign(Object.assign({}, data), { get: () => data, save: () => { called = true; } })); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; const product = new TestProduct(Object.assign({}, data)); await product.delete(loginUser); (0, globals_1.expect)(findMock).toBeCalledTimes(1); (0, globals_1.expect)(called).toBeTruthy(); (0, globals_1.expect)(product['Status']).toBe('Deleted'); (0, globals_1.expect)(product['UpdatedById']).toBe('1234567890'); (0, globals_1.expect)(product['UpdatedSSYN']).toBe('N'); (0, globals_1.expect)(true).toBe(true); }); (0, globals_1.it)('should throw error if user dont have the privileges to delete product', async () => { globals_1.jest .spyOn(sso_1.LoginUser.prototype, 'checkPrivileges') .mockResolvedValueOnce(false); try { const product = new TestProduct(Object.assign({}, data)); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.delete(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('You do not have permission to delete product.'); } }); }); (0, globals_1.describe)('findOne', () => { (0, globals_1.it)('should return product', async () => { const findOneMock = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(Object.assign(Object.assign({}, data), { get: () => data })); await TestProduct['findOne']({ where: { ProductId: '123', }, }); (0, globals_1.expect)(findOneMock).toBeCalledTimes(1); }); }); (0, globals_1.describe)('findAllWithPagination', () => { (0, globals_1.it)('should return products', async () => { const findAllWithPaginationMock = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findAllWithPagination') .mockReturnValueOnce({ count: 1, rows: [ Object.assign(Object.assign({}, data), { get: () => data }), ], }); const product = new TestProduct(Object.assign({}, data)); await product.findAllWithPagination({ where: { ProductId: '123', }, }); (0, globals_1.expect)(findAllWithPaginationMock).toBeCalledTimes(1); }); }); (0, globals_1.describe)('create', () => { (0, globals_1.it)('should create a new product', async () => { globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(null); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; const product = new TestProduct(Object.assign({}, data)); await product.create(loginUser); (0, globals_1.expect)(pRepoCreate).toBeCalledTimes(1); }); (0, globals_1.it)('should throw error if SKU is already existed', async () => { globals_1.jest.spyOn(product_repository_1.ProductRepository.prototype, 'findOne').mockReturnValueOnce(Object.assign(Object.assign({}, data), { get: () => data })); const product = new TestProduct(Object.assign({}, data)); try { const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.create(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('SKU is already existed.'); } (0, globals_1.expect)(pRepoCreate).toBeCalledTimes(0); }); (0, globals_1.it)('should throw error if IsPriceInclusiveTaxYN not defined if IsTaxable equal to Y', async () => { const temp = Object.assign({}, data); data.IsTaxableYN = 'Y'; data.IsPriceInclusiveTaxYN = 'Y'; globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findByPk') .mockResolvedValueOnce(Object.assign(Object.assign({}, data), { get: () => data })); globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(null); const product = new TestProduct(Object.assign({}, data)); try { const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.create(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('IsPriceInclusiveTaxYN is required if IsTaxableYN equal Y.'); } data = temp; (0, globals_1.expect)(pRepoCreate).toBeCalledTimes(0); }); (0, globals_1.it)('should throw error if TaxCode not defined if IsTaxable equal to Y', async () => { const temp = Object.assign({}, data); data.IsTaxableYN = 'Y'; data.TaxCode = null; globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findByPk') .mockResolvedValueOnce(Object.assign(Object.assign({}, data), { get: () => data })); globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(null); const product = new TestProduct(Object.assign({}, data)); try { const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.create(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('TaxCode is required if IsTaxableYN equal Y.'); } (0, globals_1.expect)(pRepoCreate).toBeCalledTimes(0); data = temp; }); (0, globals_1.it)('should throw error if user dont have the privileges to create product', async () => { globals_1.jest .spyOn(sso_1.LoginUser.prototype, 'checkPrivileges') .mockResolvedValueOnce(false); globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(null); try { const product = new TestProduct(Object.assign({}, data)); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.create(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('You do not have permission to add product.'); } }); }); (0, globals_1.describe)('update', () => { (0, globals_1.it)('should update product', async () => { const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; (0, globals_1.expect)(true).toBe(true); }); }); (0, globals_1.describe)('diactivate', () => { (0, globals_1.it)('should deactivate product', async () => { let called = false; const findMock = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(Object.assign(Object.assign({}, data), { get: () => data, save: () => { called = true; } })); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; const product = new TestProduct(Object.assign({}, data)); await product.deactivate(loginUser); (0, globals_1.expect)(findMock).toBeCalledTimes(1); (0, globals_1.expect)(called).toBeTruthy(); (0, globals_1.expect)(product['Status']).toBe('Inactive'); (0, globals_1.expect)(product['UpdatedById']).toBe('1234567890'); (0, globals_1.expect)(product['UpdatedSSYN']).toBe('N'); (0, globals_1.expect)(true).toBe(true); }); (0, globals_1.it)('should throw error if user dont have the privileges to deactivate product', async () => { globals_1.jest .spyOn(sso_1.LoginUser.prototype, 'checkPrivileges') .mockResolvedValueOnce(false); try { const product = new TestProduct(Object.assign({}, data)); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.deactivate(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('You do not have permission to deactivate product.'); } }); }); (0, globals_1.describe)('discontinue', () => { (0, globals_1.it)('should discontinue product', async () => { let called = false; const findMock = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(Object.assign(Object.assign({}, data), { get: () => data, save: () => { called = true; } })); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; const product = new TestProduct(Object.assign({}, data)); await product.discontinue(loginUser); (0, globals_1.expect)(findMock).toBeCalledTimes(1); (0, globals_1.expect)(called).toBeTruthy(); (0, globals_1.expect)(product['Status']).toBe('Discontinue'); (0, globals_1.expect)(product['UpdatedById']).toBe('1234567890'); (0, globals_1.expect)(product['UpdatedSSYN']).toBe('N'); (0, globals_1.expect)(true).toBe(true); }); (0, globals_1.it)('should throw error if user dont have the privileges to discontinue product', async () => { globals_1.jest .spyOn(sso_1.LoginUser.prototype, 'checkPrivileges') .mockResolvedValueOnce(false); try { const product = new TestProduct(Object.assign({}, data)); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.discontinue(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('You do not have permission to discontinue product.'); } }); }); (0, globals_1.describe)('activate', () => { (0, globals_1.it)('should activate product', async () => { let called = false; const findMock = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(Object.assign(Object.assign({}, data), { get: () => data, save: () => { called = true; } })); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; const product = new TestProduct(Object.assign({}, data)); await product.activate(loginUser); (0, globals_1.expect)(findMock).toBeCalledTimes(1); (0, globals_1.expect)(called).toBeTruthy(); (0, globals_1.expect)(product['Status']).toBe('Active'); (0, globals_1.expect)(product['UpdatedById']).toBe('1234567890'); (0, globals_1.expect)(product['UpdatedSSYN']).toBe('N'); (0, globals_1.expect)(true).toBe(true); }); (0, globals_1.it)('should throw error if user dont have the privileges to activate product', async () => { globals_1.jest .spyOn(sso_1.LoginUser.prototype, 'checkPrivileges') .mockResolvedValueOnce(false); try { const product = new TestProduct(Object.assign({}, data)); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.activate(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('You do not have permission to activate product.'); } }); }); (0, globals_1.describe)('verify', () => { (0, globals_1.it)('should activate product', async () => { let called = false; const findMock = globals_1.jest .spyOn(product_repository_1.ProductRepository.prototype, 'findOne') .mockReturnValueOnce(Object.assign(Object.assign({}, data), { get: () => data, save: () => { called = true; } })); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; const product = new TestProduct(Object.assign({}, data)); await product.verify(loginUser); (0, globals_1.expect)(findMock).toBeCalledTimes(1); (0, globals_1.expect)(called).toBeTruthy(); (0, globals_1.expect)(product['VerifiedYN']).toBe('Y'); (0, globals_1.expect)(product['VerifiedById']).toBe('1234567890'); (0, globals_1.expect)(true).toBe(true); }); (0, globals_1.it)('should throw error if user dont have the privileges to activate product', async () => { globals_1.jest .spyOn(sso_1.LoginUser.prototype, 'checkPrivileges') .mockResolvedValueOnce(false); try { const product = new TestProduct(Object.assign({}, data)); const sessionService = await sso_1.SessionService.init(); const loginUser = await sso_1.LoginUser.init(sessionService); loginUser.ObjectId = '1234567890'; await product.verify(loginUser); } catch (error) { (0, globals_1.expect)(error.message).toEqual('You do not have permission to verify product.'); } }); }); }); //# sourceMappingURL=product.base.spec.js.map