-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new resetWizard directive (#48)
- add a new resetWizard directive - fix the filename for the selectedStep directive tests - extend the README with some resetWizard directive specific details - add sections for Components and Directives to the README
- Loading branch information
Showing
6 changed files
with
226 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
/** | ||
* Created by marc on 29.12.16. | ||
*/ | ||
|
||
export {EnableBackLinksDirective} from './enable-back-links.directive'; | ||
export {GoToStepDirective} from './go-to-step.directive'; | ||
export {NextStepDirective} from './next-step.directive'; | ||
export {PreviousStepDirective} from './previous-step.directive'; | ||
export {OptionalStepDirective} from './optional-step.directive'; | ||
export {WizardStepTitleDirective} from './wizard-step-title.directive'; | ||
export {EnableBackLinksDirective} from './enable-back-links.directive'; | ||
export {PreviousStepDirective} from './previous-step.directive'; | ||
export {ResetWizardDirective} from './reset-wizard.directive'; | ||
export {SelectedStepDirective} from './selected-step.directive'; | ||
export {WizardCompletionStepDirective} from './wizard-completion-step.directive'; | ||
export {WizardStepDirective} from './wizard-step.directive'; | ||
export {WizardStepTitleDirective} from './wizard-step-title.directive'; | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import {OptionalStepDirective} from './optional-step.directive'; | ||
import {Component} from '@angular/core'; | ||
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; | ||
import {By} from '@angular/platform-browser'; | ||
import {WizardModule} from '../wizard.module'; | ||
import {WizardState} from '../navigation/wizard-state.model'; | ||
import {NavigationMode} from '../navigation/navigation-mode.interface'; | ||
import {ResetWizardDirective} from './reset-wizard.directive'; | ||
|
||
@Component({ | ||
selector: 'test-wizard', | ||
template: ` | ||
<wizard> | ||
<wizard-step stepTitle='Steptitle 1'> | ||
Step 1 | ||
</wizard-step> | ||
<wizard-step stepTitle='Steptitle 2'> | ||
Step 2 | ||
<button type="button" resetWizard>Reset (normal)</button> | ||
<button type="button" resetWizard (finalize)='cleanup()'>Reset (cleanup)</button> | ||
<button type="button" reset (finalize)='cleanup()'>Reset (cleanup short)</button> | ||
</wizard-step> | ||
</wizard> | ||
` | ||
}) | ||
class WizardTestComponent { | ||
public eventLog: Array<string> = []; | ||
|
||
public cleanup(): void { | ||
this.eventLog.push('Cleanup done!'); | ||
}; | ||
} | ||
|
||
describe('ResetWizardDirective', () => { | ||
let wizardTest: WizardTestComponent; | ||
let wizardTestFixture: ComponentFixture<WizardTestComponent>; | ||
|
||
let wizardState: WizardState; | ||
let navigationMode: NavigationMode; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [WizardTestComponent], | ||
imports: [WizardModule] | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
wizardTestFixture = TestBed.createComponent(WizardTestComponent); | ||
wizardTestFixture.detectChanges(); | ||
|
||
wizardTest = wizardTestFixture.componentInstance; | ||
wizardState = wizardTestFixture.debugElement.query(By.css('wizard')).injector.get(WizardState); | ||
navigationMode = wizardState.navigationMode; | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(wizardTestFixture.debugElement.query(By.directive(ResetWizardDirective))).toBeTruthy(); | ||
expect(wizardTestFixture.debugElement.queryAll(By.directive(ResetWizardDirective)).length).toBe(3); | ||
}); | ||
|
||
it('should reset the wizard correctly 1', fakeAsync(() => { | ||
let resetButtons = wizardTestFixture.debugElement | ||
.queryAll(By.directive(ResetWizardDirective)); | ||
|
||
navigationMode.goToStep(1); | ||
tick(); | ||
wizardTestFixture.detectChanges(); | ||
|
||
expect(wizardState.getStepAtIndex(0).selected).toBe(false); | ||
expect(wizardState.getStepAtIndex(0).completed).toBe(true); | ||
expect(wizardState.getStepAtIndex(1).selected).toBe(true); | ||
expect(wizardState.getStepAtIndex(1).completed).toBe(false); | ||
expect(wizardTest.eventLog).toEqual([]); | ||
|
||
resetButtons[0].nativeElement.click(); | ||
tick(); | ||
wizardTestFixture.detectChanges(); | ||
|
||
expect(wizardState.getStepAtIndex(0).selected).toBe(true); | ||
expect(wizardState.getStepAtIndex(0).completed).toBe(false); | ||
expect(wizardState.getStepAtIndex(1).selected).toBe(false); | ||
expect(wizardState.getStepAtIndex(1).completed).toBe(false); | ||
expect(wizardTest.eventLog).toEqual([]); | ||
})); | ||
|
||
it('should reset the wizard correctly 2', fakeAsync(() => { | ||
let resetButtons = wizardTestFixture.debugElement | ||
.queryAll(By.directive(ResetWizardDirective)); | ||
|
||
navigationMode.goToStep(1); | ||
tick(); | ||
wizardTestFixture.detectChanges(); | ||
|
||
expect(wizardState.getStepAtIndex(0).selected).toBe(false); | ||
expect(wizardState.getStepAtIndex(0).completed).toBe(true); | ||
expect(wizardState.getStepAtIndex(1).selected).toBe(true); | ||
expect(wizardState.getStepAtIndex(1).completed).toBe(false); | ||
expect(wizardTest.eventLog).toEqual([]); | ||
|
||
resetButtons[1].nativeElement.click(); | ||
tick(); | ||
wizardTestFixture.detectChanges(); | ||
|
||
expect(wizardState.getStepAtIndex(0).selected).toBe(true); | ||
expect(wizardState.getStepAtIndex(0).completed).toBe(false); | ||
expect(wizardState.getStepAtIndex(1).selected).toBe(false); | ||
expect(wizardState.getStepAtIndex(1).completed).toBe(false); | ||
expect(wizardTest.eventLog).toEqual(['Cleanup done!']); | ||
})); | ||
|
||
it('should reset the wizard correctly 3', fakeAsync(() => { | ||
let resetButtons = wizardTestFixture.debugElement | ||
.queryAll(By.directive(ResetWizardDirective)); | ||
|
||
navigationMode.goToStep(1); | ||
tick(); | ||
wizardTestFixture.detectChanges(); | ||
|
||
expect(wizardState.getStepAtIndex(0).selected).toBe(false); | ||
expect(wizardState.getStepAtIndex(0).completed).toBe(true); | ||
expect(wizardState.getStepAtIndex(1).selected).toBe(true); | ||
expect(wizardState.getStepAtIndex(1).completed).toBe(false); | ||
expect(wizardTest.eventLog).toEqual([]); | ||
|
||
resetButtons[2].nativeElement.click(); | ||
tick(); | ||
wizardTestFixture.detectChanges(); | ||
|
||
expect(wizardState.getStepAtIndex(0).selected).toBe(true); | ||
expect(wizardState.getStepAtIndex(0).completed).toBe(false); | ||
expect(wizardState.getStepAtIndex(1).selected).toBe(false); | ||
expect(wizardState.getStepAtIndex(1).completed).toBe(false); | ||
expect(wizardTest.eventLog).toEqual(['Cleanup done!']); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {Directive, EventEmitter, HostListener, Output} from '@angular/core'; | ||
import {WizardState} from '../navigation/wizard-state.model'; | ||
import {NavigationMode} from '../navigation/navigation-mode.interface'; | ||
|
||
/** | ||
* The `resetWizard` directive can be used to reset the wizard to its initial state. | ||
* This directive accepts an output, which can be used to specify some custom cleanup work during the reset process. | ||
* | ||
* ### Syntax | ||
* | ||
* ```html | ||
* <button resetWizard (finalize)="custom reset task">...</button> | ||
* ``` | ||
* | ||
* @author Marc Arndt | ||
*/ | ||
@Directive({ | ||
selector: '[reset], [resetWizard]' | ||
}) | ||
export class ResetWizardDirective { | ||
/** | ||
* An [[EventEmitter]] containing some tasks to be done, directly before the wizard is being reset | ||
*/ | ||
@Output() | ||
public finalize: EventEmitter<void> = new EventEmitter(); | ||
|
||
/** | ||
* The navigation mode | ||
* | ||
* @returns {NavigationMode} | ||
*/ | ||
private get navigationMode(): NavigationMode { | ||
return this.wizardState.navigationMode; | ||
} | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param wizardState The wizard state | ||
*/ | ||
constructor(private wizardState: WizardState) { } | ||
|
||
/** | ||
* Resets the wizard | ||
*/ | ||
@HostListener('click', ['$event']) onClick(): void { | ||
// do some optional cleanup work | ||
this.finalize.emit(); | ||
// reset the wizard to its initial state | ||
this.navigationMode.reset(); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters