const app = require('../../../../../client/server/server'); const catchErrors = require('../../../../../../services/utils/jasmineHelpers').catchErrors; const restoreFixtures = require('../../../../../../services/db/testing_fixtures'); describe('Client Create', () => { let sqlStatements = {deletes: ` DELETE FROM vn.address WHERE nickname = "Wade"; DELETE FROM vn.client WHERE name = "Wade"; DELETE FROM account.user WHERE name = "Deadpool"; `, inserts: ``, updates: ``}; beforeAll(() => { restoreFixtures(sqlStatements); }); afterAll(() => { restoreFixtures(sqlStatements); }); let newAccountData = { active: true, name: 'Wade', userName: 'Deadpool', email: 'Deadpool@marvel.com', fi: '16195279J', socialName: 'Deadpool Marvel', salesPersonFk: 1 }; it('should find Charles Xavier', done => { app.models.Account.findOne({where: {name: 'CharlesXavier'}}) .then(account => { expect(account.name).toEqual('CharlesXavier'); done(); }); }); it(`should not find Deadpool as he's not created yet`, done => { app.models.Account.findOne({where: {name: newAccountData.userName}}) .then(account => { expect(account).toEqual(null); app.models.Client.findOne({where: {name: newAccountData.name}}) .then(client => { expect(client).toEqual(null); done(); }); }) .catch(catchErrors(done)); }); it('should not be able to create a user if exists', done => { app.models.Client.findOne({where: {name: 'Charles Xavier'}}) .then(client => { app.models.Account.findOne({where: {id: client.id}}) .then(account => { let formerAccountData = { name: client.name, userName: account.name, email: client.email, fi: client.fi, socialName: client.socialName }; app.models.Client.createWithUser(formerAccountData, (err, client) => { expect(err.details.codes.name[0]).toEqual('uniqueness'); done(); }); }); }) .catch(catchErrors(done)); }); it('should create a new account', done => { app.models.Client.createWithUser(newAccountData, (error, client) => { if (error) return catchErrors(done)(error); app.models.Account.findOne({where: {name: newAccountData.userName}}) .then(account => { expect(account.name).toEqual(newAccountData.userName); app.models.Client.findOne({where: {name: newAccountData.name}}) .then(client => { expect(client.id).toEqual(account.id); expect(client.name).toEqual(newAccountData.name); expect(client.email).toEqual(newAccountData.email); expect(client.fi).toEqual(newAccountData.fi); expect(client.socialName).toEqual(newAccountData.socialName); done(); }); }) .catch(catchErrors(done)); }); }); });