diff --git a/angular-wizard/angular-wizard-tests.ts b/angular-wizard/angular-wizard-tests.ts index 61ad7a484..05a5d574f 100644 --- a/angular-wizard/angular-wizard-tests.ts +++ b/angular-wizard/angular-wizard-tests.ts @@ -1,68 +1,114 @@ /// /// +/// /// // test file taken from https://github.com/mgonto/angular-wizard -interface WizardScope extends ng.IScope { - referenceCurrentStep:string; - stepValidation:()=>void; - finishedWizard:()=>void; - enterValidation:()=>void; +interface IWizardScope extends ng.IScope { + referenceCurrentStep: string; + stepValidation: () => void; + finishedWizard: () => void; + enterValidation: () => void; + exitValidation: boolean; + dynamicStepDisabled: string; } describe('AngularWizard', function () { - var $compile:ng.ICompileService, - $rootScope:ng.IRootScopeService, WizardHandler:angular.mgoAngularWizard.WizardHandler, scope:WizardScope; + var $compile: ng.ICompileService, + $q: ng.IQService, + $rootScope: ng.IRootScopeService, + $timeout: ng.ITimeoutService, + WizardHandler: angular.mgoAngularWizard.WizardHandler; + + beforeEach(() => angular.module('mgo-angular-wizard')); + + beforeEach(inject(function (_$compile_: ng.ICompileService, + _$q_: ng.IQService, + _$rootScope_: ng.IRootScopeService, + _$timeout_: ng.ITimeoutService, + _WizardHandler_: angular.mgoAngularWizard.WizardHandler) { + $compile = _$compile_; + $q = _$q_; + $rootScope = _$rootScope_; + $timeout = _$timeout_; + WizardHandler = _WizardHandler_; + })); /** - * Create the view with wizard to test + * Create the generic view with wizard to test * @param {Scope} scope A scope to bind to * @return {[DOM element]} A DOM element compiled */ - function createView(scope:WizardScope) { + function createGenericView(scope: IWizardScope) { scope.referenceCurrentStep = null; var element = angular.element('' - + ' ' - + '

This is the first step

' - + '

Here you can use whatever you want. You can use other directives, binding, etc.

' - + ' ' - + '
' - + ' ' - + '

Continuing

' - + '

You have continued here!

' - + ' ' - + '
' - + ' ' - + '

Even more steps!!

' - + ' ' - + '
' - + '
'); + + ' ' + + '

This is the first step

' + + '

Here you can use whatever you want. You can use other directives, binding, etc.

' + + ' ' + + '
' + + ' ' + + '

Dynamic {{dynamicStepDisabled}}

' + + '

You have continued here!

' + + ' ' + + '
' + + ' ' + + '

Continuing

' + + '

You have continued here!

' + + ' ' + + '
' + + ' ' + + '

Even more steps!!

