import { expect } from 'chai'; import { userData } from '../../fixtures/user-data'; import { User } from '../../../src/models/user'; import { getNewUserRequestBody, getUpdateUserRequestBody } from '../../../src/helpers/user'; describe('formatUserProfile', () => { let expectedResponse: any; let userDetails: any; beforeEach(() => { userDetails = Object.assign({}, userData); expectedResponse = { email: 'email@example.com', secondaryEmail: 'email@email.com', firstName: 'John', lastName: 'Doe', primaryTelephone: '12345678', homeAddress: { townCity: 'London', country: 'GBR', postcode: 'SE19HL', state: 'Greater London' }, id: 'fakeId', organisationName: 'company x', }; }); it('returns a formatted user profile with existing demographics', () => { expectedResponse.demographics = { industry: { code: userDetails.industry }, responsibility: { code: userDetails.responsibility }, position: { code: userDetails.position } }; expect(getUpdateUserRequestBody(userDetails)).to.eql(expectedResponse); }); context('update user profile structure', () => { it('returns a formatted user profile when only one demographic type is not undefined', () => { userDetails.responsibility = undefined; userDetails.position = undefined; expectedResponse.demographics = { industry: { code: userDetails.industry } }; expect(getUpdateUserRequestBody(userDetails)).to.eql(expectedResponse); }); it('returns a formatted user profile with out existing demographics', () => { userDetails.industry = undefined; userDetails.responsibility = undefined; userDetails.position = undefined; expect(getUpdateUserRequestBody(userDetails)).to.eql(expectedResponse); }); }); }); describe('validateUserProfile', () => { let expectedResponse: any; let userDetails: any; beforeEach(() => { userDetails = Object.assign({}, userData); expectedResponse = { email: 'email@example.com', secondaryEmail: 'email@email.com', firstName: 'John', lastName: 'Doe', primaryTelephone: '12345678', organisationName: 'company x', homeAddress: { townCity: 'London', country: 'GBR', postcode: 'SE19HL', state: 'Greater London' }, id: 'fakeId' }; }); it('new user request complains about empty password', () => { userDetails = Object.assign({}, { email: 'meh', country: 'meh' }); assertsUserValidation(userDetails, 'Password cannot be null', getNewUserRequestBody); }); it('update user request complains about empty id', () => { userDetails = Object.assign({}, { email: 'meh', country: 'meh' }); assertsUserValidation(userDetails, 'User ID cannot be null', getUpdateUserRequestBody); }); it('update user request complains about empty country and empty email', () => { assertsUserValidation(Object.assign({}, { id: 'meh', country: 'meh' } as any), 'Email cannot be null', getUpdateUserRequestBody); assertsUserValidation(Object.assign({}, { id: 'meh', email: 'meh' } as any), 'Country cannot be null', getUpdateUserRequestBody); }); it('new user request complains about empty country and empty email', () => { assertsUserValidation(Object.assign({}, { password: 'meh', country: 'meh' } as any), 'Email cannot be null', getNewUserRequestBody); assertsUserValidation(Object.assign({}, { password: 'meh', email: 'meh' } as any), 'Country cannot be null', getNewUserRequestBody); }); function assertsUserValidation(userDetails: User, errorMessage: string, validationFunction: Function) { let response: any; try { expect(validationFunction(userDetails)).to.eql(expectedResponse); expect.fail('Should have thrown the error!'); } catch (exception) { response = exception; } expect(response.message).to.equal(errorMessage); } context('update user profile structure', () => { it('returns a formatted user profile when only one demographic type is not undefined', () => { userDetails.responsibility = undefined; userDetails.position = undefined; expectedResponse.demographics = { industry: { code: userDetails.industry } }; expect(getUpdateUserRequestBody(userDetails)).to.eql(expectedResponse); }); it('returns a formatted user profile with out existing demographics', () => { userDetails.industry = undefined; userDetails.responsibility = undefined; userDetails.position = undefined; expect(getUpdateUserRequestBody(userDetails)).to.eql(expectedResponse); }); }); }); describe('getNewUserRequestBody', () => { let userDetails: any; beforeEach(() => { userDetails = Object.assign({}, userData); userDetails.password = 'password123'; }); it('should contain the postcode', () => { expect(getNewUserRequestBody(userDetails).homeAddress).to.contain({ postcode: userDetails.postcode }); }); it('should contain the state', () => { expect(getNewUserRequestBody(userDetails).homeAddress).to.contain({ state: userDetails.state }); }); }); describe('getUpdateUserRequestBody', () => { let userDetails: any; beforeEach(() => { userDetails = Object.assign({}, userData, { organisationId: 'orgId' }); }); it('should contain the postcode', () => { expect(getUpdateUserRequestBody(userDetails).homeAddress).to.contain({ postcode: userDetails.postcode }); }); it('should contain the state', () => { expect(getUpdateUserRequestBody(userDetails).homeAddress).to.contain({ state: userDetails.state }); }); it('should contain the organisationId', () => { expect(getUpdateUserRequestBody(userDetails).organisationId).to.equal(userDetails.organisationId); }); });