import { Injectable } from '@angular/core'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { AfterEach, BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect, spy } from 'chai'; import { PageHelpModalService } from './page-help-modals.service'; import { PageHelpInfoFromApi, PageHelpRoutes } from './page-help-modals.typing'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(PageHelpModalService, { imports: [ GCMockModule ] }) export class PageHelpModalServiceSpec implements Spec { completedPageHelp: PageHelpInfoFromApi[] = [{ tourKey: PageHelpRoutes.APPLICATION, dateCompleted: '' }]; @BeforeEach() mock (service: PageHelpModalService) { service['pageHelpModalResources']['getCompletedPageHelp'] = async () => { return this.completedPageHelp; }; } @AfterEach() restore () { spy.restore(); } @TestCase('should be able to set page help info') async setPageHelpInfo (service: PageHelpModalService) { spy.on(service['pageHelpModalResources'], 'getCompletedPageHelp', async () => { return this.completedPageHelp; }); await service.setPageHelpInfo(); expect(service['pageHelpModalResources']['getCompletedPageHelp']).to.have.been.called.once; const dismissedValue = !!service.pageHelpDismissedMap[PageHelpRoutes.APPLICATION]; // this route was not dismissed - dateCompleted is empty expect(dismissedValue).to.be.false; } @TestCase('should be able to check if we should open page help modal - dont open') async checkIfWeShouldOpenPageHelpModal (service: PageHelpModalService) { await service.setPageHelpInfo(); spy.on(service['locationService'], 'getGenericRoute', () => { return 'some route'; }); spy.on(service, 'shouldShowModal'); spy.on(service, 'openModal', () => {}); service.checkIfWeShouldOpenPageHelpModal(); expect(service['locationService']['getGenericRoute']).to.have.been.called.once; expect(service['shouldShowModal']).to.have.been.called.once; // The route is not part of our help, so it should not have opened the modal expect(service['openModal']).to.not.have.been.called; } @TestCase('should be able to check if we should open page help modal - should open') async checkIfWeShouldOpenPageHelpModalShouldOpen (service: PageHelpModalService) { await service.setPageHelpInfo(); spy.on(service['locationService'], 'getGenericRoute', () => { return PageHelpRoutes.APPLICATION; }); spy.on(service, 'shouldShowModal'); spy.on(service, 'openModal', () => {}); service.checkIfWeShouldOpenPageHelpModal(); expect(service['locationService']['getGenericRoute']).to.have.been.called.once; expect(service['shouldShowModal']).to.have.been.called.once; // Modal should show because we have not dismissed the route expect(service['openModal']).to.have.been.called.once; } @TestCase('should be able to get should show modal - true') async shouldShowModalTrue (service: PageHelpModalService) { await service.setPageHelpInfo(); const shouldShow = service.shouldShowModal(PageHelpRoutes.APPLICATION); expect(shouldShow).to.be.true; } @TestCase('should be able to get should show modal - false') async shouldShowModalFalse (service: PageHelpModalService) { await service.setPageHelpInfo(); const shouldShow = service.shouldShowModal(null); expect(shouldShow).to.be.false; } @TestCase('should be able to open modal with dismiss') async openModalAndDismiss (service: PageHelpModalService) { spy.on(service['modalFactory'], 'open', () => { return true; }); spy.on(service['pageHelpModalResources'], 'dismissPageHelp', async () => { return true; }); await service.openModal(PageHelpRoutes.APPLICATION); expect(service['modalFactory']['open']).to.have.been.called.once; expect(service['pageHelpModalResources']['dismissPageHelp']).to.have.been.called.once; } @TestCase('should be able to open modal no dismiss') async openModalNoDismiss (service: PageHelpModalService) { spy.on(service['modalFactory'], 'open', () => { return false; }); spy.on(service['pageHelpModalResources'], 'dismissPageHelp', async () => { return true; }); await service.openModal(PageHelpRoutes.APPLICATION); expect(service['modalFactory']['open']).to.have.been.called.once; expect(service['pageHelpModalResources']['dismissPageHelp']).to.not.have.been.called; } @TestCase('should be able to get help content by route - application') getHelpContentByRouteApplication (service: PageHelpModalService) { spy.on(service, 'getApplicationHelpContent'); service.getHelpContentByRoute(PageHelpRoutes.APPLICATION); expect(service['getApplicationHelpContent']).to.have.been.called.once; } @TestCase('should be able to get help content by route - non application') getHelpContentByRouteNotApplication (service: PageHelpModalService) { spy.on(service, 'getApplicationHelpContent'); service.getHelpContentByRoute(null); expect(service['getApplicationHelpContent']).to.not.have.been.called; } @TestCase('should be able to get application help content') getApplicationHelpContent (service: PageHelpModalService) { const { modalHeader, modalContent } = service.getApplicationHelpContent(); expect(modalHeader).to.be.equal('Get Started with Applications'); const expectedContent = 'Download applications, including the Activity Trail'; const includesContent = modalContent.includes(expectedContent); expect(includesContent).to.be.true; } }