' + + ' ' + + '
' + + ''); var elementCompiled = $compile(element)(scope); $rootScope.$digest(); return elementCompiled; } it("should correctly create the wizard", function () { - var view = createView(scope); + var scope = $rootScope.$new(); + var view = createGenericView(scope); expect(WizardHandler).toBeTruthy(); - expect(view.find('section').length).toEqual(3); + expect(view.find('section').length).toEqual(4); // expect the correct step to be desirable one expect(scope.referenceCurrentStep).toEqual('Starting'); }); - it("should go to the next step", function () { - var view = createView(scope); + var scope = $rootScope.$new(); + var view = createGenericView(scope); + expect(scope.referenceCurrentStep).toEqual('Starting'); + WizardHandler.wizard().next(); + $rootScope.$digest(); + expect(scope.referenceCurrentStep).toEqual('Dynamic'); + }); + it("should render only those steps which are enabled", function () { + var scope =$rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().next(); $rootScope.$digest(); expect(scope.referenceCurrentStep).toEqual('Continuing'); }); + it("should enable or disable dynamic steps based on conditions", function () { + var scope = $rootScope.$new(); + var view = createGenericView(scope); + expect(scope.referenceCurrentStep).toEqual('Starting'); + scope.dynamicStepDisabled = 'Y'; + $rootScope.$digest(); + WizardHandler.wizard().goTo(2); + $rootScope.$digest(); + expect(scope.referenceCurrentStep).toEqual('More steps'); + }); it("should return to a previous step", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().next(); $rootScope.$digest(); @@ -72,24 +118,27 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('Starting'); }); it("should go to a step specified by name", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().goTo('More steps'); $rootScope.$digest(); expect(scope.referenceCurrentStep).toEqual('More steps'); }); it("should go to a step specified by index", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().goTo(2); $rootScope.$digest(); expect(scope.referenceCurrentStep).toEqual('More steps'); }); it("should go to next step becasue callback is truthy", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().next(function () { return true @@ -98,8 +147,9 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('Continuing'); }); it("should NOT go to next step because callback is falsey", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().next(function () { return false @@ -108,16 +158,18 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('Starting'); }); it("should go to next step because CANEXIT is UNDEFINED", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().next(); $rootScope.$digest(); expect(scope.referenceCurrentStep).toEqual('Continuing'); }); it("should go to next step because CANEXIT is TRUE", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); scope.stepValidation = function () { return true; }; @@ -130,8 +182,9 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('More steps'); }); it("should NOT go to next step because CANEXIT is FALSE", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); scope.stepValidation = function () { return false; }; @@ -144,8 +197,9 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('Continuing'); }); it("should go to next step because CANENTER is TRUE", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); scope.enterValidation = function () { return true; }; @@ -158,8 +212,9 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('More steps'); }); it("should NOT go to next step because CANENTER is FALSE", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); scope.enterValidation = function () { return false; }; @@ -172,8 +227,9 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('Continuing'); }); it("should NOT return to a previous step. Although CANEXIT is false and we are heading to a previous state, the can enter validation is false", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); scope.stepValidation = function () { return false; }; @@ -189,8 +245,9 @@ describe('AngularWizard', function () { expect(scope.referenceCurrentStep).toEqual('Continuing'); }); it("should return to a previous step even though CANEXIT is false", function () { - - var view = createView(scope); + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); scope.stepValidation = function () { return false; }; @@ -202,17 +259,66 @@ describe('AngularWizard', function () { $rootScope.$digest(); expect(scope.referenceCurrentStep).toEqual('Starting'); }); - it("should finish", function () { - - var flag = false; - scope.finishedWizard = function () { - flag = true; + it("should go to the next step because the promise that CANENTER returns resolves to true", function (done) { + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); + scope.enterValidation = function () { + var deferred = $q.defer(); + $timeout(function () { + deferred.resolve(true); + done(); + }); + return deferred.promise; }; - var view = createView(scope); + expect(scope.referenceCurrentStep).toEqual('Starting'); + WizardHandler.wizard().next(); + $rootScope.$digest(); + expect(scope.referenceCurrentStep).toEqual('Continuing'); + WizardHandler.wizard().next(); + $timeout.flush(); + expect(scope.referenceCurrentStep).toEqual('More steps'); + }); + it("should go to the next step because CANEXIT is set to true", function () { + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); + scope.exitValidation = true; + expect(scope.referenceCurrentStep).toEqual('Starting'); + WizardHandler.wizard().next(); + $rootScope.$digest(); + expect(scope.referenceCurrentStep).toEqual('Continuing'); + WizardHandler.wizard().next(); + $rootScope.$digest(); + expect(scope.referenceCurrentStep).toEqual('More steps'); + }); + it("should finish", function () { + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var flag = false; + scope.finishedWizard = function () { flag = true; }; + var view = createGenericView(scope); expect(scope.referenceCurrentStep).toEqual('Starting'); WizardHandler.wizard().finish(); expect(flag).toBeTruthy(); $rootScope.$digest(); }); + it("should go to first step when reset is called", function () { + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); + expect(scope.referenceCurrentStep).toEqual('Starting'); + WizardHandler.wizard().goTo(2); + $rootScope.$digest(); + expect(scope.referenceCurrentStep).toEqual('More steps'); + WizardHandler.wizard().reset(); + $rootScope.$digest(); + expect(scope.referenceCurrentStep).toEqual('Starting'); + }); + it("step description should be accessible", function () { + var scope = $rootScope.$new(); + scope.dynamicStepDisabled = 'Y'; + var view = createGenericView(scope); + expect((view.isolateScope()).steps[0].description).toEqual('Step description'); + }); }); - diff --git a/angular-wizard/angular-wizard.d.ts b/angular-wizard/angular-wizard.d.ts index f079a46c2..a854f94a0 100644 --- a/angular-wizard/angular-wizard.d.ts +++ b/angular-wizard/angular-wizard.d.ts @@ -1,21 +1,38 @@ -// Type definitions for Angular Wizard 0.4.2 +// Type definitions for Angular Wizard 0.6.1 // Project: https://github.com/mgonto/angular-wizard -// Definitions by: Marko Jurisic +// Definitions by: Marko Jurisic , Ronald Wildenberg // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module angular.mgoAngularWizard { interface WizardHandler { - wizard(name?:string): Wizard; - addWizard(name:string, wizard:Wizard):void; - removeWizard(name:string):void; + wizard(name?: string): Wizard; + addWizard(name: string, wizard: Wizard): void; + removeWizard(name: string): void; } interface Wizard { - next(nextHandler?:Function):void; - previous():void; - goTo(step:number):void; - goTo(step:string):void; - finish():void; - currentStepNumber():number; + next(nextHandler?: () => boolean): void; + previous(): void; + cancel: () => void; + goTo(step: number | string): void; + finish(): void; + reset: () => void; + + addStep: (step: WzStep) => void; + currentStep: () => WzStep; + currentStepNumber(): number; + currentStepDescription: () => string; + currentStepTitle: () => string; + getEnabledSteps(): WzStep[]; + } + + interface WzStep { + canenter: (...args: any[]) => boolean; + canexit: (...args: any[]) => boolean; + description: string; + selected: boolean; + title: string; + wzData: any; + wzTitle: string; } }