forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-wizard-tests.ts
324 lines (315 loc) · 13.9 KB
/
angular-wizard-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/// <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 IWizardScope extends ng.IScope {
referenceCurrentStep: string;
stepValidation: () => void;
finishedWizard: () => void;
enterValidation: () => void;
exitValidation: boolean;
dynamicStepDisabled: string;
}
describe('AngularWizard', function () {
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 generic view with wizard to test
* @param {Scope} scope A scope to bind to
* @return {[DOM element]} A DOM element compiled
*/
function createGenericView(scope: IWizardScope) {
scope.referenceCurrentStep = null;
var element = angular.element('<wizard on-finish="finishedWizard()" current-step="referenceCurrentStep" ng-init="msg = 14" >'
+ ' <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 scope = <IWizardScope>$rootScope.$new();
var view = createGenericView(scope);
expect(WizardHandler).toBeTruthy();
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 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 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');
WizardHandler.wizard().previous();
$rootScope.$digest();
expect(scope.referenceCurrentStep).toEqual('Starting');
});
it("should go to a step specified by name", function () {
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 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 scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
expect(scope.referenceCurrentStep).toEqual('Starting');
WizardHandler.wizard().next(function () {
return true
});
$rootScope.$digest();
expect(scope.referenceCurrentStep).toEqual('Continuing');
});
it("should NOT go to next step because callback is falsey", function () {
var scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
expect(scope.referenceCurrentStep).toEqual('Starting');
WizardHandler.wizard().next(function () {
return false
});
$rootScope.$digest();
expect(scope.referenceCurrentStep).toEqual('Starting');
});
it("should go to next step because CANEXIT is UNDEFINED", 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 go to next step because CANEXIT is TRUE", function () {
var scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
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');
});
it("should NOT go to next step because CANEXIT is FALSE", function () {
var scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
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');
});
it("should go to next step because CANENTER is TRUE", function () {
var scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
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');
});
it("should NOT go to next step because CANENTER is FALSE", function () {
var scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
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');
});
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 scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
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');
});
it("should return to a previous step even though CANEXIT is false", function () {
var scope = <IWizardScope>$rootScope.$new();
scope.dynamicStepDisabled = 'Y';
var view = createGenericView(scope);
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');
});
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;
};
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');
});
});