|
2 | 2 | * Created by marc on 09.01.17. |
3 | 3 | */ |
4 | 4 | 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 | +} |
5 | 37 |
|
6 | 38 | 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 | + |
7 | 54 | 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']); |
10 | 102 | }); |
11 | 103 | }); |
0 commit comments