import { GCMockModule } from '@core/mocks/gc-module.mock'; import { AfterEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect, spy } from 'chai'; import { InvitationService } from './invitation.service'; import { ApplicantFromDistributionList } from './invitation.typing'; @DescribeAngularService(InvitationService, { imports: [ GCMockModule ] }) export class InvitationServiceSpec implements Spec { applicants: ApplicantFromDistributionList[] = [{ firstName: 'George', lastName: 'Ivey', email: 'parkeryc2017+george@gmail.com', isEmployee: false }, { firstName: 'Josie', lastName: 'Conway', email: 'josie.conway@mailinator.com', isEmployee: false }, { firstName: 'Elizabeth Parker', lastName: 'Crawford', email: 'parker.crawford@blackbaud.com', isEmployee: true }, { firstName: 'Tina', lastName: 'Conway', email: 'kc54861+new@gmail.com', isEmployee: false }, { firstName: 'Kenny', lastName: 'Pickett', email: 'kenny.pickett@mailinator.com', isEmployee: false }]; @AfterEach() restore () { spy.restore(); } @TestCase('should be able to download template - with records') async downloadTemplateForImportDistributionList (service: InvitationService) { service['invitationResources']['exportDistributionList'] = async () => { return this.applicants; }; spy.on(service, 'parse'); spy.on(service['fileService'], 'downloadCSV', () => {}); await service.downloadTemplateForImportDistributionList(1); expect(service.parse).to.have.been.called.once; expect(service['fileService']['downloadCSV']).to.have.been.called.once; } @TestCase('should be able to download template - without records') async downloadTemplateForImportDistributionListNoRecords (service: InvitationService) { service['invitationResources']['exportDistributionList'] = async () => { return []; }; spy.on(service['fileService'], 'downloadString', () => {}); await service.downloadTemplateForImportDistributionList(1); expect(service['fileService']['downloadString']).to.have.been.called.once; } @TestCase('should be able to import distribution list - pass') async importDistributionListPass (service: InvitationService) { spy.on(service['invitationResources'], 'importDistributionList', async () => {}); const pass = await service.importDistributionList(1, new Blob()); expect(service['invitationResources']['importDistributionList']).to.have.been.called.once; expect(pass).to.be.true; } @TestCase('should be able to import distribution list - fail') async importDistributionListFail (service: InvitationService) { spy.on(service['invitationResources'], 'importDistributionList', async () => { /* eslint-disable no-throw-literal */ throw { error: { message: 'There was an error' } }; }); const pass = await service.importDistributionList(1, new Blob()); expect(service['invitationResources']['importDistributionList']).to.have.been.called.once; expect(pass).to.be.false; } }