-
Notifications
You must be signed in to change notification settings - Fork 14
/
onLoad-spec.js
274 lines (206 loc) · 8.43 KB
/
onLoad-spec.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
'use strict';
var HttpBackend = require('../lib/http-backend-proxy');
var Browser = require('./helpers/protractor-browser')
var PROMISE = "PROMISE"
describe('onLoad configuration', function() {
var browser, browser_get;
beforeEach(function() {
browser = {
get: function() { return PROMISE; },
addMockModule: function() {},
removeMockModule: function() {},
executeScript: function() {}
};
spyOn(browser, 'get').andCallThrough();
spyOn(browser, 'addMockModule');
spyOn(browser, 'executeScript');
//Needed because onLoad will replace browser.get this its own implementation.
//But we need to test that the original still gets called in the end.
browser_get = browser.get;
});
describe('The proxy object', function() {
var proxy;
beforeEach(function() {
proxy = new HttpBackend(browser);
});
it('should not support a reset() function', function() {
expect(proxy.reset).toBeUndefined();
});
});
describe('The onLoad object', function() {
var onLoad;
beforeEach(function() {
var proxy = new HttpBackend(browser);
onLoad = proxy.onLoad
});
it('should not have a flush() function', function() {
expect(onLoad.flush).toBeUndefined();
});
it('should not have a syncContext() function', function() {
expect(onLoad.syncContext).toBeUndefined();
});
it('should not have an onLoad object', function() {
expect(onLoad.onLoad).toBeUndefined();
});
it('should not initialize a context object', function() {
expect(onLoad.context).toBeUndefined();
});
});
describe('A proxy where no onLoad calls were made', function() {
var proxy, getResult;
beforeEach(function() {
proxy = new HttpBackend(browser);
var onLoad = proxy.onLoad;
getResult = browser.get('index.html');
});
it('should make no addMockModule calls', function() {
expect(browser.addMockModule).not.toHaveBeenCalled();
});
it('should not patch the browser.get() method', function() {
expect(browser.get).toEqual(browser_get);
});
it('should call get once.', function() {
expect(browser_get.calls.length).toEqual(1);
});
it('should call get with the passed url.', function() {
expect(browser_get.calls[0].args[0]).toEqual('index.html');
});
it('should return the promise returned by get.', function() {
expect(getResult).toEqual(PROMISE);
});
});
describe('A proxy where onLoad calls were made', function() {
var proxy, getResult;
beforeEach(function() {
proxy = new HttpBackend(browser);
proxy.onLoad.whenGET('/session-info').passThrough();
proxy.onLoad.whenGET('/preferences').passThrough();
getResult = browser.get('index.html');
});
it('should call addMockModule once', function() {
expect(browser.addMockModule.calls.length).toEqual(1);
});
it('should call addMockModule with a script that adds a module that uses ngMockE2E', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'angular.module("http-backend-proxy", ["ngMockE2E"]).run(["$httpBackend", function($httpBackend){');
});
it('should call addMockModule with a script that contains all the calls made to onLoad', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/session-info").passThrough();');
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/preferences").passThrough();');
});
it('should patch the browser.get() method', function() {
expect(browser.get).not.toEqual(browser_get);
});
it('should call get once.', function() {
expect(browser_get.calls.length).toEqual(1);
});
it('should call get with the passed url.', function() {
expect(browser_get.calls[0].args[0]).toEqual('index.html');
});
it('should return the promise returned by get.', function() {
expect(getResult).toEqual(PROMISE);
});
it('should not call executeScript.', function() {
expect(browser.executeScript).not.toHaveBeenCalled();
});
describe('adding additional calls before subsequent page loads', function() {
beforeEach(function() {
proxy.onLoad.whenGET('/more-info').passThrough();
browser.addMockModule.reset();
browser.get('index.html');
});
it('should include all the previous calls in the onLoad script', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/session-info").passThrough();');
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/preferences").passThrough();');
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/more-info").passThrough();');
});
it('should add the new call to the onLoad script', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/more-info").passThrough();');
});
});
describe('calling onLoad.reset()', function() {
beforeEach(function() {
spyOn(browser, 'removeMockModule');
proxy.onLoad.reset();
});
it('should remove proxy module from the browser.', function() {
expect(browser.removeMockModule).toHaveBeenCalledWith('http-backend-proxy');
});
it('should remove the browser.get() patch', function() {
expect(browser.get).toEqual(browser_get);
});
it('should cause addMockModule to again not be called when browser.get is invoked.', function() {
browser.addMockModule.reset();
browser.get();
expect(browser.addMockModule).not.toHaveBeenCalled();
});
});
describe('and a context exists', function() {
beforeEach(function() {
proxy.context = 'proxy context'
browser.addMockModule.reset();
browser.get('index.html');
});
it('should call addMockModule once', function() {
expect(browser.addMockModule.calls.length).toEqual(1);
});
it('should call addMockModule with a script that contains all the calls made to onLoad', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/session-info").passThrough();');
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.whenGET("/preferences").passThrough();');
});
it('should call addMockModule with a script that sets the context on $httpBackend', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.context="proxy context";');
});
describe('applying a context to onLoad', function() {
beforeEach(function() {
proxy.onLoad.context = 'loader context';
browser.addMockModule.reset();
browser.get('index.html');
});
it('should not change the proxy context', function() {
expect(proxy.context).toEqual('proxy context')
});
it('should use the proxy context when calling addMockModule', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.context="proxy context";');
});
});
describe('changing the proxy context before subsequent page loads', function() {
beforeEach(function() {
proxy.context = 'new context';
browser.addMockModule.reset();
browser.get('index.html');
});
it('should use the new context when calling addMockModule', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.context="new context";');
});
});
});
});
describe('A proxy with an alternate context field name', function() {
var proxy;
beforeEach(function() {
proxy = new HttpBackend(browser, { contextField: 'alternate' });
proxy.alternate = 'proxy context';
proxy.onLoad.whenGET(/.*/).passThrough();
browser.get('index.html');
});
it('should call addMockModule once', function() {
expect(browser.addMockModule.calls.length).toEqual(1);
});
it('should call addMockModule with a script that uses the alternate field name to the context on $httpBackend', function() {
expect(browser.addMockModule.calls[0].args[1]).toContain(
'$httpBackend.alternate="proxy context";');
});
});
});