|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import * as Architect from '@angular-devkit/architect'; |
| 10 | +import { NullLogger } from '@angular-devkit/core/src/logger'; |
9 | 11 | import * as fs from 'fs'; |
| 12 | +import * as guessParser from 'guess-parser'; |
| 13 | +import { RoutingModule } from 'guess-parser/dist/common/interfaces'; |
| 14 | +import 'jasmine'; |
| 15 | + |
| 16 | +import { PrerenderBuilderOptions } from './models'; |
10 | 17 | import { getRoutes, shardArray } from './utils'; |
11 | 18 |
|
| 19 | + |
12 | 20 | describe('Prerender Builder Utils', () => { |
13 | 21 | describe('#getRoutes', () => { |
14 | | - const WORKSPACE_ROOT = '/path/to/angular/json'; |
15 | 22 | const ROUTES_FILE = './routes.txt'; |
16 | 23 | const ROUTES_FILE_CONTENT = ['/route1', '/route1', '/route2', '/route3'].join('\n'); |
17 | 24 | const ROUTES = ['/route3', '/route3', '/route4']; |
| 25 | + const GUESSED_ROUTES = [{ path: '/route4' }, { path: '/route5' }, { path: '/**' }, { path: '/user/:id' }]; |
| 26 | + |
| 27 | + const CONTEXT = { |
| 28 | + workspaceRoot: '/path/to/angular/json', |
| 29 | + getTargetOptions: () => ({ tsConfig: 'tsconfig.app.json' }), |
| 30 | + logger: new NullLogger(), |
| 31 | + } as unknown as Architect.BuilderContext; |
| 32 | + |
| 33 | + let parseAngularRoutesSpy: jasmine.Spy; |
| 34 | + let loggerErrorSpy: jasmine.Spy; |
18 | 35 |
|
19 | 36 | beforeEach(() => { |
20 | 37 | spyOn(fs, 'readFileSync').and.returnValue(ROUTES_FILE_CONTENT); |
| 38 | + spyOn(Architect, 'targetFromTargetString').and.returnValue({} as Architect.Target); |
| 39 | + parseAngularRoutesSpy = spyOn(guessParser, 'parseAngularRoutes') |
| 40 | + .and.returnValue(GUESSED_ROUTES as RoutingModule[]); |
| 41 | + loggerErrorSpy = spyOn(CONTEXT.logger, 'error'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Should return the union of the routes from routes, routesFile, and the extracted routes without any parameterized routes', async () => { |
| 45 | + const options = { |
| 46 | + routes: ROUTES, |
| 47 | + routesFile: ROUTES_FILE, |
| 48 | + guessRoutes: true, |
| 49 | + } as unknown as PrerenderBuilderOptions; |
| 50 | + const routes = await getRoutes(options, CONTEXT); |
| 51 | + expect(routes).toContain('/route1'); |
| 52 | + expect(routes).toContain('/route2'); |
| 53 | + expect(routes).toContain('/route3'); |
| 54 | + expect(routes).toContain('/route4'); |
| 55 | + expect(routes).toContain('/route5'); |
21 | 56 | }); |
22 | 57 |
|
23 | | - it('Should return the deduped union of options.routes and options.routesFile - routes and routesFile defined', () => { |
24 | | - const routes = getRoutes(WORKSPACE_ROOT, ROUTES_FILE, ROUTES); |
25 | | - expect(routes).toEqual(['/route1', '/route2', '/route3', '/route4']); |
| 58 | + it('Should return only the given routes', async () => { |
| 59 | + const options = { routes: ROUTES } as PrerenderBuilderOptions; |
| 60 | + const routes = await getRoutes(options, CONTEXT); |
| 61 | + expect(routes).toContain('/route3'); |
| 62 | + expect(routes).toContain('/route4'); |
26 | 63 | }); |
27 | 64 |
|
28 | | - it('Should return the deduped union of options.routes and options.routesFile - only routes defined', () => { |
29 | | - const routes = getRoutes(WORKSPACE_ROOT, undefined, ROUTES); |
30 | | - expect(routes).toEqual(['/route3', '/route4']); |
| 65 | + it('Should return the routes from the routesFile', async () => { |
| 66 | + const options = { routesFile: ROUTES_FILE } as PrerenderBuilderOptions; |
| 67 | + const routes = await getRoutes(options, CONTEXT); |
| 68 | + expect(routes).toContain('/route1'); |
| 69 | + expect(routes).toContain('/route2'); |
| 70 | + expect(routes).toContain('/route3'); |
31 | 71 | }); |
32 | 72 |
|
33 | | - it('Should return the deduped union of options.routes and options.routesFile - only routes file defined', () => { |
34 | | - const routes = getRoutes(WORKSPACE_ROOT, ROUTES_FILE, undefined); |
35 | | - expect(routes).toEqual(['/route1', '/route2', '/route3']); |
| 73 | + it('Should catch errors thrown by parseAngularRoutes', async () => { |
| 74 | + const options = { routes: ROUTES, guessRoutes: true } as PrerenderBuilderOptions; |
| 75 | + parseAngularRoutesSpy.and.throwError('Test Error'); |
| 76 | + const routes = await getRoutes(options, CONTEXT); |
| 77 | + expect(routes).toContain('/route3'); |
| 78 | + expect(routes).toContain('/route4'); |
| 79 | + expect(loggerErrorSpy).toHaveBeenCalled(); |
36 | 80 | }); |
37 | 81 | }); |
38 | 82 |
|
|
0 commit comments