Skip to content

Commit 6abd28f

Browse files
committed
- added string parameter support for the canGoToStep and goToStep methods to support string inputs through input field of directives
- added tests for the goToStep directive
1 parent ea13cd7 commit 6abd28f

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

src/components/components/wizard.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Component, ContentChildren, QueryList, AfterContentInit, Input} from '@a
22
import {WizardStepComponent} from './wizard-step.component';
33
import {isNumber} from 'util';
44
import {MovingDirection} from '../util/MovingDirection';
5+
import {isString} from "util";
56

67
@Component({
78
selector: 'wizard',
@@ -112,13 +113,15 @@ export class WizardComponent implements AfterContentInit {
112113
return this.hasStep(nextStepIndex) && this.canGoToStep(nextStepIndex);
113114
}
114115

115-
canGoToStep(inputStep: WizardStepComponent | number): boolean {
116+
canGoToStep(inputStep: WizardStepComponent | number | string): boolean {
116117
let nextStepIndex: number;
117118

118119
if (inputStep instanceof WizardStepComponent) {
119120
nextStepIndex = this.getIndexOfStep(inputStep);
120121
} else if (isNumber(inputStep)) {
121122
nextStepIndex = inputStep as number;
123+
} else if (isString(inputStep)) {
124+
nextStepIndex = parseInt(inputStep as string);
122125
}
123126

124127
let result: boolean = this.hasStep(nextStepIndex);
@@ -133,7 +136,7 @@ export class WizardComponent implements AfterContentInit {
133136
return result;
134137
}
135138

136-
goToStep(inputStep: WizardStepComponent | number) {
139+
goToStep(inputStep: WizardStepComponent | number | string): void {
137140
let nextStepIndex: number;
138141
let nextStep: WizardStepComponent;
139142

@@ -142,7 +145,10 @@ export class WizardComponent implements AfterContentInit {
142145
nextStep = inputStep;
143146
} else if (isNumber(inputStep)) {
144147
nextStepIndex = inputStep as number;
145-
nextStep = this.getStepAtIndex(inputStep);
148+
nextStep = this.getStepAtIndex(nextStepIndex);
149+
} else if (isString(inputStep)) {
150+
nextStepIndex = parseInt(inputStep as string);
151+
nextStep = this.getStepAtIndex(nextStepIndex);
146152
}
147153

148154
// In which direction is a step transition done?

src/components/directives/go-to-step.directive.spec.ts

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,102 @@
22
* Created by marc on 09.01.17.
33
*/
44
import {GoToStepDirective} from './go-to-step.directive';
5+
import {Component, ViewChild} from "@angular/core";
6+
import {WizardComponent} from "../components/wizard.component";
7+
import {ComponentFixture, async, TestBed} from "@angular/core/testing";
8+
import {WizardStepComponent} from "../components/wizard-step.component";
9+
import {WizardNavigationBarComponent} from "../components/wizard-navigation-bar.component";
10+
import {By} from "@angular/platform-browser";
11+
12+
@Component({
13+
selector: 'test-wizard',
14+
template: `
15+
<wizard>
16+
<wizard-step title='Steptitle 1'>
17+
Step 1
18+
<button type="button" goToStep="1" (finalize)="finalizeStep(1)">Go to second step</button>
19+
</wizard-step>
20+
<wizard-step title='Steptitle 2'>
21+
Step 2
22+
<button type="button" goToStep="0" (finalize)="finalizeStep(2)">Go to first step</button>
23+
</wizard-step>
24+
</wizard>
25+
`
26+
})
27+
class WizardTestComponent {
28+
@ViewChild(WizardComponent)
29+
public wizard: WizardComponent;
30+
31+
public eventLog: Array<string> = new Array<string>();
32+
33+
finalizeStep(stepIndex: number): void {
34+
this.eventLog.push(`finalize ${stepIndex}`);
35+
}
36+
}
537

638
describe('GoToStepDirective', () => {
39+
let wizardTest: WizardTestComponent;
40+
let wizardTestFixture: ComponentFixture<WizardTestComponent>;
41+
42+
beforeEach(async(() => {
43+
TestBed.configureTestingModule({
44+
declarations: [WizardComponent, WizardStepComponent, WizardNavigationBarComponent, WizardTestComponent, GoToStepDirective]
45+
}).compileComponents();
46+
}));
47+
48+
beforeEach(() => {
49+
wizardTestFixture = TestBed.createComponent(WizardTestComponent);
50+
wizardTest = wizardTestFixture.componentInstance;
51+
wizardTestFixture.detectChanges();
52+
});
53+
754
it('should create an instance', () => {
8-
let directive = new GoToStepDirective(null);
9-
expect(directive).toBeTruthy();
55+
expect(wizardTestFixture.debugElement.query(By.css('wizard-step[title="Steptitle 1"] > button[goToStep]'))).toBeTruthy();
56+
expect(wizardTestFixture.debugElement.query(By.css('wizard-step[title="Steptitle 2"] > button[goToStep]'))).toBeTruthy();
57+
expect(wizardTestFixture.debugElement.queryAll(By.css('wizard-step > button[goToStep]')).length).toBe(2);
58+
});
59+
60+
it('should move to step correctly', () => {
61+
const firstStepGoToButton = wizardTestFixture.debugElement.query(By.css('wizard-step[title="Steptitle 1"] > button[goToStep]')).nativeElement;
62+
const secondStepGoToButton = wizardTestFixture.debugElement.query(By.css('wizard-step[title="Steptitle 2"] > button[goToStep]')).nativeElement;
63+
64+
expect(wizardTest.wizard.currentStepIndex).toBe(0);
65+
expect(wizardTestFixture.debugElement.query(By.css('wizard > wizard-step .current'))).toBe(wizardTestFixture.debugElement.query(By.css('wizard > wizard-step:first-child')));
66+
67+
// click button
68+
firstStepGoToButton.click();
69+
wizardTestFixture.detectChanges();
70+
71+
expect(wizardTest.wizard.currentStepIndex).toBe(1);
72+
expect(wizardTestFixture.debugElement.query(By.css('wizard > wizard-step .current'))).toBe(wizardTestFixture.debugElement.query(By.css('wizard > wizard-step :nth-child(2)')));
73+
74+
// click button
75+
secondStepGoToButton.click();
76+
wizardTestFixture.detectChanges();
77+
78+
expect(wizardTest.wizard.currentStepIndex).toBe(0);
79+
expect(wizardTestFixture.debugElement.query(By.css('wizard > wizard-step .current'))).toBe(wizardTestFixture.debugElement.query(By.css('wizard > wizard-step:first-child')));
80+
});
81+
82+
it('should finalize step correctly', () => {
83+
const firstStepGoToButton = wizardTestFixture.debugElement.query(By.css('wizard-step[title="Steptitle 1"] > button[goToStep]')).nativeElement;
84+
const secondStepGoToButton = wizardTestFixture.debugElement.query(By.css('wizard-step[title="Steptitle 2"] > button[goToStep]')).nativeElement;
85+
86+
expect(wizardTest.wizard.currentStepIndex).toBe(0);
87+
expect(wizardTest.eventLog).toEqual([]);
88+
89+
// click button
90+
firstStepGoToButton.click();
91+
wizardTestFixture.detectChanges();
92+
93+
expect(wizardTest.wizard.currentStepIndex).toBe(1);
94+
expect(wizardTest.eventLog).toEqual(['finalize 1']);
95+
96+
// click button
97+
secondStepGoToButton.click();
98+
wizardTestFixture.detectChanges();
99+
100+
expect(wizardTest.wizard.currentStepIndex).toBe(0);
101+
expect(wizardTest.eventLog).toEqual(['finalize 1', 'finalize 2']);
10102
});
11103
});

0 commit comments

Comments
 (0)