-
Notifications
You must be signed in to change notification settings - Fork 98
/
wizard-navigation-bar.component.spec.ts
325 lines (254 loc) · 14.3 KB
/
wizard-navigation-bar.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* tslint:disable:no-unused-variable */
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {ViewChild, Component} from '@angular/core';
import {WizardNavigationBarComponent} from './wizard-navigation-bar.component';
import {WizardComponent} from './wizard.component';
import {WizardStepComponent} from './wizard-step.component';
import {GoToStepDirective} from '../directives/go-to-step.directive';
import {By} from '@angular/platform-browser';
import {OptionalStepDirective} from '../directives/optional-step.directive';
@Component({
selector: 'test-wizard',
template: `
<wizard>
<wizard-step title='Steptitle 1'>Step 1</wizard-step>
<wizard-step title='Steptitle 2' optionalStep>Step 2</wizard-step>
<wizard-step title='Steptitle 3'>Step 3</wizard-step>
</wizard>
`
})
class WizardTestComponent {
@ViewChild(WizardComponent)
public wizard: WizardComponent;
}
describe('WizardNavigationBarComponent', () => {
let wizardTest: WizardTestComponent;
let wizardTestFixture: ComponentFixture<WizardTestComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ WizardComponent, WizardStepComponent, WizardNavigationBarComponent,
WizardTestComponent, GoToStepDirective, OptionalStepDirective ]
}).compileComponents();
}));
beforeEach(() => {
wizardTestFixture = TestBed.createComponent(WizardTestComponent);
wizardTest = wizardTestFixture.componentInstance;
wizardTestFixture.detectChanges();
});
it('should create', () => {
expect(wizardTest).toBeTruthy();
expect(wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'))).toBeTruthy();
});
it('should create only one navigation bar', () => {
expect(wizardTest).toBeTruthy();
expect(wizardTestFixture.debugElement.queryAll(By.css('wizard-navigation-bar')).length).toBe(1);
});
it('should show the initial step correctly', () => {
const navBar = wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'));
// the first step is the current step
expect(navBar.query(By.css('li.current'))).toBeTruthy();
expect(navBar.query(By.css('li.current'))).toBe(navBar.query(By.css('li:first-child')));
expect(navBar.queryAll(By.css('li.current')).length).toBe(1);
// no step is currently marked as done
expect(navBar.query(By.css('li.done'))).toBeNull();
// no step is marked as editing
expect(navBar.query(By.css('li.editing'))).toBeNull();
// only the second step is marked as optional
expect(navBar.query(By.css('li.optional'))).toBeTruthy();
expect(navBar.query(By.css('li.optional'))).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.optional')).length).toBe(1);
// the second and third step is marked as default (neither done or current)
expect(navBar.query(By.css('li.default'))).toBeTruthy();
expect(navBar.queryAll(By.css('li.default'))[0]).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.default'))[1]).toBe(navBar.query(By.css('li:nth-child(3)')));
expect(navBar.queryAll(By.css('li.default')).length).toBe(2);
});
it('should show the second step correctly', () => {
const navBar = wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'));
// go to second step
wizardTest.wizard.goToNextStep();
wizardTestFixture.detectChanges();
// the second step is the current step
expect(navBar.query(By.css('li.current'))).toBeTruthy();
expect(navBar.query(By.css('li.current'))).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.current')).length).toBe(1);
// the first step should be marked as done
expect(navBar.query(By.css('li.done'))).toBeTruthy();
expect(navBar.query(By.css('li.done'))).toBe(navBar.query(By.css('li:nth-child(1)')));
expect(navBar.queryAll(By.css('li.done')).length).toBe(1);
// no step is marked as editing
expect(navBar.query(By.css('li.editing'))).toBeNull();
// no step is marked as optional, because the optional step is the current step
expect(navBar.query(By.css('li.optional'))).toBeNull();
// only the third step is marked as default (neither done or current)
expect(navBar.query(By.css('li.default'))).toBeTruthy();
expect(navBar.query(By.css('li.default'))).toBe(navBar.query(By.css('li:nth-child(3)')));
expect(navBar.queryAll(By.css('li.default')).length).toBe(1);
});
it('should show the third step correctly', () => {
const navBar = wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'));
// go to second step
wizardTest.wizard.goToNextStep();
// go to third step
wizardTest.wizard.goToNextStep();
wizardTestFixture.detectChanges();
// the third step is the current step
expect(navBar.query(By.css('li.current'))).toBeTruthy();
expect(navBar.query(By.css('li.current'))).toBe(navBar.query(By.css('li:nth-child(3)')));
expect(navBar.queryAll(By.css('li.current')).length).toBe(1);
// the first and second step should be marked as done
expect(navBar.query(By.css('li.done'))).toBeTruthy();
expect(navBar.queryAll(By.css('li.done'))[0]).toBe(navBar.query(By.css('li:nth-child(1)')));
expect(navBar.queryAll(By.css('li.done'))[1]).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.done')).length).toBe(2);
// no step is marked as editing
expect(navBar.query(By.css('li.editing'))).toBeNull();
// no step is marked as optional, because the optional step is a "done" step
expect(navBar.query(By.css('li.optional'))).toBeNull();
// no step is marked as default (neither done, current or optional)
expect(navBar.query(By.css('li.default'))).toBeNull();
});
it('should show the third step correctly, after jump from first to third step', () => {
const navBar = wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'));
// go to third step and jump over the optional second step
wizardTest.wizard.goToStep(2);
wizardTestFixture.detectChanges();
// the third step is the current step
expect(navBar.query(By.css('li.current'))).toBeTruthy();
expect(navBar.query(By.css('li.current'))).toBe(navBar.query(By.css('li:nth-child(3)')));
expect(navBar.queryAll(By.css('li.current')).length).toBe(1);
// the first step should be marked as done
expect(navBar.query(By.css('li.done'))).toBeTruthy();
expect(navBar.query(By.css('li.done'))).toBe(navBar.query(By.css('li:nth-child(1)')));
expect(navBar.queryAll(By.css('li.done')).length).toBe(1);
// no step is marked as editing
expect(navBar.query(By.css('li.editing'))).toBeNull();
// the second step is marked as optional, because we jumped over it
expect(navBar.query(By.css('li.optional'))).toBeTruthy();
expect(navBar.query(By.css('li.optional'))).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.optional')).length).toBe(1);
// the second step is marked as default (neither done nor current)
expect(navBar.query(By.css('li.default'))).toBeTruthy();
expect(navBar.query(By.css('li.default'))).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.default')).length).toBe(1);
});
it('should show the first step correctly, after going back from the second step to the first step', () => {
const navBar = wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'));
// go to second step
wizardTest.wizard.goToNextStep();
// go back to first step
wizardTest.wizard.goToPreviousStep();
wizardTestFixture.detectChanges();
// no step is the current step
expect(navBar.query(By.css('li.current'))).toBeNull();
// no step should be marked as done
expect(navBar.query(By.css('li.done'))).toBeNull();
// the first step is marked as editing
expect(navBar.query(By.css('li.editing'))).toBeTruthy();
expect(navBar.query(By.css('li.editing'))).toBe(navBar.query(By.css('li:nth-child(1)')));
expect(navBar.queryAll(By.css('li.editing')).length).toBe(1);
// the second step is marked as optional
expect(navBar.query(By.css('li.optional'))).toBeTruthy();
expect(navBar.query(By.css('li.optional'))).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.optional')).length).toBe(1);
// the second and third step is marked as default (neither done or current)
expect(navBar.query(By.css('li.default'))).toBeTruthy();
expect(navBar.queryAll(By.css('li.default'))[0]).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.default'))[1]).toBe(navBar.query(By.css('li:nth-child(3)')));
expect(navBar.queryAll(By.css('li.default')).length).toBe(2);
});
it('should show the first step correctly, after first jumping from the first to the third step ' +
'and then back from the third step to the first step', () => {
const navBar = wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'));
// go to third step, by jumping over the optional step
wizardTest.wizard.goToStep(2);
// go back to first step
wizardTest.wizard.goToStep(0);
wizardTestFixture.detectChanges();
// no step is the current step
expect(navBar.query(By.css('li.current'))).toBeNull();
// no step should be marked as done
expect(navBar.query(By.css('li.done'))).toBeNull();
// the first step is marked as editing
expect(navBar.query(By.css('li.editing'))).toBeTruthy();
expect(navBar.query(By.css('li.editing'))).toBe(navBar.query(By.css('li:nth-child(1)')));
expect(navBar.queryAll(By.css('li.editing')).length).toBe(1);
// the second step is marked as optional
expect(navBar.query(By.css('li.optional'))).toBeTruthy();
expect(navBar.query(By.css('li.optional'))).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.optional')).length).toBe(1);
// the second and third step is marked as default (neither done or current)
expect(navBar.query(By.css('li.default'))).toBeTruthy();
expect(navBar.queryAll(By.css('li.default'))[0]).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.default'))[1]).toBe(navBar.query(By.css('li:nth-child(3)')));
expect(navBar.queryAll(By.css('li.default')).length).toBe(2);
});
it('should show the second step correctly, after first jumping from the first to the third step ' +
'and then back from the third step to the second step', () => {
const navBar = wizardTestFixture.debugElement.query(By.css('wizard-navigation-bar'));
// go to third step, by jumping over the optional step
wizardTest.wizard.goToStep(2);
// go back to second step
wizardTest.wizard.goToPreviousStep();
wizardTestFixture.detectChanges();
// the second step is the current step
expect(navBar.query(By.css('li.current'))).toBeTruthy();
expect(navBar.query(By.css('li.current'))).toBe(navBar.query(By.css('li:nth-child(2)')));
expect(navBar.queryAll(By.css('li.current')).length).toBe(1);
// the first step should be marked as done
expect(navBar.query(By.css('li.done'))).toBeTruthy();
expect(navBar.query(By.css('li.done'))).toBe(navBar.query(By.css('li:nth-child(1)')));
expect(navBar.queryAll(By.css('li.done')).length).toBe(1);
// no step is marked as editing
expect(navBar.query(By.css('li.editing'))).toBeNull();
// no step is marked as optional, because the optional step is the current step
expect(navBar.query(By.css('li.optional'))).toBeNull();
// the third step is marked as default (neither done or current)
expect(navBar.query(By.css('li.default'))).toBeTruthy();
expect(navBar.query(By.css('li.default'))).toBe(navBar.query(By.css('li:nth-child(3)')));
expect(navBar.queryAll(By.css('li.default')).length).toBe(1);
});
it('should move back to the first step from the second step, after clicking on the corresponding link', () => {
const goToFirstStepLink = wizardTestFixture.debugElement.query(By.css('li:nth-child(1) a')).nativeElement;
expect(wizardTest.wizard.currentStepIndex).toBe(0);
// go to the second step
wizardTest.wizard.goToNextStep();
expect(wizardTest.wizard.currentStepIndex).toBe(1);
// go back to the first step
goToFirstStepLink.click();
wizardTestFixture.detectChanges();
expect(wizardTest.wizard.currentStepIndex).toBe(0);
});
it('should move back to the first step from the third step, after clicking on the corresponding link', () => {
const goToFirstStepLink = wizardTestFixture.debugElement.query(By.css('li:nth-child(1) a')).nativeElement;
expect(wizardTest.wizard.currentStepIndex).toBe(0);
// go to the second step
wizardTest.wizard.goToStep(2);
expect(wizardTest.wizard.currentStepIndex).toBe(2);
// go back to the first step
goToFirstStepLink.click();
wizardTestFixture.detectChanges();
expect(wizardTest.wizard.currentStepIndex).toBe(0);
});
it('should move back to the second step from the third step, after clicking on the corresponding link', () => {
const goToSecondStepLink = wizardTestFixture.debugElement.query(By.css('li:nth-child(2) a')).nativeElement;
expect(wizardTest.wizard.currentStepIndex).toBe(0);
// go to the second step
wizardTest.wizard.goToStep(2);
expect(wizardTest.wizard.currentStepIndex).toBe(2);
// go back to the first step
goToSecondStepLink.click();
wizardTestFixture.detectChanges();
expect(wizardTest.wizard.currentStepIndex).toBe(1);
});
it('should not move to the second step from the first step, after clicking on the corresponding link', () => {
const goToFirstStepLink = wizardTestFixture.debugElement.query(By.css('li:nth-child(1)'));
const goToSecondStepLink = wizardTestFixture.debugElement.query(By.css('li:nth-child(2)'));
const goToThirdStepLink = wizardTestFixture.debugElement.query(By.css('li:nth-child(3)'));
expect(wizardTest.wizard.currentStepIndex).toBe(0);
// links contain a class that is not clickable (contains "pointer-events: none;")
expect(goToFirstStepLink.classes.hasOwnProperty('current')).toBeTruthy('First step label is clickable');
expect(goToSecondStepLink.classes.hasOwnProperty('default')).toBeTruthy('Second step label is clickable');
expect(goToThirdStepLink.classes.hasOwnProperty('default')).toBeTruthy('Third step label is clickable');
});
});