Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7288be2

Browse files
feat(ngMock): add they helpers for testing multiple specs
There are now three new test helpers: `they`, `tthey` and `xthey`, which will create multiple `it`, `iit` and `xit` blocks, respectively, parameterized by each item in a collection that is passed. (with tests and ammendments by @petebacondarwin) Closes #10864
1 parent 2b279dd commit 7288be2

File tree

4 files changed

+224
-11
lines changed

4 files changed

+224
-11
lines changed

test/.jshintrc

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
"waitsFor": false,
143143
"runs": false,
144144
"dump": false,
145+
"they": false,
146+
"tthey": false,
147+
"xthey": false,
145148

146149
/* e2e */
147150
"browser": false,

test/helpers/privateMocks.js

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
'use strict';
22

3+
/* globals xit */
4+
5+
function baseThey(msg, vals, spec, itFn) {
6+
var valsIsArray = isArray(vals);
7+
8+
forEach(vals, function(val, key) {
9+
var m = msg.replace('$prop', angular.toJson(valsIsArray ? val : key));
10+
itFn(m, function() {
11+
/* jshint -W040 : ignore possible strict violation due to use of this */
12+
spec.call(this, val);
13+
});
14+
});
15+
}
16+
17+
function they(msg, vals, spec) {
18+
baseThey(msg, vals, spec, it);
19+
}
20+
21+
function tthey(msg, vals, spec) {
22+
baseThey(msg, vals, spec, iit);
23+
}
24+
25+
function xthey(msg, vals, spec) {
26+
/* jshint -W040 : ignore possible strict violation due to use of this */
27+
baseThey(msg, vals, spec, xit);
28+
}
29+
30+
31+
332
function createMockStyleSheet(doc, wind) {
433
doc = doc ? doc[0] : document;
534
wind = wind || window;

test/helpers/privateMocksSpec.js

+190
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,196 @@
11
'use strict';
22

33
describe('private mocks', function() {
4+
5+
describe('Jasmine extensions', function() {
6+
7+
describe('they', function() {
8+
it('should call `it` for each item in an array', function() {
9+
spyOn(window, 'it');
10+
11+
they('should do stuff with $prop', ['a', 'b', 'c']);
12+
expect(window.it.calls.length).toEqual(3);
13+
expect(window.it).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
14+
expect(window.it).toHaveBeenCalledWith('should do stuff with "b"', jasmine.any(Function));
15+
expect(window.it).toHaveBeenCalledWith('should do stuff with "c"', jasmine.any(Function));
16+
});
17+
18+
19+
it('should pass each item in an array to the handler', function() {
20+
var handlerSpy = jasmine.createSpy('handler');
21+
spyOn(window, 'it').andCallFake(function(msg, handler) {
22+
handler();
23+
});
24+
they('should do stuff with $prop', ['a', 'b', 'c'], handlerSpy);
25+
expect(handlerSpy).toHaveBeenCalledWith('a');
26+
expect(handlerSpy).toHaveBeenCalledWith('b');
27+
expect(handlerSpy).toHaveBeenCalledWith('c');
28+
});
29+
30+
31+
it('should call `it` for each key-value pair an object', function() {
32+
spyOn(window, 'it');
33+
34+
they('should do stuff with $prop', {a: 1, b:2, c:3});
35+
expect(window.it.calls.length).toEqual(3);
36+
expect(window.it).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
37+
expect(window.it).toHaveBeenCalledWith('should do stuff with "b"', jasmine.any(Function));
38+
expect(window.it).toHaveBeenCalledWith('should do stuff with "c"', jasmine.any(Function));
39+
});
40+
41+
42+
it('should pass each key-value pair in an object to the handler', function() {
43+
var handlerSpy = jasmine.createSpy('handler');
44+
spyOn(window, 'it').andCallFake(function(msg, handler) {
45+
handler();
46+
});
47+
they('should do stuff with $prop', {a: 1, b:2, c:3}, handlerSpy);
48+
expect(handlerSpy).toHaveBeenCalledWith(1);
49+
expect(handlerSpy).toHaveBeenCalledWith(2);
50+
expect(handlerSpy).toHaveBeenCalledWith(3);
51+
});
52+
53+
54+
it('should call handler with correct `this`', function() {
55+
var handlerSpy = jasmine.createSpy('handler');
56+
var dummyThis = { name: 'dummyThis' };
57+
58+
spyOn(window, 'it').andCallFake(function(msg, handler) {
59+
handler.call(dummyThis);
60+
});
61+
62+
they('should do stuff with $prop', ['a'], handlerSpy);
63+
expect(window.it).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
64+
expect(handlerSpy.mostRecentCall.object).toBe(dummyThis);
65+
});
66+
});
67+
68+
69+
describe('tthey', function() {
70+
it('should call `iit` for each item in an array', function() {
71+
spyOn(window, 'iit');
72+
73+
tthey('should do stuff with $prop', ['a', 'b', 'c']);
74+
expect(window.iit.calls.length).toEqual(3);
75+
expect(window.iit).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
76+
expect(window.iit).toHaveBeenCalledWith('should do stuff with "b"', jasmine.any(Function));
77+
expect(window.iit).toHaveBeenCalledWith('should do stuff with "c"', jasmine.any(Function));
78+
});
79+
80+
81+
it('should pass each item in an array to the handler', function() {
82+
var handlerSpy = jasmine.createSpy('handler');
83+
spyOn(window, 'iit').andCallFake(function(msg, handler) {
84+
handler();
85+
});
86+
tthey('should do stuff with $prop', ['a', 'b', 'c'], handlerSpy);
87+
expect(handlerSpy).toHaveBeenCalledWith('a');
88+
expect(handlerSpy).toHaveBeenCalledWith('b');
89+
expect(handlerSpy).toHaveBeenCalledWith('c');
90+
});
91+
92+
93+
it('should call `it` for each key-value pair an object', function() {
94+
spyOn(window, 'iit');
95+
96+
tthey('should do stuff with $prop', {a: 1, b:2, c:3});
97+
expect(window.iit.calls.length).toEqual(3);
98+
expect(window.iit).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
99+
expect(window.iit).toHaveBeenCalledWith('should do stuff with "b"', jasmine.any(Function));
100+
expect(window.iit).toHaveBeenCalledWith('should do stuff with "c"', jasmine.any(Function));
101+
});
102+
103+
104+
it('should pass each key-value pair in an object to the handler', function() {
105+
var handlerSpy = jasmine.createSpy('handler');
106+
spyOn(window, 'iit').andCallFake(function(msg, handler) {
107+
handler();
108+
});
109+
tthey('should do stuff with $prop', {a: 1, b:2, c:3}, handlerSpy);
110+
expect(handlerSpy).toHaveBeenCalledWith(1);
111+
expect(handlerSpy).toHaveBeenCalledWith(2);
112+
expect(handlerSpy).toHaveBeenCalledWith(3);
113+
});
114+
115+
116+
it('should call handler with correct `this`', function() {
117+
var handlerSpy = jasmine.createSpy('handler');
118+
var dummyThis = { name: 'dummyThis' };
119+
120+
spyOn(window, 'iit').andCallFake(function(msg, handler) {
121+
handler.call(dummyThis);
122+
});
123+
124+
tthey('should do stuff with $prop', ['a'], handlerSpy);
125+
expect(window.iit).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
126+
expect(handlerSpy.mostRecentCall.object).toBe(dummyThis);
127+
});
128+
});
129+
130+
131+
describe('xthey', function() {
132+
it('should call `xit` for each item in an array', function() {
133+
spyOn(window, 'xit');
134+
135+
xthey('should do stuff with $prop', ['a', 'b', 'c']);
136+
expect(window.xit.calls.length).toEqual(3);
137+
expect(window.xit).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
138+
expect(window.xit).toHaveBeenCalledWith('should do stuff with "b"', jasmine.any(Function));
139+
expect(window.xit).toHaveBeenCalledWith('should do stuff with "c"', jasmine.any(Function));
140+
});
141+
142+
143+
it('should pass each item in an array to the handler', function() {
144+
var handlerSpy = jasmine.createSpy('handler');
145+
spyOn(window, 'xit').andCallFake(function(msg, handler) {
146+
handler();
147+
});
148+
xthey('should do stuff with $prop', ['a', 'b', 'c'], handlerSpy);
149+
expect(handlerSpy).toHaveBeenCalledWith('a');
150+
expect(handlerSpy).toHaveBeenCalledWith('b');
151+
expect(handlerSpy).toHaveBeenCalledWith('c');
152+
});
153+
154+
155+
it('should call `it` for each key-value pair an object', function() {
156+
spyOn(window, 'xit');
157+
158+
xthey('should do stuff with $prop', {a: 1, b:2, c:3});
159+
expect(window.xit.calls.length).toEqual(3);
160+
expect(window.xit).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
161+
expect(window.xit).toHaveBeenCalledWith('should do stuff with "b"', jasmine.any(Function));
162+
expect(window.xit).toHaveBeenCalledWith('should do stuff with "c"', jasmine.any(Function));
163+
});
164+
165+
166+
it('should pass each key-value pair in an object to the handler', function() {
167+
var handlerSpy = jasmine.createSpy('handler');
168+
spyOn(window, 'xit').andCallFake(function(msg, handler) {
169+
handler();
170+
});
171+
xthey('should do stuff with $prop', {a: 1, b:2, c:3}, handlerSpy);
172+
expect(handlerSpy).toHaveBeenCalledWith(1);
173+
expect(handlerSpy).toHaveBeenCalledWith(2);
174+
expect(handlerSpy).toHaveBeenCalledWith(3);
175+
});
176+
177+
178+
it('should call handler with correct `this`', function() {
179+
var handlerSpy = jasmine.createSpy('handler');
180+
var dummyThis = { name: 'dummyThis' };
181+
182+
spyOn(window, 'xit').andCallFake(function(msg, handler) {
183+
handler.call(dummyThis);
184+
});
185+
186+
xthey('should do stuff with $prop', ['a'], handlerSpy);
187+
expect(window.xit).toHaveBeenCalledWith('should do stuff with "a"', jasmine.any(Function));
188+
expect(handlerSpy.mostRecentCall.object).toBe(dummyThis);
189+
});
190+
});
191+
});
192+
193+
4194
describe('createMockStyleSheet', function() {
5195

6196
it('should allow custom styles to be created and removed when the stylesheet is destroyed',

test/ngMessages/messagesSpec.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@ describe('ngMessages', function() {
44
beforeEach(inject.strictDi());
55
beforeEach(module('ngMessages'));
66

7-
function they(msg, vals, spec, focus) {
8-
forEach(vals, function(val, key) {
9-
var m = msg.replace('$prop', key);
10-
(focus ? iit : it)(m, function() {
11-
spec(val);
12-
});
13-
});
14-
}
15-
16-
function tthey(msg, vals, spec) {
17-
they(msg, vals, spec, true);
7+
function messageChildren(element) {
8+
return (element.length ? element[0] : element).querySelectorAll('[ng-message], [ng-message-exp]');
189
}
1910

2011
function s(str) {

0 commit comments

Comments
 (0)