Typescript definition and tests updated for angular-wizard 0.6.1

This commit is contained in:
Ronald Wildenberg
2016-01-22 11:12:22 +01:00
parent fa3f7cf10e
commit 5cca5ff51a
2 changed files with 193 additions and 70 deletions
+165 -59
View File
@@ -1,68 +1,114 @@
/// <reference path="../jasmine/jasmine.d.ts" />
/// <reference path="../angularjs/angular.d.ts" />
/// <reference path="../angularjs/angular-mocks.d.ts" />
/// <reference path="angular-wizard.d.ts" />
// 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('<wizard on-finish="finishedWizard()" current-step="referenceCurrentStep" ng-init="msg = 14" >'
+ ' <wz-step title="Starting" canenter="enterValidation">'
+ ' <h1>This is the first step</h1>'
+ ' <p>Here you can use whatever you want. You can use other directives, binding, etc.</p>'
+ ' <input type="submit" wz-next value="Continue" />'
+ ' </wz-step>'
+ ' <wz-step title="Continuing" canexit="stepValidation">'
+ ' <h1>Continuing</h1>'
+ ' <p>You have continued here!</p>'
+ ' <input type="submit" wz-next value="Go on" />'
+ ' </wz-step>'
+ ' <wz-step title="More steps" canenter="enterValidation">'
+ ' <p>Even more steps!!</p>'
+ ' <input type="submit" wz-next value="Finish now" />'
+ ' </wz-step>'
+ '</wizard>');
+ ' <wz-step wz-title="Starting" canenter="enterValidation" description="Step description">'
+ ' <h1>This is the first step</h1>'
+ ' <p>Here you can use whatever you want. You can use other directives, binding, etc.</p>'
+ ' <input type="submit" wz-next value="Continue" />'
+ ' </wz-step>'
+ ' <wz-step wz-title="Dynamic" wz-disabled="{{dynamicStepDisabled == \'Y\'}}">'
+ ' <h1>Dynamic {{dynamicStepDisabled}}</h1>'
+ ' <p>You have continued here!</p>'
+ ' <input type="submit" wz-next value="Go on" />'
+ ' </wz-step>'
+ ' <wz-step wz-title="Continuing" canexit="stepValidation">'
+ ' <h1>Continuing</h1>'
+ ' <p>You have continued here!</p>'
+ ' <input type="submit" wz-next value="Go on" />'
+ ' </wz-step>'
+ ' <wz-step wz-title="More steps" canenter="enterValidation">'
+ ' <p>Even more steps!!</p>'
+ ' <input type="submit" wz-next value="Finish now" />'
+ ' </wz-step>'
+ '</wizard>');
var elementCompiled = $compile(element)(scope);
$rootScope.$digest();
return elementCompiled;
}
it("should correctly create the wizard", function () {
var view = createView(scope);
var scope = <IWizardScope>$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 = <IWizardScope>$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 =<IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$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 = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
expect((<any>view.isolateScope()).steps[0].description).toEqual('Step description');
});
});
+28 -11
View File
@@ -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 <https://github.com/mjurisic>
// Definitions by: Marko Jurisic <https://github.com/mjurisic>, Ronald Wildenberg <https://github.com/rwwilden>
// 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;
}
}