import { Injectable } from '@angular/core'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { ProgramTypes } from '@core/typings/program.typing'; import { BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { LocationService, RouteSnapshot } from './location.service'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(LocationService, { imports: [ GCMockModule ] }) export class LocationServiceSpec implements Spec { currentUrl = 'https://yourcausegrantsqa.com'; @BeforeEach() beforeEach () { Object.defineProperty(window, 'location', { get: () => { return new URL(this.currentUrl); }, configurable: false }); } @TestCase('should be able to get relative url with standard base') getRelativeUrlStandard (service: LocationService) { const base = 'https://yourcausegrantsqa.com'; this.currentUrl = base; const path = '/management/nomination-view/1/form/no-form'; const url = service.getRelativeUrl(path); expect(url).to.be.equal(`${base}${path}`); } @TestCase('should be able to get relative url with subdomain') getRelativeUrlSubdomain (service: LocationService) { const base = 'https://subdomain.yourcausegrantsqa.com'; this.currentUrl = base; const path = '/management/nomination-view/1/form/no-form'; const url = service.getRelativeUrl(path); expect(url).to.be.equal(`${base}${path}`); } @TestCase('should be able to get relative url with local') getRelativeUrlLocal (service: LocationService) { const base = 'https://localhost:51851'; this.currentUrl = base; const path = '/management/nomination-view/1/form/no-form'; const url = service.getRelativeUrl(path); expect(url).to.be.equal(`${base}${path}`); } @TestCase('should be able to get relative url with search') getRelativeUrlSearch (service: LocationService) { const base = 'https://yourcausegrantsqa.com'; this.currentUrl = base; const path = '/management/nomination-view/1/form/no-form'; const search = 'id=1'; const url = service.getRelativeUrl(path, search); expect(url).to.be.equal(`${base}${path}?${search}`); } @TestCase('should be able to get relative url with hash') getRelativeUrlHash (service: LocationService) { const base = 'https://yourcausegrantsqa.com'; this.currentUrl = base; const path = '/management/nomination-view/1/form/no-form'; const hash = 'hash'; const url = service.getRelativeUrl(path, '', hash); expect(url).to.be.equal(`${base}${path}#${hash}`); } @TestCase('should be able to map out a guid from a route') testMapGuid (service: LocationService) { const url1 = '/path/to/4e5df174-908d-4ecf-a1eb-93b753ed59e1'; const url2 = '/path/to/4e5df174-908d-4ecf-a1eb-93b753ed59e1/something'; const url3 = '/path/to/4e5df174-908d-4ecf-a1eb-93b753ed59e1/something/a5d65ee6-2432-4ac2-bf19-5095130639d3'; const expectedUrl1 = '/path/to/:id'; const expectedUrl2 = '/path/to/:id/something'; const expectedUrl3 = '/path/to/:id/something/:id'; expect(service.getGenericRoute(url1)).to.equal(expectedUrl1); expect(service.getGenericRoute(url2)).to.equal(expectedUrl2); expect(service.getGenericRoute(url3)).to.equal(expectedUrl3); } @TestCase('should be able to map out an id from a route') testMapID (service: LocationService) { const url1 = '/path/to/123'; const url2 = '/path/to/123/something'; const url3 = '/path/to/123/something/456'; const expectedUrl1 = '/path/to/:id'; const expectedUrl2 = '/path/to/:id/something'; const expectedUrl3 = '/path/to/:id/something/:id'; expect(service.getGenericRoute(url1)).to.equal(expectedUrl1); expect(service.getGenericRoute(url2)).to.equal(expectedUrl2); expect(service.getGenericRoute(url3)).to.equal(expectedUrl3); } @TestCase('should be able to map out an id and a guid from a route') testMapIDandGUID (service: LocationService) { const url = '/path/to/4e5df174-908d-4ecf-a1eb-93b753ed59e1/something/456'; const expectedUrl = '/path/to/:id/something/:id'; expect(service.getGenericRoute(url)).to.equal(expectedUrl); } @TestCase('should be able to get the correct page title') testShouldGetCorrectPageTitle (service: LocationService) { const pageName = 'TEST'; const mockSnapshot: RouteSnapshot = { data: {}, children: [{ children: [{ children: [], data: {} }], data: { pageName } }] }; const foundPageName = service.recurseGetTitle(mockSnapshot); expect(foundPageName).to.equal(pageName); } @TestCase('should be able to get the correct page title from breadcrumb') testShouldGetCorrectPageTitleFromBreadcrumb (service: LocationService) { const breadcrumbLabel = 'TEST'; const mockSnapshot: RouteSnapshot = { data: {}, children: [{ children: [{ children: [], data: {} }], data: { breadcrumbLabel } }] }; const foundPageName = service.recurseGetTitle(mockSnapshot); expect(foundPageName).to.equal(breadcrumbLabel); } @TestCase('should be able to get url to navigate to app') async getNavigateToAppUrl (service: LocationService) { const appId = 134; const row = { application: { id: appId }, grantProgram: { programType: ProgramTypes.NOMINATION } } as any; const url = service.getNavigateToAppUrl( row, 'application' ); expect(url).to.contain(`/management/nomination-view/${appId}/form/no-form`); } }