Skip to content

Commit 7164daa

Browse files
committed
Added test suite for loadComponent functionality.
1 parent 03f9beb commit 7164daa

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from "@angular/core";
2+
3+
@Component({
4+
selector: "bar",
5+
template: "BAR"
6+
})
7+
export class BarComponent {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Component } from "@angular/core";
2+
3+
@Component({
4+
selector: "foo",
5+
template: "FOO",
6+
standalone: true
7+
})
8+
export class FooComponent {}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)