-
Notifications
You must be signed in to change notification settings - Fork 98
/
wizard.component.spec.ts
351 lines (265 loc) · 12.9 KB
/
wizard.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { Component, ViewChild } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { ArchwizardModule } from '../archwizard.module';
import { WizardStep } from '../util/wizard-step.interface';
import { WizardComponent } from './wizard.component';
import { checkClasses } from '../util/test-utils';
@Component({
selector: 'aw-test-wizard',
template: `
<aw-wizard
[disableNavigationBar]="disableNavigationBar" [defaultStepIndex]="defaultStepIndex"
[awNavigationMode] [navigateForward]="navigateForward" [navigateBackward]="navigateBackward">
<aw-wizard-step stepTitle='Steptitle 1' *ngIf="showStep1">
Step 1
</aw-wizard-step>
<aw-wizard-step stepTitle='Steptitle 2'>
Step 2
</aw-wizard-step>
<aw-wizard-step stepTitle='Steptitle 3' *ngIf="showStep3">
Step 3
</aw-wizard-step>
</aw-wizard>
`
})
class WizardTestComponent {
public navigateForward = 'deny';
public navigateBackward = 'deny';
public disableNavigationBar = false;
public defaultStepIndex = 0;
public showStep1 = true;
public showStep3 = true;
@ViewChild(WizardComponent)
public wizard: WizardComponent;
}
describe('WizardComponent', () => {
let wizardTestFixture: ComponentFixture<WizardTestComponent>;
let wizardTest: WizardTestComponent;
let wizard: WizardComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [WizardTestComponent],
imports: [ArchwizardModule]
}).compileComponents();
}));
beforeEach(fakeAsync(() => {
wizardTestFixture = TestBed.createComponent(WizardTestComponent);
wizardTestFixture.detectChanges();
wizardTest = wizardTestFixture.componentInstance;
wizard = wizardTest.wizard;
// wait a tick to ensure that the initialization has been completed
tick();
wizardTestFixture.detectChanges();
}));
it('should create', () => {
expect(wizardTest).toBeTruthy();
expect(wizard).toBeTruthy();
});
it('should contain navigation bar at the correct position in default navBarLocation mode', () => {
const navBarEl = wizardTestFixture.debugElement.query(By.css('aw-wizard-navigation-bar'));
const wizardEl = wizardTestFixture.debugElement.query(By.css('aw-wizard'));
const wizardStepsDiv = wizardTestFixture.debugElement.query(By.css('div.wizard-steps'));
// check default: the navbar should be at the top of the wizard if no navBarLocation was set
expect(navBarEl).toBeTruthy();
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard')).children.length).toBe(2);
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :first-child')).name).toBe('aw-wizard-navigation-bar');
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :last-child')).name).toBe('div');
checkClasses(navBarEl.classes, ['horizontal', 'small']);
checkClasses(wizardEl.classes, ['horizontal']);
checkClasses(wizardStepsDiv.classes, ['wizard-steps', 'horizontal']);
});
it('should contain navigation bar at the correct position in top navBarLocation mode', () => {
wizard.navBarLocation = 'top';
wizardTestFixture.detectChanges();
const navBarEl = wizardTestFixture.debugElement.query(By.css('aw-wizard-navigation-bar'));
const wizardEl = wizardTestFixture.debugElement.query(By.css('aw-wizard'));
const wizardStepsDiv = wizardTestFixture.debugElement.query(By.css('div.wizard-steps'));
// check default: the navbar should be at the top of the wizard if no navBarLocation was set
expect(navBarEl).toBeTruthy();
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard')).children.length).toBe(2);
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :first-child')).name).toBe('aw-wizard-navigation-bar');
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :last-child')).name).toBe('div');
checkClasses(navBarEl.classes, ['horizontal', 'small']);
checkClasses(wizardEl.classes, ['horizontal']);
checkClasses(wizardStepsDiv.classes, ['wizard-steps', 'horizontal']);
});
it('should contain navigation bar at the correct position in left navBarLocation mode', () => {
wizard.navBarLocation = 'left';
wizardTestFixture.detectChanges();
const navBarEl = wizardTestFixture.debugElement.query(By.css('aw-wizard-navigation-bar'));
const wizardEl = wizardTestFixture.debugElement.query(By.css('aw-wizard'));
const wizardStepsDiv = wizardTestFixture.debugElement.query(By.css('div.wizard-steps'));
// check default: the navbar should be at the top of the wizard if no navBarLocation was set
expect(navBarEl).toBeTruthy();
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard')).children.length).toBe(2);
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :first-child')).name).toBe('aw-wizard-navigation-bar');
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :last-child')).name).toBe('div');
checkClasses(navBarEl.classes, ['vertical', 'small']);
checkClasses(wizardEl.classes, ['vertical']);
checkClasses(wizardStepsDiv.classes, ['wizard-steps', 'vertical']);
});
it('should contain navigation bar at the correct position in bottom navBarLocation mode', () => {
wizard.navBarLocation = 'bottom';
wizardTestFixture.detectChanges();
const navBarEl = wizardTestFixture.debugElement.query(By.css('aw-wizard-navigation-bar'));
const wizardEl = wizardTestFixture.debugElement.query(By.css('aw-wizard'));
const wizardStepsDiv = wizardTestFixture.debugElement.query(By.css('div.wizard-steps'));
// check default: the navbar should be at the top of the wizard if no navBarLocation was set
expect(navBarEl).toBeTruthy();
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard')).children.length).toBe(2);
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :first-child')).name).toBe('div');
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :last-child')).name).toBe('aw-wizard-navigation-bar');
checkClasses(navBarEl.classes, ['horizontal', 'small']);
checkClasses(wizardEl.classes, ['horizontal']);
checkClasses(wizardStepsDiv.classes, ['wizard-steps', 'horizontal']);
});
it('should contain navigation bar at the correct position in right navBarLocation mode', () => {
wizard.navBarLocation = 'right';
wizardTestFixture.detectChanges();
const navBarEl = wizardTestFixture.debugElement.query(By.css('aw-wizard-navigation-bar'));
const wizardEl = wizardTestFixture.debugElement.query(By.css('aw-wizard'));
const wizardStepsDiv = wizardTestFixture.debugElement.query(By.css('div.wizard-steps'));
// check default: the navbar should be at the top of the wizard if no navBarLocation was set
expect(navBarEl).toBeTruthy();
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard')).children.length).toBe(2);
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :first-child')).name).toBe('div');
expect(wizardTestFixture.debugElement.query(By.css('aw-wizard > :last-child')).name).toBe('aw-wizard-navigation-bar');
checkClasses(navBarEl.classes, ['vertical', 'small']);
checkClasses(wizardEl.classes, ['vertical']);
checkClasses(wizardStepsDiv.classes, ['wizard-steps', 'vertical']);
});
it('should change the navigation mode correctly during runtime', () => {
const oldNavigation = wizard.navigation;
wizardTest.navigateForward = 'allow';
wizardTest.navigateBackward = 'allow';
wizardTestFixture.detectChanges();
expect(wizard.navigation).not.toBe(oldNavigation);
});
it('should change disableNavigationBar correctly during runtime', () => {
expect(wizard.disableNavigationBar).toBe(false);
wizardTest.disableNavigationBar = true;
wizardTestFixture.detectChanges();
expect(wizard.disableNavigationBar).toBe(true);
});
it('should change defaultStepIndex correctly during runtime', () => {
expect(wizard.defaultStepIndex).toBe(0);
wizardTest.defaultStepIndex = 1;
wizardTestFixture.detectChanges();
expect(wizard.defaultStepIndex).toBe(1);
});
it('should react on a previous step removal and insertion correctly', fakeAsync(() => {
wizard.goToStep(1);
tick();
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(1);
expect(wizard.wizardSteps.length).toBe(3);
wizardTest.showStep1 = false;
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(0);
expect(wizard.wizardSteps.length).toBe(2);
wizardTest.showStep1 = true;
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(1);
expect(wizard.wizardSteps.length).toBe(3);
}));
it('should react on a later step removal and insertion correctly', fakeAsync(() => {
wizard.goToStep(1);
tick();
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(1);
expect(wizard.wizardSteps.length).toBe(3);
wizardTest.showStep3 = false;
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(1);
expect(wizard.wizardSteps.length).toBe(2);
wizardTest.showStep3 = true;
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(1);
expect(wizard.wizardSteps.length).toBe(3);
}));
it('should react on a combined removal and insertion of previous and later steps correctly', fakeAsync(() => {
wizard.goToStep(1);
tick();
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(1);
expect(wizard.wizardSteps.length).toBe(3);
wizardTest.showStep1 = false;
wizardTest.showStep3 = false;
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(0);
expect(wizard.wizardSteps.length).toBe(1);
wizardTest.showStep1 = true;
wizardTest.showStep3 = true;
wizardTestFixture.detectChanges();
expect(wizard.currentStepIndex).toBe(1);
expect(wizard.wizardSteps.length).toBe(3);
}));
it('should have steps', () => {
expect(wizard.wizardSteps.length).toBe(3);
});
it('should start at first step', () => {
expect(wizard.currentStepIndex).toBe(0);
expect(wizard.currentStep.stepTitle).toBe('Steptitle 1');
checkWizardSteps(wizard.wizardSteps, 0);
});
it('should return correct step at index', () => {
expect(() => wizard.getStepAtIndex(-1))
.toThrow(new Error(`Expected a known step, but got stepIndex: -1.`));
expect(wizard.getStepAtIndex(0).stepTitle).toBe('Steptitle 1');
expect(wizard.getStepAtIndex(1).stepTitle).toBe('Steptitle 2');
expect(wizard.getStepAtIndex(2).stepTitle).toBe('Steptitle 3');
// Check that the first wizard step is the only selected one
checkWizardSteps(wizard.wizardSteps, 0);
expect(() => wizard.getStepAtIndex(3))
.toThrow(new Error(`Expected a known step, but got stepIndex: 3.`));
});
it('should return correct index at step', () => {
expect(wizard.getIndexOfStep(wizard.getStepAtIndex(0))).toBe(0);
expect(wizard.getIndexOfStep(wizard.getStepAtIndex(1))).toBe(1);
expect(wizard.getIndexOfStep(wizard.getStepAtIndex(2))).toBe(2);
});
it('should have next step', () => {
expect(wizard.hasNextStep()).toBe(true);
wizard.currentStepIndex++;
expect(wizard.hasNextStep()).toBe(true);
wizard.currentStepIndex++;
expect(wizard.hasNextStep()).toBe(false);
});
it('should have previous step', () => {
expect(wizard.hasPreviousStep()).toBe(false);
wizard.currentStepIndex++;
expect(wizard.hasPreviousStep()).toBe(true);
wizard.currentStepIndex++;
expect(wizard.hasPreviousStep()).toBe(true);
});
it('should be last step', () => {
expect(wizard.isLastStep()).toBe(false);
wizard.currentStepIndex++;
expect(wizard.isLastStep()).toBe(false);
wizard.currentStepIndex++;
expect(wizard.isLastStep()).toBe(true);
});
it('should return null when staying in an incorrect step', () => {
wizard.currentStepIndex = -1;
expect(wizard.currentStep).toBeNull();
});
});
function checkWizardSteps(steps: WizardStep[], selectedStepIndex: number) {
steps.forEach((step, index) => {
// Only the selected step should be selected
if (index === selectedStepIndex) {
expect(step.selected).toBe(true, `the selected wizard step index ${index} is not selected`);
} else {
expect(step.selected).toBe(false, `the not selected wizard step index ${index} is selected`);
}
// All steps before the selected step need to be completed
if (index < selectedStepIndex) {
expect(step.completed).toBe(true,
`the wizard step ${index} is not completed while the currently selected step index is ${selectedStepIndex}`);
} else if (index > selectedStepIndex) {
expect(step.completed).toBe(false,
`the wizard step ${index} is completed while the currently selected step index is ${selectedStepIndex}`);
}
});
}