import { Injectable } from '@angular/core'; import { Route } from '@angular/router'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { environment } from '@environment'; import { BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { GCPreloadingStrategy } from './router-preloader.service'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(GCPreloadingStrategy, { imports: [GCMockModule] }) export class GCPreloadingStrategySpec implements Spec { // a routing module off of the current portal e.g. my-workspace-routing-module in the GM portal testPresentRoute: Route = { path: 'present' }; // a routing module off of a different portal e.g. my-applications in the GM portal testOtherRoute: Route = { path: 'other' }; // a routing module off of a routing module e.g. applicant profile off of the application manager module testMissingRoute: Route = { path: 'missing' }; // current portal e.g. GM portal testCurrentRoot: Route = { path: environment.routeBase, children: [{ path: 'subroot', children: [this.testPresentRoute] }] }; // other portal e.g. applicant portal testOtherRoot: Route = { path: 'other', children: [{ path: 'subroot', children: [this.testOtherRoute] }] }; testAllRoutes: Route[] = [this.testCurrentRoot, this.testOtherRoot]; @BeforeEach() setUpRoutes (preloader: GCPreloadingStrategy) { preloader['routes'] = this.testAllRoutes; } @TestCase('should exist') testExists (preloader: GCPreloadingStrategy) { expect(preloader).to.exist; } @TestCase('should detect a present route') testShouldDetectAPresentRoute (preloader: GCPreloadingStrategy) { let called = false; function load () { called = true; } preloader.preload(this.testPresentRoute, load); expect(called).to.be.true; } @TestCase('should detect a missing route') testShouldDetectAMissingRoute (preloader: GCPreloadingStrategy) { let called = false; function load () { called = true; } preloader.preload(this.testMissingRoute, load); expect(called).to.be.true; } @TestCase('should detect an other route') testShouldDetectAnOtherRoute (preloader: GCPreloadingStrategy) { let called = false; function load () { called = true; } preloader.preload(this.testOtherRoute, load); expect(called).to.be.false; } @TestCase('should be able to detect the right root route') testShouldDetectRightRootRoute (preloader: GCPreloadingStrategy) { const result = preloader.getCurrentRouteRoot(); expect(result).to.equal(this.testCurrentRoot); } @TestCase('should be able to detect the right root route for a route') testShouldDetectRightRootRouteForRoute (preloader: GCPreloadingStrategy) { const result = preloader.getShouldLoad(this.testOtherRoute); expect(result).to.equal(this.testOtherRoot); } @TestCase('should be able to detect the right root route for a missing route') testShouldDetectRightRootRouteForMissing (preloader: GCPreloadingStrategy) { const result = preloader.getShouldLoad(this.testMissingRoute); expect(result).to.be.undefined; } }