const app = require('vn-loopback/server/server'); const LoopBackContext = require('loopback-context'); describe('loopback model Supplier-account', () => { describe('create', () => { const supplierId = 1; const bankEntityId = 2100; it('should throw an error when attempting to set an invalid iban account', async() => { let error; const expectedError = 'The IBAN does not have the correct format'; const iban = 'incorrect format'; try { await app.models.SupplierAccount.create( { supplierFk: supplierId, bankEntityFk: bankEntityId, iban: iban }); } catch (e) { error = e; expect(error.message).toContain(expectedError); } expect(error).toBeDefined(); }); it('should create a valid supplier account', async() => { const tx = await app.models.Claim.beginTransaction({}); try { const options = {transaction: tx}; const iban = 'ES91 2100 0418 4502 0005 1332'; const activeCtx = { accessToken: {userId: 5}, http: { req: { headers: {origin: 'http://localhost'} } } }; activeCtx.http.req.__ = value => { return value; }; spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); const createdSupplierAccount = await app.models.SupplierAccount.create({ supplierFk: supplierId, bankEntityFk: bankEntityId, iban: iban }, options); expect(createdSupplierAccount.iban).toBe(iban); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); }); });