const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('loopback model Supplier', () => { let supplierOne; let supplierTwo; beforeAll(async() => { supplierOne = await models.Supplier.findById(1); supplierTwo = await models.Supplier.findById(442); const activeCtx = { accessToken: {userId: 9}, http: { req: { headers: {origin: 'http://localhost'} } } }; spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); }); describe('payMethodFk', () => { it('should throw an error when attempting to set an invalid payMethod id in the supplier', async() => { const tx = await models.Supplier.beginTransaction({}); const options = {transaction: tx}; try { let error; const expectedError = 'You can not select this payment method without a registered bankery account'; await supplierOne.updateAttribute('payMethodFk', 8, options) .catch(e => { error = e; expect(error.message).toContain(expectedError); }); expect(error).toBeDefined(); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should not throw if the payMethod id is valid', async() => { const tx = await models.Supplier.beginTransaction({}); const options = {transaction: tx}; try { let error; await supplierTwo.updateAttribute('payMethodFk', 4, options) .catch(e => { error = e; }); expect(error).not.toBeDefined(); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should have checked isPayMethodChecked for payMethod hasVerfified is false', async() => { const tx = await models.Supplier.beginTransaction({}); const options = {transaction: tx}; try { await supplierTwo.updateAttribute('isPayMethodChecked', true, options); await supplierTwo.updateAttribute('payMethodFk', 5, options); const result = await models.Supplier.findById(442, null, options); expect(result.isPayMethodChecked).toEqual(true); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should have unchecked isPayMethodChecked for payMethod hasVerfified is true', async() => { const tx = await models.Supplier.beginTransaction({}); const options = {transaction: tx}; try { await supplierTwo.updateAttribute('isPayMethodChecked', true, options); await supplierTwo.updateAttribute('payMethodFk', 2, options); const result = await models.Supplier.findById(442, null, options); expect(result.isPayMethodChecked).toEqual(false); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should have unchecked isPayMethodChecked for payDay and peyDemFk', async() => { const tx = await models.Supplier.beginTransaction({}); const options = {transaction: tx}; try { await supplierTwo.updateAttribute('payMethodFk', 2, options); await supplierTwo.updateAttribute('isPayMethodChecked', true, options); await supplierTwo.updateAttribute('payDay', 5, options); const firstResult = await models.Supplier.findById(442, null, options); await supplierTwo.updateAttribute('isPayMethodChecked', true, options); await supplierTwo.updateAttribute('payDemFk', 1, options); const secondResult = await models.Supplier.findById(442, null, options); expect(firstResult.isPayMethodChecked).toEqual(false); expect(secondResult.isPayMethodChecked).toEqual(false); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should update the account attribute when a new supplier is created', async() => { const tx = await models.Supplier.beginTransaction({}); const options = {transaction: tx}; try { const newSupplier = await models.Supplier.create({name: 'ALFRED PENNYWORTH'}, options); const fetchedSupplier = await models.Supplier.findById(newSupplier.id, null, options); expect(Number(fetchedSupplier.account)).toEqual(4100000000 + newSupplier.id); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); }); });