2016-05-10 01:06:27 +00:00
/// <reference types="jasmine" />
2015-04-21 10:51:50 +00:00
// test file taken from https://github.com/mgonto/angular-wizard
2016-07-21 17:07:13 +00:00
import * as ng from "angular" ;
import * as angular from "angular" ;
2015-04-21 10:51:50 +00:00
2016-01-22 10:12:22 +00:00
interface IWizardScope extends ng . IScope {
2020-01-02 23:55:58 +00:00
editMode : boolean ;
2016-01-22 10:12:22 +00:00
referenceCurrentStep : string ;
stepValidation : ( ) = > void ;
finishedWizard : ( ) = > void ;
enterValidation : ( ) = > void ;
exitValidation : boolean ;
dynamicStepDisabled : string ;
2015-04-21 20:41:07 +00:00
}
2015-04-21 10:51:50 +00:00
2015-04-21 20:41:07 +00:00
describe ( 'AngularWizard' , function ( ) {
2016-01-22 10:12:22 +00:00
var $compile : ng.ICompileService ,
$q : ng.IQService ,
$rootScope : ng.IRootScopeService ,
$timeout : ng.ITimeoutService ,
WizardHandler : angular.mgoAngularWizard.WizardHandler ;
beforeEach ( ( ) = > angular . module ( 'mgo-angular-wizard' ) ) ;
2015-04-21 10:51:50 +00:00
/ * *
2016-01-22 10:12:22 +00:00
* Create the generic view with wizard to test
2015-04-21 10:51:50 +00:00
* @param { Scope } scope A scope to bind to
* @return { [ DOM element ] } A DOM element compiled
* /
2016-01-22 10:12:22 +00:00
function createGenericView ( scope : IWizardScope ) {
2015-04-21 20:41:07 +00:00
scope . referenceCurrentStep = null ;
2020-01-02 23:55:58 +00:00
scope . editMode = false ;
var element = angular . element ( '<wizard on-finish="finishedWizard()" current-step="referenceCurrentStep" ng-init="msg = 14" edit-mode="editMode">'
2016-01-22 10:12:22 +00:00
+ ' <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>' ) ;
2015-04-21 20:41:07 +00:00
var elementCompiled = $compile ( element ) ( scope ) ;
$rootScope . $digest ( ) ;
return elementCompiled ;
2015-04-21 10:51:50 +00:00
}
2015-04-21 20:41:07 +00:00
it ( "should correctly create the wizard" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( WizardHandler ) . toBeTruthy ( ) ;
2016-01-22 10:12:22 +00:00
expect ( view . find ( 'section' ) . length ) . toEqual ( 4 ) ;
2015-04-21 20:41:07 +00:00
// expect the correct step to be desirable one
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should go to the next step" , function ( ) {
2016-01-22 10:12:22 +00:00
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 ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2016-01-22 10:12:22 +00:00
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' ) ;
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should return to a previous step" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
WizardHandler . wizard ( ) . previous ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should go to a step specified by name" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . goTo ( 'More steps' ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'More steps' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should go to a step specified by index" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . goTo ( 2 ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'More steps' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should go to next step becasue callback is truthy" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( function ( ) {
return true
} ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should NOT go to next step because callback is falsey" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( function ( ) {
return false
} ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should go to next step because CANEXIT is UNDEFINED" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should go to next step because CANEXIT is TRUE" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
scope . stepValidation = function ( ) {
return 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' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should NOT go to next step because CANEXIT is FALSE" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
scope . stepValidation = function ( ) {
return false ;
} ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should go to next step because CANENTER is TRUE" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
scope . enterValidation = function ( ) {
return 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' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should NOT go to next step because CANENTER is FALSE" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
scope . enterValidation = function ( ) {
return false ;
} ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
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 ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
scope . stepValidation = function ( ) {
return false ;
} ;
scope . enterValidation = function ( ) {
return false ;
} ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
WizardHandler . wizard ( ) . previous ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should return to a previous step even though CANEXIT is false" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
scope . stepValidation = function ( ) {
return false ;
} ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
WizardHandler . wizard ( ) . previous ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2016-11-01 12:45:51 +00:00
it ( "should go to the next step because the promise that CANENTER returns resolves to true" , function ( done ) {
2016-01-22 10:12:22 +00:00
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 ;
} ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . next ( ) ;
$rootScope . $digest ( ) ;
expect ( scope . referenceCurrentStep ) . toEqual ( 'Continuing' ) ;
WizardHandler . wizard ( ) . next ( ) ;
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' ) ;
} ) ;
2015-04-21 20:41:07 +00:00
it ( "should finish" , function ( ) {
2016-01-22 10:12:22 +00:00
var scope = < IWizardScope > $rootScope . $new ( ) ;
scope . dynamicStepDisabled = 'Y' ;
2015-04-21 20:41:07 +00:00
var flag = false ;
2016-01-22 10:12:22 +00:00
scope . finishedWizard = function ( ) { flag = true ; } ;
var view = createGenericView ( scope ) ;
2015-04-21 20:41:07 +00:00
expect ( scope . referenceCurrentStep ) . toEqual ( 'Starting' ) ;
WizardHandler . wizard ( ) . finish ( ) ;
expect ( flag ) . toBeTruthy ( ) ;
$rootScope . $digest ( ) ;
2015-04-21 10:51:50 +00:00
} ) ;
2016-01-22 10:12:22 +00:00
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' ) ;
} ) ;
2020-01-02 23:55:58 +00:00
it ( "should set edit mode through custom method" , ( ) = > {
const scope = < IWizardScope > $rootScope . $new ( ) ;
scope . editMode = true ;
createGenericView ( scope ) ;
expect ( scope . editMode ) . toBeTrue ( ) ;
WizardHandler . wizard ( ) . setEditMode ( false ) ;
$rootScope . $digest ( ) ;
expect ( scope . editMode ) . toBeFalse ( ) ;
} ) ;
2015-04-21 20:41:07 +00:00
} ) ;