const app = require(`${servicesDir}/client/server/server`); describe('Client updateBillingData', () => { afterAll(async() => { let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; let validparams = { phone: 1111111111, payMethodFk: 5, dueDay: 0, iban: null, hasLcr: 0, hasCoreVnl: 1, hasSepaVnl: 1 }; let idWithDataChecked = 101; await app.models.Client.updateBillingData(ctxOfAdmin, validparams, idWithDataChecked); }); it('should return an error if the user is not administrative and the isTaxDataChecked value is true', async() => { let error; let ctxOfNoAdmin = {req: {accessToken: {userId: 1}}}; let params = {iban: null}; let idWithDataChecked = 101; await app.models.Client.updateBillingData(ctxOfNoAdmin, params, idWithDataChecked) .catch(e => { error = e; }); expect(error.message).toEqual(`You don't have enough privileges to do that`); }); it('should update the billing data and check if the changes were made', async() => { let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; let params = { phone: 2222222222, payMethodFk: 4, dueDay: 30, iban: 'ES91 2100 0418 4502 0005 1332', hasLcr: 1, hasCoreVnl: 0, hasSepaVnl: 0 }; let idWithDataChecked = 101; await app.models.Client.updateBillingData(ctxOfAdmin, params, idWithDataChecked); let client = await app.models.Client.findById(idWithDataChecked); expect(client.phone).not.toEqual(params.phone); expect(client.payMethodFk).toEqual(params.payMethodFk); expect(client.dueDay).toEqual(params.dueDay); expect(client.iban).toEqual(params.iban); expect(client.hasLcr).toBeTruthy(); expect(client.hasCoreVnl).toBeFalsy(); expect(client.hasSepaVnl).toBeFalsy(); }); it('should return an error if the given IBAN is an invalid one', async() => { let ctxOfAdmin = {req: {accessToken: {userId: 5}}}; let validparams = { payMethodFk: 5, iban: null}; let idWithDataChecked = 101; await app.models.Client.updateBillingData(ctxOfAdmin, validparams, idWithDataChecked); validparams = {iban: 12345}; try { await app.models.Client.updateBillingData(ctxOfAdmin, validparams, idWithDataChecked); } catch (error) { expect(error.toString()).toContain('The IBAN does not have the correct format'); } let client = await app.models.Client.findById(idWithDataChecked); expect(client.iban).toBeFalsy(); }); });