|
| 1 | +import { memoryLocationPlugin, UIRouter } from "@uirouter/core"; |
| 2 | +import { UIRouterModule } from "../../src/uiRouterNgModule"; |
| 3 | +import { inject, TestBed, waitForAsync } from "@angular/core/testing"; |
| 4 | +import { UIView } from "../../src/directives/uiView"; |
| 5 | +import { Ng2StateDeclaration } from "../../src/interface"; |
| 6 | + |
| 7 | +const fooState = { |
| 8 | + name: 'foo', |
| 9 | + url: '/foo', |
| 10 | + loadComponent: () => import("./foo/foo.component").then(result => result.FooComponent) |
| 11 | +}; |
| 12 | + |
| 13 | +const barState = { |
| 14 | + name: 'bar', |
| 15 | + url: '/bar', |
| 16 | + loadComponent: () => import("./bar/bar.component").then(result => result.BarComponent) |
| 17 | +}; |
| 18 | + |
| 19 | +function configFn(router: UIRouter) { |
| 20 | + router.plugin(memoryLocationPlugin); |
| 21 | +} |
| 22 | + |
| 23 | +describe('lazy loading', () => { |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + const routerModule = UIRouterModule.forRoot({ useHash: true, states: [], config: configFn }); |
| 27 | + TestBed.configureTestingModule({ |
| 28 | + declarations: [], |
| 29 | + imports: [routerModule] |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should lazy load a standalone component', waitForAsync( |
| 34 | + inject([UIRouter], ({ stateRegistry, stateService, globals }: UIRouter) => { |
| 35 | + stateRegistry.register(fooState); |
| 36 | + const fixture = TestBed.createComponent(UIView); |
| 37 | + fixture.detectChanges(); |
| 38 | + const names = stateRegistry.get().map(state => state.name).sort(); |
| 39 | + expect(names.length).toBe(2); |
| 40 | + expect(names).toEqual(['', 'foo']); |
| 41 | + |
| 42 | + stateService.go('foo') |
| 43 | + .then(() => { |
| 44 | + expect(globals.current.name).toBe('foo'); |
| 45 | + expect((globals.current as Ng2StateDeclaration).component).toBeTruthy(); |
| 46 | + const innerText = fixture.debugElement.nativeElement.textContent.replace(/\s+/g, ' ').trim(); |
| 47 | + expect(innerText).toBe('FOO'); |
| 48 | + }); |
| 49 | + }) |
| 50 | + )); |
| 51 | + |
| 52 | + it('should throw error if component is not standalone', waitForAsync( |
| 53 | + inject([UIRouter], ({ stateRegistry, stateService }: UIRouter) => { |
| 54 | + stateRegistry.register(barState); |
| 55 | + const fixture = TestBed.createComponent(UIView); |
| 56 | + fixture.detectChanges(); |
| 57 | + const names = stateRegistry.get().map(state => state.name).sort(); |
| 58 | + expect(names.length).toBe(2); |
| 59 | + expect(names).toEqual(['', 'bar']); |
| 60 | + |
| 61 | + const success = () => { throw Error('success not expected') }; |
| 62 | + const error = err => expect(err.detail.message).toBe("Is not a standalone component."); |
| 63 | + stateService.go('bar').then(success, error); |
| 64 | + }) |
| 65 | + )); |
| 66 | +}); |
0 commit comments