Skip to content

Commit faa9bcf

Browse files
test(views): Added views builder unit tests
1 parent 5c67a47 commit faa9bcf

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import { Component } from '@angular/core';
2+
import { ng2ViewsBuilder } from '../../src/statebuilders/views';
3+
import { StateObject, StateRegistry, UIRouter } from '@uirouter/core';
4+
5+
6+
describe('views statebuilder', () => {
7+
let router: UIRouter;
8+
let root: StateObject;
9+
@Component({ template: '<h1>foo</h1>' }) class Cmp {}
10+
@Component({ template: '<h1>foo2</h1>' }) class Cmp2 {}
11+
12+
beforeEach(() => {
13+
router = new UIRouter();
14+
root = router.stateRegistry.root();
15+
});
16+
17+
it('should process the default view found on the state declaration', () => {
18+
const state = {
19+
parent: root,
20+
component: Cmp,
21+
name: 'statename',
22+
} as any as StateObject;
23+
24+
const expectedViews = {
25+
$default: {
26+
$type: 'ng2',
27+
$name: '$default',
28+
$uiViewName: '$default',
29+
$uiViewContextAnchor: root.name,
30+
$context: state,
31+
component: Cmp,
32+
}
33+
};
34+
35+
expect(ng2ViewsBuilder(state)).toEqual(expectedViews)
36+
});
37+
38+
it('should process the default view found in the views declaration', () => {
39+
const state = {
40+
parent: root,
41+
name: 'statename',
42+
views: {
43+
$default: { component: Cmp },
44+
}
45+
} as any as StateObject;
46+
47+
const expectedViews = {
48+
$default: {
49+
$type: 'ng2',
50+
$name: '$default',
51+
$uiViewName: '$default',
52+
$uiViewContextAnchor: root.name,
53+
$context: state,
54+
component: Cmp,
55+
}
56+
};
57+
58+
let actual = ng2ViewsBuilder(state);
59+
expect(actual).toEqual(expectedViews)
60+
});
61+
62+
it('should prefer the default view found in the views declaration', () => {
63+
const state = {
64+
parent: root,
65+
name: 'statename',
66+
cmp: Cmp2,
67+
views: {
68+
$default: { component: Cmp },
69+
}
70+
} as any as StateObject;
71+
72+
const expectedViews = {
73+
$default: {
74+
$type: 'ng2',
75+
$name: '$default',
76+
$uiViewName: '$default',
77+
$uiViewContextAnchor: root.name,
78+
$context: state,
79+
component: Cmp,
80+
}
81+
};
82+
83+
let actual = ng2ViewsBuilder(state);
84+
expect(actual).toEqual(expectedViews)
85+
});
86+
87+
it('should process other named views found in the views declaration', () => {
88+
const state = {
89+
parent: root,
90+
name: 'statename',
91+
views: {
92+
'header': { component: Cmp },
93+
'footer': { component: Cmp2 },
94+
}
95+
} as any as StateObject;
96+
97+
const expectedViews = {
98+
header: {
99+
$type: 'ng2',
100+
$name: 'header',
101+
$uiViewName: 'header',
102+
$uiViewContextAnchor: "",
103+
$context: state,
104+
component: Cmp,
105+
},
106+
footer: {
107+
$type: 'ng2',
108+
$name: 'footer',
109+
$uiViewName: 'footer',
110+
$uiViewContextAnchor: "",
111+
$context: state,
112+
component: Cmp2,
113+
}
114+
};
115+
116+
let actual = ng2ViewsBuilder(state);
117+
expect(actual).toEqual(expectedViews)
118+
});
119+
120+
it('should allow shorthand { name: ComponentClass } in views block', () => {
121+
const state = {
122+
parent: root,
123+
name: 'statename',
124+
views: {
125+
'header': Cmp,
126+
'footer': Cmp2,
127+
}
128+
} as any as StateObject;
129+
130+
const expectedViews = {
131+
header: {
132+
$type: 'ng2',
133+
$name: 'header',
134+
$uiViewName: 'header',
135+
$uiViewContextAnchor: "",
136+
$context: state,
137+
component: Cmp,
138+
},
139+
footer: {
140+
$type: 'ng2',
141+
$name: 'footer',
142+
$uiViewName: 'footer',
143+
$uiViewContextAnchor: "",
144+
$context: state,
145+
component: Cmp2,
146+
}
147+
};
148+
149+
let actual = ng2ViewsBuilder(state);
150+
expect(actual).toEqual(expectedViews)
151+
});
152+
});

0 commit comments

Comments
 (0)