From 59777f9c5016f247c9ba7b7459773bffa9545ad5 Mon Sep 17 00:00:00 2001 From: earshinov Date: Tue, 11 Jun 2019 23:49:09 +0300 Subject: [PATCH] Clean up navigation mode tests (#219) * Extract test-utils.ts with checkWizardState function * Clean up navigation unit tests It makes little sense to have tests for StrictNavigationMode, SemiStrictNavigationMode, FreeNavigationMode after the corresponding classes have been removed in https://github.com/madoar/angular-archwizard/pull/211 * Fix issues reported by Codacy, except 'magic numbers' which seem inevitable in unit tests * Remove trailing commas * Provide comments for the `checkWizardStep` function --- .../navigation/free-navigation-mode.spec.ts | 286 ----------------- .../semi-strict-navigation-mode.spec.ts | 299 ------------------ .../navigation/strict-navigation-mode.spec.ts | 228 ------------- .../wizard-navigation-allow-forward.spec.ts | 178 +++++++++++ ...rd-navigation-with-completion-step.spec.ts | 181 +++++++++++ ...zard-navigation-with-optional-step.spec.ts | 174 ++++++++++ src/lib/navigation/wizard-navigation.spec.ts | 174 ++++++++++ src/lib/util/test-utils.ts | 30 ++ 8 files changed, 737 insertions(+), 813 deletions(-) delete mode 100644 src/lib/navigation/free-navigation-mode.spec.ts delete mode 100644 src/lib/navigation/semi-strict-navigation-mode.spec.ts delete mode 100644 src/lib/navigation/strict-navigation-mode.spec.ts create mode 100644 src/lib/navigation/wizard-navigation-allow-forward.spec.ts create mode 100644 src/lib/navigation/wizard-navigation-with-completion-step.spec.ts create mode 100644 src/lib/navigation/wizard-navigation-with-optional-step.spec.ts create mode 100644 src/lib/navigation/wizard-navigation.spec.ts create mode 100644 src/lib/util/test-utils.ts diff --git a/src/lib/navigation/free-navigation-mode.spec.ts b/src/lib/navigation/free-navigation-mode.spec.ts deleted file mode 100644 index 6b1cfc30..00000000 --- a/src/lib/navigation/free-navigation-mode.spec.ts +++ /dev/null @@ -1,286 +0,0 @@ -import {Component, ViewChild} from '@angular/core'; -import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; -import {ArchwizardModule} from '../archwizard.module'; -import {WizardComponent} from '../components/wizard.component'; - -@Component({ - selector: 'aw-test-wizard', - template: ` - - - Step 1 - - - Step 2 - - - Step 3 - - - ` -}) -class WizardTestComponent { - @ViewChild(WizardComponent) - public wizard: WizardComponent; -} - -describe('FreeNavigationMode', () => { - let wizardTestFixture: ComponentFixture; - - let wizardTest: WizardTestComponent; - let wizard: WizardComponent; - - beforeEach(async(() => { - TestBed.configureTestingModule({ - declarations: [WizardTestComponent], - imports: [ArchwizardModule] - }).compileComponents(); - })); - - beforeEach(() => { - wizardTestFixture = TestBed.createComponent(WizardTestComponent); - wizardTestFixture.detectChanges(); - - wizardTest = wizardTestFixture.componentInstance; - wizard = wizardTest.wizard; - }); - - it('should return correct can go to step', async(() => { - wizard.canGoToStep(-1).then(result => expect(result).toBe(false)); - wizard.canGoToStep(0).then(result => expect(result).toBe(true)); - wizard.canGoToStep(1).then(result => expect(result).toBe(true)); - wizard.canGoToStep(2).then(result => expect(result).toBe(true)); - wizard.canGoToStep(3).then(result => expect(result).toBe(false)); - })); - - it('should go to step', fakeAsync(() => { - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(2); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(0); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(true); - expect(wizard.completed).toBe(true); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(true); - expect(wizard.completed).toBe(true); - - wizard.goToStep(2); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(true); - expect(wizard.completed).toBe(true); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(true); - expect(wizard.completed).toBe(true); - })); - - it('should go to next step', fakeAsync(() => { - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); - - it('should go to previous step', fakeAsync(() => { - expect(wizard.getStepAtIndex(0).completed).toBe(false); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToPreviousStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); - - it('should stay at the current step', fakeAsync(() => { - expect(wizard.getStepAtIndex(0).completed).toBe(false); - - wizard.goToPreviousStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(-1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(0); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); - - it('should reset the wizard correctly', fakeAsync(() => { - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.reset(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.defaultStepIndex = -1; - expect(() => wizard.reset()) - .toThrow(new Error(`The wizard doesn't contain a step with index -1`)); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.defaultStepIndex = 2; - wizard.reset(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); -}); diff --git a/src/lib/navigation/semi-strict-navigation-mode.spec.ts b/src/lib/navigation/semi-strict-navigation-mode.spec.ts deleted file mode 100644 index 8e09aa3e..00000000 --- a/src/lib/navigation/semi-strict-navigation-mode.spec.ts +++ /dev/null @@ -1,299 +0,0 @@ -import {Component, ViewChild} from '@angular/core'; -import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; -import {ArchwizardModule} from '../archwizard.module'; -import {WizardComponent} from '../components/wizard.component'; - -@Component({ - selector: 'aw-test-wizard', - template: ` - - - Step 1 - - - Step 2 - - - Step 3 - - - ` -}) -class WizardTestComponent { - @ViewChild(WizardComponent) - public wizard: WizardComponent; -} - -describe('SemiStrictNavigationMode', () => { - let wizardTestFixture: ComponentFixture; - - let wizardTest: WizardTestComponent; - let wizard: WizardComponent; - - beforeEach(async(() => { - TestBed.configureTestingModule({ - declarations: [WizardTestComponent], - imports: [ArchwizardModule] - }).compileComponents(); - })); - - beforeEach(() => { - wizardTestFixture = TestBed.createComponent(WizardTestComponent); - wizardTestFixture.detectChanges(); - - wizardTest = wizardTestFixture.componentInstance; - wizard = wizardTest.wizard; - }); - - it('should return correct can go to step', async(() => { - wizard.canGoToStep(-1).then(result => expect(result).toBe(false)); - wizard.canGoToStep(0).then(result => expect(result).toBe(true)); - wizard.canGoToStep(1).then(result => expect(result).toBe(true)); - wizard.canGoToStep(2).then(result => expect(result).toBe(false)); - wizard.canGoToStep(3).then(result => expect(result).toBe(false)); - })); - - it('should go to step', fakeAsync(() => { - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(2); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(true); - expect(wizard.completed).toBe(true); - - wizard.goToStep(0); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(2); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(true); - expect(wizard.completed).toBe(true); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); - - it('should go to next step', fakeAsync(() => { - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); - - it('should go to previous step', fakeAsync(() => { - expect(wizard.getStepAtIndex(0).completed).toBe(false); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToPreviousStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); - - it('should stay at the current step', fakeAsync(() => { - expect(wizard.getStepAtIndex(0).completed).toBe(false); - - wizard.goToPreviousStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(-1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.goToStep(0); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); - - it('should reset the wizard correctly', fakeAsync(() => { - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(true); - expect(wizard.completed).toBe(true); - - wizard.reset(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.defaultStepIndex = -1; - expect(() => wizard.reset()) - .toThrow(new Error(`The wizard doesn't contain a step with index -1`)); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.defaultStepIndex = 2; - expect(() => wizard.reset()) - .toThrow(new Error(`The default step index 2 references a completion step`)); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.defaultStepIndex = 1; - wizard.reset(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); -}); diff --git a/src/lib/navigation/strict-navigation-mode.spec.ts b/src/lib/navigation/strict-navigation-mode.spec.ts deleted file mode 100644 index cc70e3f6..00000000 --- a/src/lib/navigation/strict-navigation-mode.spec.ts +++ /dev/null @@ -1,228 +0,0 @@ -import {Component, ViewChild} from '@angular/core'; -import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; -import {ArchwizardModule} from '../archwizard.module'; -import {WizardComponent} from '../components/wizard.component'; -import {WizardStep} from '../util/wizard-step.interface'; - -@Component({ - selector: 'aw-test-wizard', - template: ` - - - Step 1 - - - Step 2 - - - Step 3 - - - ` -}) -class WizardTestComponent { - @ViewChild(WizardComponent) - public wizard: WizardComponent; -} - -function checkWizardSteps(steps: Array, selectedStepIndex: number) { - steps.forEach((step, index) => { - // Only the selected step should be selected - if (index === selectedStepIndex) { - expect(step.selected).toBe(true, `the selected wizard step index ${index} is not selected`); - } else { - expect(step.selected).toBe(false, `the not selected wizard step index ${index} is selected`); - } - - // All steps before the selected step need to be completed - if (index < selectedStepIndex) { - expect(step.completed).toBe(true, - `the wizard step ${index} is not completed while the currently selected step index is ${selectedStepIndex}`); - } else if (index > selectedStepIndex) { - expect(step.completed).toBe(false, - `the wizard step ${index} is completed while the currently selected step index is ${selectedStepIndex}`); - } - }); -} - -describe('StrictNavigationMode', () => { - let wizardTestFixture: ComponentFixture; - - let wizardTest: WizardTestComponent; - let wizard: WizardComponent; - - beforeEach(async(() => { - TestBed.configureTestingModule({ - declarations: [WizardTestComponent], - imports: [ArchwizardModule] - }).compileComponents(); - })); - - beforeEach(() => { - wizardTestFixture = TestBed.createComponent(WizardTestComponent); - wizardTestFixture.detectChanges(); - - wizardTest = wizardTestFixture.componentInstance; - wizard = wizardTest.wizard; - }); - - it('should return correct can go to step', async(() => { - wizard.canGoToStep(-1).then(result => expect(result).toBe(false)); - wizard.canGoToStep(0).then(result => expect(result).toBe(true)); - wizard.canGoToStep(1).then(result => expect(result).toBe(true)); - wizard.canGoToStep(2).then(result => expect(result).toBe(false)); - wizard.canGoToStep(3).then(result => expect(result).toBe(false)); - })); - - it('should go to step', fakeAsync(() => { - checkWizardSteps(wizard.wizardSteps, 0); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.currentStep).toBe(wizard.getStepAtIndex(1)); - expect(wizard.currentStep.completed).toBe(false); - - checkWizardSteps(wizard.wizardSteps, 1); - - wizard.goToStep(2); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.currentStep).toBe(wizard.getStepAtIndex(2)); - expect(wizard.currentStep.completed).toBe(false); - - checkWizardSteps(wizard.wizardSteps, 2); - - wizard.goToStep(0); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.currentStep).toBe(wizard.getStepAtIndex(0)); - expect(wizard.currentStep.completed).toBe(true); - - checkWizardSteps(wizard.wizardSteps, 0); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.currentStep).toBe(wizard.getStepAtIndex(1)); - expect(wizard.currentStep.completed).toBe(false); - - checkWizardSteps(wizard.wizardSteps, 1); - - wizard.goToStep(2); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.currentStep).toBe(wizard.getStepAtIndex(2)); - expect(wizard.currentStep.completed).toBe(false); - - checkWizardSteps(wizard.wizardSteps, 2); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.currentStep).toBe(wizard.getStepAtIndex(1)); - expect(wizard.currentStep.completed).toBe(true); - - checkWizardSteps(wizard.wizardSteps, 1); - })); - - it('should go to next step', fakeAsync(() => { - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.currentStep.stepTitle).toBe('Steptitle 2'); - expect(wizard.currentStep.completed).toBe(false); - - checkWizardSteps(wizard.wizardSteps, 1); - })); - - it('should go to previous step', fakeAsync(() => { - expect(wizard.getStepAtIndex(0).completed).toBe(false); - checkWizardSteps(wizard.wizardSteps, 0); - - wizard.goToStep(1); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.getStepAtIndex(0).completed).toBe(true); - checkWizardSteps(wizard.wizardSteps, 1); - - wizard.goToPreviousStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.currentStep).toBe(wizard.getStepAtIndex(0)); - - checkWizardSteps(wizard.wizardSteps, 0); - })); - - it('should reset the wizard correctly', fakeAsync(() => { - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - wizard.goToNextStep(); - tick(); - wizardTestFixture.detectChanges(); - - expect(wizard.currentStepIndex).toBe(2); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(true); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(true); - expect(wizard.getStepAtIndex(2).selected).toBe(true); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.reset(); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.defaultStepIndex = -1; - expect(() => wizard.reset()) - .toThrow(new Error(`The wizard doesn't contain a step with index -1`)); - - expect(wizard.currentStepIndex).toBe(0); - expect(wizard.getStepAtIndex(0).selected).toBe(true); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(false); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - - wizard.defaultStepIndex = 1; - wizard.reset(); - - expect(wizard.currentStepIndex).toBe(1); - expect(wizard.getStepAtIndex(0).selected).toBe(false); - expect(wizard.getStepAtIndex(0).completed).toBe(false); - expect(wizard.getStepAtIndex(1).selected).toBe(true); - expect(wizard.getStepAtIndex(1).completed).toBe(false); - expect(wizard.getStepAtIndex(2).selected).toBe(false); - expect(wizard.getStepAtIndex(2).completed).toBe(false); - expect(wizard.completed).toBe(false); - })); -}); diff --git a/src/lib/navigation/wizard-navigation-allow-forward.spec.ts b/src/lib/navigation/wizard-navigation-allow-forward.spec.ts new file mode 100644 index 00000000..d38e210a --- /dev/null +++ b/src/lib/navigation/wizard-navigation-allow-forward.spec.ts @@ -0,0 +1,178 @@ +import {Component, ViewChild} from '@angular/core'; +import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; +import {ArchwizardModule} from '../archwizard.module'; +import {WizardComponent} from '../components/wizard.component'; +import {checkWizardState} from '../util/test-utils'; + +@Component({ + selector: 'aw-test-wizard', + template: ` + + + Step 1 + + + Step 2 + + + Step 3 + + + ` +}) +class WizardTestComponent { + @ViewChild(WizardComponent) + public wizard: WizardComponent; +} + +describe('Wizard navigation with navigateForward=allow', () => { + let wizardTestFixture: ComponentFixture; + + let wizardTest: WizardTestComponent; + let wizard: WizardComponent; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [WizardTestComponent], + imports: [ArchwizardModule] + }).compileComponents(); + })); + + beforeEach(() => { + wizardTestFixture = TestBed.createComponent(WizardTestComponent); + wizardTestFixture.detectChanges(); + + wizardTest = wizardTestFixture.componentInstance; + wizard = wizardTest.wizard; + }); + + it('should return correct can go to step', async(() => { + wizard.canGoToStep(-1).then(result => expect(result).toBe(false)); + wizard.canGoToStep(0).then(result => expect(result).toBe(true)); + wizard.canGoToStep(1).then(result => expect(result).toBe(true)); + wizard.canGoToStep(2).then(result => expect(result).toBe(true)); + wizard.canGoToStep(3).then(result => expect(result).toBe(false)); + })); + + it('should go to step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + // If forward navigation is allowed, visited steps after + // the selected step are still considered completed + checkWizardState(wizard, 0, [0, 1, 2], true); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0, 1, 2], true); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1, 2], true); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0, 1, 2], true); + })); + + it('should go to next step', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + })); + + it('should go to previous step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + // If forward navigation is allowed, visited steps after + // the selected step are still considered completed + checkWizardState(wizard, 0, [0, 1], false); + })); + + it('should stay at the current step', fakeAsync(() => { + expect(wizard.getStepAtIndex(0).completed).toBe(false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(-1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + })); + + it('should reset the wizard correctly', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.reset(); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = -1; + expect(() => wizard.reset()) + .toThrow(new Error(`The wizard doesn't contain a step with index -1`)); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = 1; + wizard.reset(); + + checkWizardState(wizard, 1, [], false); + + wizard.defaultStepIndex = 2; + wizard.reset(); + + checkWizardState(wizard, 2, [], false); + })); +}); diff --git a/src/lib/navigation/wizard-navigation-with-completion-step.spec.ts b/src/lib/navigation/wizard-navigation-with-completion-step.spec.ts new file mode 100644 index 00000000..ebac0fa4 --- /dev/null +++ b/src/lib/navigation/wizard-navigation-with-completion-step.spec.ts @@ -0,0 +1,181 @@ +import {Component, ViewChild} from '@angular/core'; +import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; +import {ArchwizardModule} from '../archwizard.module'; +import {WizardComponent} from '../components/wizard.component'; +import {checkWizardState} from '../util/test-utils'; + +@Component({ + selector: 'aw-test-wizard', + template: ` + + + Step 1 + + + Step 2 + + + Step 3 + + + ` +}) +class WizardTestComponent { + @ViewChild(WizardComponent) + public wizard: WizardComponent; +} + +describe('Wizard navigation with completion step', () => { + let wizardTestFixture: ComponentFixture; + + let wizardTest: WizardTestComponent; + let wizard: WizardComponent; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [WizardTestComponent], + imports: [ArchwizardModule] + }).compileComponents(); + })); + + beforeEach(() => { + wizardTestFixture = TestBed.createComponent(WizardTestComponent); + wizardTestFixture.detectChanges(); + + wizardTest = wizardTestFixture.componentInstance; + wizard = wizardTest.wizard; + }); + + it('should return correct can go to step', async(() => { + wizard.canGoToStep(-1).then(result => expect(result).toBe(false)); + wizard.canGoToStep(0).then(result => expect(result).toBe(true)); + wizard.canGoToStep(1).then(result => expect(result).toBe(true)); + wizard.canGoToStep(2).then(result => expect(result).toBe(false)); + wizard.canGoToStep(3).then(result => expect(result).toBe(false)); + })); + + it('should go to step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + // Completion step is marked completed right after being navigated to, + // and marks the whole wizard completed as well. + checkWizardState(wizard, 2, [0, 1, 2], true); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + // Completion step is marked completed right after being navigated to, + // and marks the whole wizard completed as well. + checkWizardState(wizard, 2, [0, 1, 2], true); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0, 1], false); + })); + + it('should go to next step', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + })); + + it('should go to previous step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + })); + + it('should stay at the current step', fakeAsync(() => { + expect(wizard.getStepAtIndex(0).completed).toBe(false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(-1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + })); + + it('should reset the wizard correctly', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + // Completion step is marked completed right after being navigated to, + // and marks the whole wizard completed as well. + checkWizardState(wizard, 2, [0, 1, 2], true); + + wizard.reset(); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = -1; + expect(() => wizard.reset()) + .toThrow(new Error(`The wizard doesn't contain a step with index -1`)); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = 1; + wizard.reset(); + + checkWizardState(wizard, 1, [], false); + + wizard.defaultStepIndex = 2; + expect(() => wizard.reset()) + .toThrow(new Error(`The default step index 2 references a completion step`)); + + checkWizardState(wizard, 1, [], false); + })); +}); diff --git a/src/lib/navigation/wizard-navigation-with-optional-step.spec.ts b/src/lib/navigation/wizard-navigation-with-optional-step.spec.ts new file mode 100644 index 00000000..be402da2 --- /dev/null +++ b/src/lib/navigation/wizard-navigation-with-optional-step.spec.ts @@ -0,0 +1,174 @@ +import {Component, ViewChild} from '@angular/core'; +import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; +import {ArchwizardModule} from '../archwizard.module'; +import {WizardComponent} from '../components/wizard.component'; +import {checkWizardState} from '../util/test-utils'; + +@Component({ + selector: 'aw-test-wizard', + template: ` + + + Step 1 + + + Step 2 + + + Step 3 + + + ` +}) +class WizardTestComponent { + @ViewChild(WizardComponent) + public wizard: WizardComponent; +} + +describe('Wizard navigation with optional step', () => { + let wizardTestFixture: ComponentFixture; + + let wizardTest: WizardTestComponent; + let wizard: WizardComponent; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [WizardTestComponent], + imports: [ArchwizardModule] + }).compileComponents(); + })); + + beforeEach(() => { + wizardTestFixture = TestBed.createComponent(WizardTestComponent); + wizardTestFixture.detectChanges(); + + wizardTest = wizardTestFixture.componentInstance; + wizard = wizardTest.wizard; + }); + + it('should return correct can go to step', async(() => { + wizard.canGoToStep(-1).then(result => expect(result).toBe(false)); + wizard.canGoToStep(0).then(result => expect(result).toBe(true)); + wizard.canGoToStep(1).then(result => expect(result).toBe(true)); + wizard.canGoToStep(2).then(result => expect(result).toBe(false)); + wizard.canGoToStep(3).then(result => expect(result).toBe(false)); + })); + + it('should go to step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0, 1], false); + })); + + it('should go to next step', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + })); + + it('should go to previous step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + })); + + it('should stay at the current step', fakeAsync(() => { + expect(wizard.getStepAtIndex(0).completed).toBe(false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(-1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + })); + + it('should reset the wizard correctly', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.reset(); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = -1; + expect(() => wizard.reset()) + .toThrow(new Error(`The wizard doesn't contain a step with index -1`)); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = 1; + wizard.reset(); + + checkWizardState(wizard, 1, [], false); + + wizard.defaultStepIndex = 2; + wizard.reset(); + + checkWizardState(wizard, 2, [], false); + })); +}); diff --git a/src/lib/navigation/wizard-navigation.spec.ts b/src/lib/navigation/wizard-navigation.spec.ts new file mode 100644 index 00000000..a5eead89 --- /dev/null +++ b/src/lib/navigation/wizard-navigation.spec.ts @@ -0,0 +1,174 @@ +import {Component, ViewChild} from '@angular/core'; +import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; +import {ArchwizardModule} from '../archwizard.module'; +import {WizardComponent} from '../components/wizard.component'; +import {checkWizardState} from '../util/test-utils'; + +@Component({ + selector: 'aw-test-wizard', + template: ` + + + Step 1 + + + Step 2 + + + Step 3 + + + ` +}) +class WizardTestComponent { + @ViewChild(WizardComponent) + public wizard: WizardComponent; +} + +describe('Wizard navigation', () => { + let wizardTestFixture: ComponentFixture; + + let wizardTest: WizardTestComponent; + let wizard: WizardComponent; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [WizardTestComponent], + imports: [ArchwizardModule] + }).compileComponents(); + })); + + beforeEach(() => { + wizardTestFixture = TestBed.createComponent(WizardTestComponent); + wizardTestFixture.detectChanges(); + + wizardTest = wizardTestFixture.componentInstance; + wizard = wizardTest.wizard; + }); + + it('should return correct can go to step', async(() => { + wizard.canGoToStep(-1).then(result => expect(result).toBe(false)); + wizard.canGoToStep(0).then(result => expect(result).toBe(true)); + wizard.canGoToStep(1).then(result => expect(result).toBe(true)); + wizard.canGoToStep(2).then(result => expect(result).toBe(false)); + wizard.canGoToStep(3).then(result => expect(result).toBe(false)); + })); + + it('should go to step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToStep(2); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0, 1], false); + })); + + it('should go to next step', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + })); + + it('should go to previous step', fakeAsync(() => { + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 1, [0], false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + })); + + it('should stay at the current step', fakeAsync(() => { + expect(wizard.getStepAtIndex(0).completed).toBe(false); + + wizard.goToPreviousStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(-1); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [], false); + + wizard.goToStep(0); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 0, [0], false); + })); + + it('should reset the wizard correctly', fakeAsync(() => { + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + wizard.goToNextStep(); + tick(); + wizardTestFixture.detectChanges(); + + checkWizardState(wizard, 2, [0, 1], false); + + wizard.reset(); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = -1; + expect(() => wizard.reset()) + .toThrow(new Error(`The wizard doesn't contain a step with index -1`)); + + checkWizardState(wizard, 0, [], false); + + wizard.defaultStepIndex = 1; + wizard.reset(); + + checkWizardState(wizard, 1, [], false); + + wizard.defaultStepIndex = 2; + wizard.reset(); + + checkWizardState(wizard, 2, [], false); + })); +}); diff --git a/src/lib/util/test-utils.ts b/src/lib/util/test-utils.ts new file mode 100644 index 00000000..32fc8fd6 --- /dev/null +++ b/src/lib/util/test-utils.ts @@ -0,0 +1,30 @@ +import { WizardComponent } from '../components/wizard.component'; + +/** + * Check wizard state and cause the current unit test to fail if the actual state does not match the expected. + * + * @param wizard Wizard component under test + * @param selectedStepIndex Expected selected step index + * @param completedStepIndexes Array of step indexes expected to be completed + * @param wizardCompleted Whether the whole wizard is expected to be completed + */ +export function checkWizardState( + wizard: WizardComponent, + selectedStepIndex: number, + completedStepIndexes: number[], + wizardCompleted: boolean, +): void { + expect(wizard.currentStepIndex).toBe(selectedStepIndex, `expected current step index to be ${selectedStepIndex}`); + + wizard.wizardSteps.forEach((step, index) => { + // Only the selected step should be selected + expect(step.selected).toBe(index === selectedStepIndex, `expected only step ${index} to be selected`); + + // All steps before the selected step need to be completed + expect(step.completed).toBe(completedStepIndexes.includes(index), + `expected step ${index} ${completedStepIndexes.includes(index) ? 'to be completed' : 'not to be completed'}`); + }); + + expect(wizard.completed).toBe(wizardCompleted, + `expected wizard ${wizardCompleted ? 'to be completed' : 'not to be completed'}`); +}