|
| 1 | +/** |
| 2 | + * Created by marc on 30.06.17. |
| 3 | + */ |
| 4 | +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {ViewChild, Component} from '@angular/core'; |
| 6 | +import {WizardComponent} from '../components/wizard.component'; |
| 7 | +import {MovingDirection} from '../util/moving-direction.enum'; |
| 8 | +import {By} from '@angular/platform-browser'; |
| 9 | +import {WizardModule} from '../wizard.module'; |
| 10 | + |
| 11 | +@Component({ |
| 12 | + selector: 'test-wizard', |
| 13 | + template: ` |
| 14 | + <wizard> |
| 15 | + <wizard-step title='Steptitle 1' (stepEnter)="enterInto($event, 1)" (stepExit)="exitFrom($event, 1)"> |
| 16 | + Step 1 |
| 17 | + </wizard-step> |
| 18 | + <wizard-step title='Steptitle 2' [canExit]="isValid" |
| 19 | + optionalStep (stepEnter)="enterInto($event, 2)" (stepExit)="exitFrom($event, 2)"> |
| 20 | + Step 2 |
| 21 | + </wizard-step> |
| 22 | + <wizard-completion-step enableBackLinks title='Completion steptitle 3' (stepEnter)="enterInto($event, 3)" |
| 23 | + (stepExit)="completionStepExit($event, 3)"> |
| 24 | + Step 3 |
| 25 | + </wizard-completion-step> |
| 26 | + </wizard> |
| 27 | + ` |
| 28 | +}) |
| 29 | +class WizardTestComponent { |
| 30 | + @ViewChild(WizardComponent) |
| 31 | + public wizard: WizardComponent; |
| 32 | + |
| 33 | + public isValid: any = true; |
| 34 | + |
| 35 | + public eventLog: Array<string> = new Array<string>(); |
| 36 | + |
| 37 | + public completionStepExit: (direction: MovingDirection, source: number) => void = this.exitFrom; |
| 38 | + |
| 39 | + enterInto(direction: MovingDirection, destination: number): void { |
| 40 | + this.eventLog.push(`enter ${MovingDirection[direction]} ${destination}`); |
| 41 | + } |
| 42 | + |
| 43 | + exitFrom(direction: MovingDirection, source: number): void { |
| 44 | + this.eventLog.push(`exit ${MovingDirection[direction]} ${source}`); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +describe('EnableBackLinksDirective', () => { |
| 49 | + let wizardTest: WizardTestComponent; |
| 50 | + let wizardTestFixture: ComponentFixture<WizardTestComponent>; |
| 51 | + |
| 52 | + beforeEach(async(() => { |
| 53 | + TestBed.configureTestingModule({ |
| 54 | + declarations: [WizardTestComponent], |
| 55 | + imports: [WizardModule] |
| 56 | + }).compileComponents(); |
| 57 | + })); |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + wizardTestFixture = TestBed.createComponent(WizardTestComponent); |
| 61 | + wizardTest = wizardTestFixture.componentInstance; |
| 62 | + wizardTestFixture.detectChanges(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should create', () => { |
| 66 | + expect(wizardTest).toBeTruthy(); |
| 67 | + expect(wizardTestFixture.debugElement.queryAll(By.css('wizard-step')).length).toBe(2); |
| 68 | + expect(wizardTestFixture.debugElement.queryAll(By.css('wizard-completion-step')).length).toBe(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should have correct step title', () => { |
| 72 | + expect(wizardTest).toBeTruthy(); |
| 73 | + expect(wizardTest.wizard.getStepAtIndex(0).title).toBe('Steptitle 1'); |
| 74 | + expect(wizardTest.wizard.getStepAtIndex(1).title).toBe('Steptitle 2'); |
| 75 | + expect(wizardTest.wizard.getStepAtIndex(2).title).toBe('Completion steptitle 3'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should enter first step after initialisation', () => { |
| 79 | + expect(wizardTest.eventLog).toEqual(['enter Forwards 1']); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should enter completion step after first step', () => { |
| 83 | + expect(wizardTest.wizard.currentStepIndex).toBe(0); |
| 84 | + |
| 85 | + wizardTest.wizard.goToNextStep(); |
| 86 | + wizardTestFixture.detectChanges(); |
| 87 | + |
| 88 | + expect(wizardTest.wizard.currentStepIndex).toBe(1); |
| 89 | + expect(wizardTest.eventLog).toEqual(['enter Forwards 1', 'exit Forwards 1', 'enter Forwards 2']); |
| 90 | + |
| 91 | + wizardTest.wizard.goToNextStep(); |
| 92 | + wizardTestFixture.detectChanges(); |
| 93 | + |
| 94 | + expect(wizardTest.wizard.currentStepIndex).toBe(2); |
| 95 | + expect(wizardTest.eventLog).toEqual(['enter Forwards 1', 'exit Forwards 1', 'enter Forwards 2', |
| 96 | + 'exit Forwards 2', 'enter Forwards 3']); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should enter completion step after jumping over second optional step', () => { |
| 100 | + wizardTest.wizard.goToStep(2); |
| 101 | + wizardTestFixture.detectChanges(); |
| 102 | + |
| 103 | + expect(wizardTest.wizard.completed).toBe(true); |
| 104 | + expect(wizardTest.eventLog).toEqual(['enter Forwards 1', 'exit Forwards 1', 'enter Forwards 3']); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should be able to leave the completion step', () => { |
| 108 | + wizardTest.wizard.goToStep(2); |
| 109 | + wizardTestFixture.detectChanges(); |
| 110 | + |
| 111 | + expect(wizardTest.wizard.canGoToStep(0)).toBe(true); |
| 112 | + expect(wizardTest.wizard.canGoToStep(1)).toBe(true); |
| 113 | + }); |
| 114 | + |
| 115 | + |
| 116 | + it('should be able to leave the completion step in any direction', () => { |
| 117 | + wizardTest.isValid = false; |
| 118 | + |
| 119 | + wizardTest.wizard.goToStep(2); |
| 120 | + wizardTestFixture.detectChanges(); |
| 121 | + |
| 122 | + expect(wizardTest.wizard.currentStepIndex).toBe(2); |
| 123 | + expect(wizardTest.wizard.currentStep.canExit).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should leave the completion step', () => { |
| 127 | + wizardTest.isValid = false; |
| 128 | + |
| 129 | + wizardTest.wizard.goToStep(2); |
| 130 | + wizardTestFixture.detectChanges(); |
| 131 | + |
| 132 | + expect(wizardTest.wizard.currentStepIndex).toBe(2); |
| 133 | + |
| 134 | + wizardTest.wizard.goToPreviousStep(); |
| 135 | + wizardTestFixture.detectChanges(); |
| 136 | + |
| 137 | + expect(wizardTest.wizard.currentStepIndex).toBe(1); |
| 138 | + expect(wizardTest.eventLog) |
| 139 | + .toEqual(['enter Forwards 1', 'exit Forwards 1', 'enter Forwards 3', 'exit Backwards 3', 'enter Backwards 2']); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should work with changed stepExit value', () => { |
| 143 | + wizardTest.isValid = false; |
| 144 | + wizardTest.completionStepExit = (direction: MovingDirection, source: number) => { |
| 145 | + wizardTest.eventLog.push(`changed exit ${MovingDirection[direction]} ${source}`); |
| 146 | + }; |
| 147 | + |
| 148 | + expect(wizardTest.wizard.completed).toBe(false); |
| 149 | + |
| 150 | + wizardTest.wizard.goToStep(2); |
| 151 | + wizardTestFixture.detectChanges(); |
| 152 | + |
| 153 | + expect(wizardTest.wizard.currentStepIndex).toBe(2); |
| 154 | + expect(wizardTest.wizard.completed).toBe(true); |
| 155 | + |
| 156 | + wizardTest.wizard.goToPreviousStep(); |
| 157 | + wizardTestFixture.detectChanges(); |
| 158 | + |
| 159 | + expect(wizardTest.wizard.currentStepIndex).toBe(1); |
| 160 | + expect(wizardTest.eventLog) |
| 161 | + .toEqual(['enter Forwards 1', 'exit Forwards 1', 'enter Forwards 3', 'changed exit Backwards 3', 'enter Backwards 2']); |
| 162 | + expect(wizardTest.wizard.completed).toBe(false); |
| 163 | + }); |
| 164 | +}); |
0 commit comments