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