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 }); }); afterAll(async() => { await supplierOne.updateAttribute('payMethodFk', supplierOne.payMethodFk); await supplierTwo.updateAttribute('payMethodFk', supplierTwo.payMethodFk); }); describe('payMethodFk', () => { it('should throw an error when attempting to set an invalid payMethod id in the supplier', async() => { let error; const expectedError = 'You can not select this payment method without a registered bankery account'; const supplier = await models.Supplier.findById(1); await supplier.updateAttribute('payMethodFk', 8) .catch(e => { error = e; expect(error.message).toContain(expectedError); }); expect(error).toBeDefined(); }); it('should not throw if the payMethod id is valid', async() => { let error; const supplier = await models.Supplier.findById(442); await supplier.updateAttribute('payMethodFk', 4) .catch(e => { error = e; }); expect(error).not.toBeDefined(); }); it('should have checked isPayMethodChecked for payMethod hasVerfified is false', async() => { const supplier = await models.Supplier.findById(442); await supplier.updateAttribute('isPayMethodChecked', true); await supplier.updateAttribute('payMethodFk', 5); const result = await models.Supplier.findById(442); expect(result.isPayMethodChecked).toEqual(true); }); it('should have unchecked isPayMethodChecked for payMethod hasVerfified is true', async() => { const supplier = await models.Supplier.findById(442); await supplier.updateAttribute('isPayMethodChecked', true); await supplier.updateAttribute('payMethodFk', 2); const result = await models.Supplier.findById(442); expect(result.isPayMethodChecked).toEqual(false); }); it('should have unchecked isPayMethodChecked for payDay and peyDemFk', async() => { const supplier = await models.Supplier.findById(442); await supplier.updateAttribute('isPayMethodChecked', true); await supplier.updateAttribute('payDay', 5); const firstResult = await models.Supplier.findById(442); await supplier.updateAttribute('isPayMethodChecked', true); await supplier.updateAttribute('payDemFk', 1); const secondResult = await models.Supplier.findById(442); expect(firstResult.isPayMethodChecked).toEqual(false); expect(secondResult.isPayMethodChecked).toEqual(false); }); }); });