|
| 1 | +import {OptionalStepDirective} from './optional-step.directive'; |
| 2 | +import {Component} from '@angular/core'; |
| 3 | +import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; |
| 4 | +import {By} from '@angular/platform-browser'; |
| 5 | +import {WizardModule} from '../wizard.module'; |
| 6 | +import {WizardState} from '../navigation/wizard-state.model'; |
| 7 | +import {NavigationMode} from '../navigation/navigation-mode.interface'; |
| 8 | +import {ResetWizardDirective} from './reset-wizard.directive'; |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: 'test-wizard', |
| 12 | + template: ` |
| 13 | + <wizard> |
| 14 | + <wizard-step stepTitle='Steptitle 1'> |
| 15 | + Step 1 |
| 16 | + </wizard-step> |
| 17 | + <wizard-step stepTitle='Steptitle 2'> |
| 18 | + Step 2 |
| 19 | + <button type="button" resetWizard>Reset (normal)</button> |
| 20 | + <button type="button" resetWizard (finalize)='cleanup()'>Reset (cleanup)</button> |
| 21 | + <button type="button" reset (finalize)='cleanup()'>Reset (cleanup short)</button> |
| 22 | + </wizard-step> |
| 23 | + </wizard> |
| 24 | + ` |
| 25 | +}) |
| 26 | +class WizardTestComponent { |
| 27 | + public eventLog: Array<string> = []; |
| 28 | + |
| 29 | + public cleanup(): void { |
| 30 | + this.eventLog.push('Cleanup done!'); |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +describe('ResetWizardDirective', () => { |
| 35 | + let wizardTest: WizardTestComponent; |
| 36 | + let wizardTestFixture: ComponentFixture<WizardTestComponent>; |
| 37 | + |
| 38 | + let wizardState: WizardState; |
| 39 | + let navigationMode: NavigationMode; |
| 40 | + |
| 41 | + beforeEach(async(() => { |
| 42 | + TestBed.configureTestingModule({ |
| 43 | + declarations: [WizardTestComponent], |
| 44 | + imports: [WizardModule] |
| 45 | + }).compileComponents(); |
| 46 | + })); |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + wizardTestFixture = TestBed.createComponent(WizardTestComponent); |
| 50 | + wizardTestFixture.detectChanges(); |
| 51 | + |
| 52 | + wizardTest = wizardTestFixture.componentInstance; |
| 53 | + wizardState = wizardTestFixture.debugElement.query(By.css('wizard')).injector.get(WizardState); |
| 54 | + navigationMode = wizardState.navigationMode; |
| 55 | + }); |
| 56 | + |
| 57 | + it('should create an instance', () => { |
| 58 | + expect(wizardTestFixture.debugElement.query(By.directive(ResetWizardDirective))).toBeTruthy(); |
| 59 | + expect(wizardTestFixture.debugElement.queryAll(By.directive(ResetWizardDirective)).length).toBe(3); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should reset the wizard correctly 1', fakeAsync(() => { |
| 63 | + let resetButtons = wizardTestFixture.debugElement |
| 64 | + .queryAll(By.directive(ResetWizardDirective)); |
| 65 | + |
| 66 | + navigationMode.goToStep(1); |
| 67 | + tick(); |
| 68 | + wizardTestFixture.detectChanges(); |
| 69 | + |
| 70 | + expect(wizardState.getStepAtIndex(0).selected).toBe(false); |
| 71 | + expect(wizardState.getStepAtIndex(0).completed).toBe(true); |
| 72 | + expect(wizardState.getStepAtIndex(1).selected).toBe(true); |
| 73 | + expect(wizardState.getStepAtIndex(1).completed).toBe(false); |
| 74 | + expect(wizardTest.eventLog).toEqual([]); |
| 75 | + |
| 76 | + resetButtons[0].nativeElement.click(); |
| 77 | + tick(); |
| 78 | + wizardTestFixture.detectChanges(); |
| 79 | + |
| 80 | + expect(wizardState.getStepAtIndex(0).selected).toBe(true); |
| 81 | + expect(wizardState.getStepAtIndex(0).completed).toBe(false); |
| 82 | + expect(wizardState.getStepAtIndex(1).selected).toBe(false); |
| 83 | + expect(wizardState.getStepAtIndex(1).completed).toBe(false); |
| 84 | + expect(wizardTest.eventLog).toEqual([]); |
| 85 | + })); |
| 86 | + |
| 87 | + it('should reset the wizard correctly 2', fakeAsync(() => { |
| 88 | + let resetButtons = wizardTestFixture.debugElement |
| 89 | + .queryAll(By.directive(ResetWizardDirective)); |
| 90 | + |
| 91 | + navigationMode.goToStep(1); |
| 92 | + tick(); |
| 93 | + wizardTestFixture.detectChanges(); |
| 94 | + |
| 95 | + expect(wizardState.getStepAtIndex(0).selected).toBe(false); |
| 96 | + expect(wizardState.getStepAtIndex(0).completed).toBe(true); |
| 97 | + expect(wizardState.getStepAtIndex(1).selected).toBe(true); |
| 98 | + expect(wizardState.getStepAtIndex(1).completed).toBe(false); |
| 99 | + expect(wizardTest.eventLog).toEqual([]); |
| 100 | + |
| 101 | + resetButtons[1].nativeElement.click(); |
| 102 | + tick(); |
| 103 | + wizardTestFixture.detectChanges(); |
| 104 | + |
| 105 | + expect(wizardState.getStepAtIndex(0).selected).toBe(true); |
| 106 | + expect(wizardState.getStepAtIndex(0).completed).toBe(false); |
| 107 | + expect(wizardState.getStepAtIndex(1).selected).toBe(false); |
| 108 | + expect(wizardState.getStepAtIndex(1).completed).toBe(false); |
| 109 | + expect(wizardTest.eventLog).toEqual(['Cleanup done!']); |
| 110 | + })); |
| 111 | + |
| 112 | + it('should reset the wizard correctly 3', fakeAsync(() => { |
| 113 | + let resetButtons = wizardTestFixture.debugElement |
| 114 | + .queryAll(By.directive(ResetWizardDirective)); |
| 115 | + |
| 116 | + navigationMode.goToStep(1); |
| 117 | + tick(); |
| 118 | + wizardTestFixture.detectChanges(); |
| 119 | + |
| 120 | + expect(wizardState.getStepAtIndex(0).selected).toBe(false); |
| 121 | + expect(wizardState.getStepAtIndex(0).completed).toBe(true); |
| 122 | + expect(wizardState.getStepAtIndex(1).selected).toBe(true); |
| 123 | + expect(wizardState.getStepAtIndex(1).completed).toBe(false); |
| 124 | + expect(wizardTest.eventLog).toEqual([]); |
| 125 | + |
| 126 | + resetButtons[2].nativeElement.click(); |
| 127 | + tick(); |
| 128 | + wizardTestFixture.detectChanges(); |
| 129 | + |
| 130 | + expect(wizardState.getStepAtIndex(0).selected).toBe(true); |
| 131 | + expect(wizardState.getStepAtIndex(0).completed).toBe(false); |
| 132 | + expect(wizardState.getStepAtIndex(1).selected).toBe(false); |
| 133 | + expect(wizardState.getStepAtIndex(1).completed).toBe(false); |
| 134 | + expect(wizardTest.eventLog).toEqual(['Cleanup done!']); |
| 135 | + })); |
| 136 | +}); |
0 commit comments