import { NavigationEnd, NavigationStart } from '@angular/router'; import { HelpContent } from '@features/platform-admin/help-content/help-content.typing'; import { Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { MockModule } from '@yourcause/common/testing'; import { expect, spy } from 'chai'; import { SidebarLink } from 'frontend-common/src/models/sidebar.model'; import { Subject } from 'rxjs'; import { AppShellService, defaultApplicantLink, defaultGMLink } from './app-shell.service'; @DescribeAngularService(AppShellService, { imports: [MockModule] }) export class AppShellServiceSpec implements Spec { @TestCase('should be able to toggle the sidebar') testSidebarToggle (service: AppShellService) { service.toggleSidebar(); const initialState = service.sidebarClosed; service.toggleSidebar(); expect(initialState).to.be.a('boolean'); expect(initialState).to.not.equal(service.sidebarClosed); } @TestCase('should be able to toggle the header buttons') testShowHeaderButtonsToggle (service: AppShellService) { service.setShowHeaderButtons(false); const initialState = service.get('showHeaderButtons'); service.setShowHeaderButtons(true); expect(service.get('showHeaderButtons')).to.be.a('boolean'); expect(initialState).to.not.equal(service.get('showHeaderButtons')); } @TestCase('should be able to toggle the header buttons') testSetActiveHelpLinkToggle (service: AppShellService) { service.setActiveHelpLink(''); const initialState = service.get('activeHelpLink'); expect(initialState).to.equal(''); service.setActiveHelpLink('true'); expect(service.get('activeHelpLink')).to.equal('true'); } @TestCase('should be able to set sidebar links') testSidebarLinks (service: AppShellService) { expect(service.sidebarLinks).to.deep.equal([]); const links: SidebarLink[] = [{ i18nKey: '', icon: '', label: '', sequence: '1' }]; service.setSidebarLinks(links); expect(service.sidebarLinks).to.equal(links); } @TestCase('should be able to set support links') testShouldSetSupportLinks (service: AppShellService) { service['helpContentService']['set']('contents', []); service.setSupportLinks(); expect(service.get('supportLinks')).to.have.lengthOf(2); expect(service.get('supportLinks')[0]).to.have.lengthOf(2); expect(service.get('supportLinks')[1]).to.have.lengthOf(2); const [[openHelp, whatsNew], [contactSupport, liveChat]] = service.get('supportLinks'); expect(openHelp.link).to.equal(defaultGMLink); expect(whatsNew.link).to.be.undefined; spy.on(service['supportService'], 'openSupportEmailModal', () => {}); contactSupport.onClick(); expect(service['supportService'].openSupportEmailModal).to.have.been.called.once; spy.on(service['supportService'], 'openLiveChat', () => {}); liveChat.onClick(); expect(service['supportService'].openLiveChat).to.have.been.called.once; spy.restore(); } @TestCase('should be able to set the applicant support link') testShouldSetApplicantSupportLink (service: AppShellService) { service['helpContentService']['set']('contents', []); service['portal']._routeBase = 'apply'; service.setSupportLinks(); const [[openHelp]] = service.get('supportLinks'); expect(openHelp.link).to.equal(defaultApplicantLink); } @TestCase('should be able to set the context-specific support link') testShouldSetContextSpecificSupportLink (service: AppShellService) { const applicableLink: HelpContent = { description: '', grantsConnectLink: 'applicable', helpContentLink: 'applicable', name: 'applicable', id: 0 }; service['locationService'].getGenericRoute = (val) => val || 'applicable/route'; service['helpContentService']['set']('contents', [applicableLink]); service.setSupportLinks(); const [[openHelp]] = service.get('supportLinks'); expect(openHelp.link).to.equal(`https://${applicableLink.helpContentLink}`); } @TestCase('should be able to not set the wrong context-specific support link') testShouldNotSetWrongContextSpecificSupportLink (service: AppShellService) { const applicableLink: HelpContent = { description: '', grantsConnectLink: 'not-applicable', helpContentLink: 'applicable', name: 'applicable', id: 0 }; service['locationService'].getGenericRoute = (val) => val || 'applicable/route'; service['helpContentService']['set']('contents', [applicableLink]); service.setSupportLinks(); const [[openHelp]] = service.get('supportLinks'); expect(openHelp.link).to.equal(defaultGMLink); } @TestCase('should be able to set the most context-specific support link') testShouldSetMostContextSpecificSupportLink (service: AppShellService) { const applicableLink: HelpContent = { description: '', grantsConnectLink: 'applicable', helpContentLink: 'applicable', name: 'applicable', id: 0 }; const mostApplicableLink: HelpContent = { description: '', grantsConnectLink: 'applicable/route', helpContentLink: 'https://most-applicable', name: 'applicable', id: 0 }; service['locationService'].getGenericRoute = (val) => val || 'applicable/route'; service['helpContentService']['set']('contents', [applicableLink, mostApplicableLink]); service.setSupportLinks(); const [[openHelp]] = service.get('supportLinks'); expect(openHelp.link).to.equal(mostApplicableLink.helpContentLink); } @TestCase('should be able to register router listener`') testShouldRegisterRouterListener (service: AppShellService) { spy.on(service, 'setSupportLinks', () => {}); const routerSub = new Subject(); service['router'] = { events: routerSub } as any; service.registerSupportLinkListener(); routerSub.next(new NavigationStart(0, '')); expect(service.setSupportLinks).not.to.have.been.called; routerSub.next(new NavigationEnd(0, '', '')); expect(service.setSupportLinks).to.have.been.called.once; } }