forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectModelSpec.js
333 lines (268 loc) · 10.8 KB
/
ObjectModelSpec.js
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
325
326
327
328
329
330
331
332
333
'use strict';
describe('angular.scenario.ObjectModel', function() {
var model;
var runner;
var spec, step;
function buildSpec(id, name, definitions) {
var spec = {
id: id,
name: name,
definition: {
name: definitions.shift()
}
};
var currentDef = spec.definition;
forEach(definitions, function(defName) {
currentDef.parent = {
name: defName
};
currentDef = currentDef.parent;
});
return spec;
}
function buildStep(name, line) {
return {
name: name || 'test step',
line: function() { return line || ''; }
};
}
beforeEach(function() {
spec = buildSpec(1, 'test spec', ['describe 1']);
step = buildStep();
runner = new angular.scenario.testing.MockRunner();
model = new angular.scenario.ObjectModel(runner);
});
it('should value default empty value', function() {
expect(model.value).toEqual({
name: '',
children: []
});
});
it('should add spec and create describe blocks on SpecBegin event', function() {
runner.emit('SpecBegin', buildSpec(1, 'test spec', ['describe 2', 'describe 1']));
expect(model.value.children['describe 1']).toBeDefined();
expect(model.value.children['describe 1'].children['describe 2']).toBeDefined();
expect(model.value.children['describe 1'].children['describe 2'].specs['test spec']).toBeDefined();
});
it('should set fullDefinitionName on SpecBegin event', function() {
runner.emit('SpecBegin', buildSpec(1, 'fake spec', ['describe 2']));
var spec = model.getSpec(1);
expect(spec.fullDefinitionName).toBeDefined();
expect(spec.fullDefinitionName).toEqual('describe 2');
});
it('should set fullDefinitionName on SpecBegin event (join more names by space)', function() {
runner.emit('SpecBegin', buildSpec(1, 'fake spec', ['describe 2', 'describe 1']));
var spec = model.getSpec(1);
expect(spec.fullDefinitionName).toEqual('describe 1 describe 2');
});
it('should add step to spec on StepBegin', function() {
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepEnd', spec, step);
runner.emit('SpecEnd', spec);
expect(model.value.children['describe 1'].specs['test spec'].steps.length).toEqual(1);
});
it('should update spec timer duration on SpecEnd event', function() {
runner.emit('SpecBegin', spec);
runner.emit('SpecEnd', spec);
expect(model.value.children['describe 1'].specs['test spec'].duration).toBeDefined();
});
it('should update step timer duration on StepEnd event', function() {
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepEnd', spec, step);
runner.emit('SpecEnd', spec);
expect(model.value.children['describe 1'].specs['test spec'].steps[0].duration).toBeDefined();
});
it('should set spec status on SpecEnd to success if no status set', function() {
runner.emit('SpecBegin', spec);
runner.emit('SpecEnd', spec);
expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('success');
});
it('should set status to error after SpecError', function() {
runner.emit('SpecBegin', spec);
runner.emit('SpecError', spec, 'error');
expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('error');
});
it('should set spec status to failure if step fails', function() {
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepEnd', spec, step);
runner.emit('StepBegin', spec, step);
runner.emit('StepFailure', spec, step, 'error');
runner.emit('StepEnd', spec, step);
runner.emit('StepBegin', spec, step);
runner.emit('StepEnd', spec, step);
runner.emit('SpecEnd', spec);
expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('failure');
});
it('should set spec status to error if step errors', function() {
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepError', spec, step, 'error');
runner.emit('StepEnd', spec, step);
runner.emit('StepBegin', spec, step);
runner.emit('StepFailure', spec, step, 'error');
runner.emit('StepEnd', spec, step);
runner.emit('SpecEnd', spec);
expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('error');
});
describe('events', function() {
var Spec = angular.scenario.ObjectModel.Spec,
Step = angular.scenario.ObjectModel.Step,
callback;
beforeEach(function() {
callback = jasmine.createSpy('listener');
});
it('should provide method for registering a listener', function() {
expect(model.on).toBeDefined();
expect(model.on instanceof Function).toBe(true);
});
it('should forward SpecBegin event', function() {
model.on('SpecBegin', callback);
runner.emit('SpecBegin', spec);
expect(callback).toHaveBeenCalled();
});
it('should forward SpecBegin event with ObjectModel.Spec as a param', function() {
model.on('SpecBegin', callback);
runner.emit('SpecBegin', spec);
expect(callback.mostRecentCall.args[0] instanceof Spec).toBe(true);
expect(callback.mostRecentCall.args[0].name).toEqual(spec.name);
});
it('should forward SpecError event', function() {
model.on('SpecError', callback);
runner.emit('SpecBegin', spec);
runner.emit('SpecError', spec, {});
expect(callback).toHaveBeenCalled();
});
it('should forward SpecError event with ObjectModel.Spec and error as a params', function() {
var error = {};
model.on('SpecError', callback);
runner.emit('SpecBegin', spec);
runner.emit('SpecError', spec, error);
var param = callback.mostRecentCall.args[0];
expect(param instanceof Spec).toBe(true);
expect(param.name).toEqual(spec.name);
expect(param.status).toEqual('error');
expect(param.error).toBe(error);
});
it('should forward SpecEnd event', function() {
model.on('SpecEnd', callback);
runner.emit('SpecBegin', spec);
runner.emit('SpecEnd', spec);
expect(callback).toHaveBeenCalled();
});
it('should forward SpecEnd event with ObjectModel.Spec as a param', function() {
model.on('SpecEnd', callback);
runner.emit('SpecBegin', spec);
runner.emit('SpecEnd', spec);
expect(callback.mostRecentCall.args[0] instanceof Spec).toBe(true);
expect(callback.mostRecentCall.args[0].name).toEqual(spec.name);
});
it('should forward StepBegin event', function() {
model.on('StepBegin', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
expect(callback).toHaveBeenCalled();
});
it('should forward StepBegin event with Spec and Step as params', function() {
model.on('StepBegin', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
var params = callback.mostRecentCall.args;
expect(params[0] instanceof Spec).toBe(true);
expect(params[0].name).toEqual(spec.name);
expect(params[1] instanceof Step).toBe(true);
});
it('should forward StepError event', function() {
model.on('StepError', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepError', spec, step, {});
expect(callback).toHaveBeenCalled();
});
it('should forward StepError event with Spec, Step and error as params', function() {
var error = {};
model.on('StepError', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepError', spec, step, error);
var params = callback.mostRecentCall.args;
expect(params[0] instanceof Spec).toBe(true);
expect(params[0].name).toEqual(spec.name);
expect(params[1] instanceof Step).toBe(true);
expect(params[1].status).toEqual('error');
expect(params[2]).toBe(error);
});
it('should forward StepFailure event', function() {
model.on('StepFailure', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepFailure', spec, step, {});
expect(callback).toHaveBeenCalled();
});
it('should forward StepFailure event with Spec, Step and error as params', function() {
var error = {};
model.on('StepFailure', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepFailure', spec, step, error);
var params = callback.mostRecentCall.args;
expect(params[0] instanceof Spec).toBe(true);
expect(params[0].name).toEqual(spec.name);
expect(params[1] instanceof Step).toBe(true);
expect(params[1].status).toEqual('failure');
expect(params[2]).toBe(error);
});
it('should forward StepEnd event', function() {
model.on('StepEnd', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepEnd', spec, step);
expect(callback).toHaveBeenCalled();
});
it('should forward StepEnd event with Spec and Step as params', function() {
model.on('StepEnd', callback);
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepEnd', spec, step);
var params = callback.mostRecentCall.args;
expect(params[0] instanceof Spec).toBe(true);
expect(params[0].name).toEqual(spec.name);
expect(params[1] instanceof Step).toBe(true);
});
it('should forward RunnerEnd event', function() {
model.on('RunnerEnd', callback);
runner.emit('RunnerEnd');
expect(callback).toHaveBeenCalled();
});
it('should set error of first failure', function() {
var error = 'first-error',
step2 = buildStep();
model.on('SpecEnd', function(spec) {
expect(spec.error).toBeDefined();
expect(spec.error).toBe(error);
});
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepFailure', spec, step, error);
runner.emit('StepBegin', spec, step2);
runner.emit('StepFailure', spec, step2, 'second-error');
runner.emit('SpecEnd', spec);
});
it('should set line number of first failure', function() {
var step = buildStep('fake', 'first-line'),
step2 = buildStep('fake2', 'second-line');
model.on('SpecEnd', function(spec) {
expect(spec.line).toBeDefined();
expect(spec.line).toBe('first-line');
});
runner.emit('SpecBegin', spec);
runner.emit('StepBegin', spec, step);
runner.emit('StepFailure', spec, step, null);
runner.emit('StepBegin', spec, step2);
runner.emit('StepFailure', spec, step2, null);
runner.emit('SpecEnd', spec);
});
});
});