]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | describe("Ext.data.operation.Operation", function() {\r |
2 | \r | |
3 | var op;\r | |
4 | \r | |
5 | function makeOperation(cfg) {\r | |
6 | // Create an instance of the abstract class, since it can still\r | |
7 | // operate as long as we don't execute it.\r | |
8 | op = new Ext.data.operation.Operation(cfg);\r | |
9 | }\r | |
10 | \r | |
11 | afterEach(function() {\r | |
12 | op = null;\r | |
13 | });\r | |
14 | \r | |
15 | describe("state", function() {\r | |
16 | describe("initial state", function() {\r | |
17 | beforeEach(function() {\r | |
18 | makeOperation();\r | |
19 | }); \r | |
20 | \r | |
21 | it("should not be started", function() {\r | |
22 | expect(op.isStarted()).toBe(false);\r | |
23 | });\r | |
24 | \r | |
25 | it("should not be running", function() {\r | |
26 | expect(op.isRunning()).toBe(false); \r | |
27 | });\r | |
28 | \r | |
29 | it("should not be complete", function() {\r | |
30 | expect(op.isComplete()).toBe(false); \r | |
31 | });\r | |
32 | \r | |
33 | it("should not be successful", function() {\r | |
34 | expect(op.wasSuccessful()).toBe(false); \r | |
35 | });\r | |
36 | \r | |
37 | it("should have no error", function() {\r | |
38 | expect(op.getError()).toBeUndefined(); \r | |
39 | });\r | |
40 | \r | |
41 | it("should have no exception", function() {\r | |
42 | expect(op.hasException()).toBe(false); \r | |
43 | });\r | |
44 | }); \r | |
45 | \r | |
46 | describe("starting", function() {\r | |
47 | beforeEach(function() {\r | |
48 | makeOperation();\r | |
49 | op.execute();\r | |
50 | }); \r | |
51 | \r | |
52 | it("should be started", function() {\r | |
53 | expect(op.isStarted()).toBe(true);\r | |
54 | });\r | |
55 | \r | |
56 | it("should be running", function() {\r | |
57 | expect(op.isRunning()).toBe(true); \r | |
58 | });\r | |
59 | \r | |
60 | it("should not be complete", function() {\r | |
61 | expect(op.isComplete()).toBe(false); \r | |
62 | });\r | |
63 | \r | |
64 | it("should not be successful", function() {\r | |
65 | expect(op.wasSuccessful()).toBe(false); \r | |
66 | });\r | |
67 | \r | |
68 | it("should have no error", function() {\r | |
69 | expect(op.getError()).toBeUndefined(); \r | |
70 | });\r | |
71 | \r | |
72 | it("should have no exception", function() {\r | |
73 | expect(op.hasException()).toBe(false); \r | |
74 | });\r | |
75 | });\r | |
76 | \r | |
77 | describe("completing successfully", function() {\r | |
78 | beforeEach(function() {\r | |
79 | makeOperation();\r | |
80 | op.execute();\r | |
81 | op.setSuccessful(true); \r | |
82 | });\r | |
83 | \r | |
84 | it("should be started", function() {\r | |
85 | expect(op.isStarted()).toBe(true);\r | |
86 | });\r | |
87 | \r | |
88 | it("should not be running", function() {\r | |
89 | expect(op.isRunning()).toBe(false); \r | |
90 | });\r | |
91 | \r | |
92 | it("should be complete", function() {\r | |
93 | expect(op.isComplete()).toBe(true); \r | |
94 | });\r | |
95 | \r | |
96 | it("should be successful", function() {\r | |
97 | expect(op.wasSuccessful()).toBe(true); \r | |
98 | });\r | |
99 | \r | |
100 | it("should have no error", function() {\r | |
101 | expect(op.getError()).toBeUndefined(); \r | |
102 | });\r | |
103 | \r | |
104 | it("should have no exception", function() {\r | |
105 | expect(op.hasException()).toBe(false); \r | |
106 | });\r | |
107 | });\r | |
108 | \r | |
109 | describe("completing with failure", function() {\r | |
110 | beforeEach(function() {\r | |
111 | makeOperation();\r | |
112 | op.execute();\r | |
113 | op.setException('Failed'); \r | |
114 | });\r | |
115 | \r | |
116 | it("should be started", function() {\r | |
117 | expect(op.isStarted()).toBe(true);\r | |
118 | });\r | |
119 | \r | |
120 | it("should not be running", function() {\r | |
121 | expect(op.isRunning()).toBe(false); \r | |
122 | });\r | |
123 | \r | |
124 | it("should be complete", function() {\r | |
125 | expect(op.isComplete()).toBe(true); \r | |
126 | });\r | |
127 | \r | |
128 | it("should not be successful", function() {\r | |
129 | expect(op.wasSuccessful()).toBe(false); \r | |
130 | });\r | |
131 | \r | |
132 | it("should have the passed error", function() {\r | |
133 | expect(op.getError()).toBe('Failed'); \r | |
134 | });\r | |
135 | \r | |
136 | it("should have an exception", function() {\r | |
137 | expect(op.hasException()).toBe(true); \r | |
138 | }); \r | |
139 | });\r | |
140 | \r | |
141 | describe("completeOperation", function() {\r | |
142 | var proxy;\r | |
143 | \r | |
144 | beforeEach(function() {\r | |
145 | proxy = {\r | |
146 | completeOperation: jasmine.createSpy('completeOperation')\r | |
147 | };\r | |
148 | });\r | |
149 | \r | |
150 | afterEach(function() {\r | |
151 | proxy = null;\r | |
152 | });\r | |
153 | \r | |
154 | it("should be called when completed successfully", function() {\r | |
155 | makeOperation({ proxy: proxy });\r | |
156 | \r | |
157 | op.execute();\r | |
158 | op.setSuccessful(true);\r | |
159 | \r | |
160 | expect(proxy.completeOperation).toHaveBeenCalledWith(op);\r | |
161 | });\r | |
162 | \r | |
163 | it("should be called when failed", function() {\r | |
164 | makeOperation({ proxy: proxy });\r | |
165 | \r | |
166 | op.execute();\r | |
167 | op.setException('Fail!');\r | |
168 | \r | |
169 | expect(proxy.completeOperation).toHaveBeenCalledWith(op);\r | |
170 | });\r | |
171 | });\r | |
172 | });\r | |
173 | \r | |
174 | describe("aborting", function() {\r | |
175 | var proxy, request;\r | |
176 | \r | |
177 | beforeEach(function() {\r | |
178 | proxy = new Ext.data.proxy.Proxy();\r | |
179 | makeOperation({\r | |
180 | proxy: proxy\r | |
181 | });\r | |
182 | op.doExecute = function() {\r | |
183 | request = new Ext.data.Request();\r | |
184 | return request; \r | |
185 | };\r | |
186 | spyOn(proxy, 'abort');\r | |
187 | spyOn(proxy, 'completeOperation');\r | |
188 | });\r | |
189 | \r | |
190 | afterEach(function() {\r | |
191 | proxy = request = null;\r | |
192 | });\r | |
193 | \r | |
194 | it("should not call if the operation has not started", function() {\r | |
195 | op.abort();\r | |
196 | expect(proxy.abort).not.toHaveBeenCalled();\r | |
197 | });\r | |
198 | \r | |
199 | it("should not call if the operation has been completed", function() {\r | |
200 | op.execute();\r | |
201 | op.setSuccessful(true);\r | |
202 | op.abort();\r | |
203 | expect(proxy.abort).not.toHaveBeenCalled();\r | |
204 | });\r | |
205 | \r | |
206 | it("should pass the request for this operation to abort", function() {\r | |
207 | op.execute();\r | |
208 | op.abort();\r | |
209 | expect(proxy.abort).toHaveBeenCalledWith(request); \r | |
210 | });\r | |
211 | \r | |
212 | it("should not call completeOperation", function() {\r | |
213 | op.execute();\r | |
214 | op.abort();\r | |
215 | \r | |
216 | expect(proxy.completeOperation).not.toHaveBeenCalled();\r | |
217 | });\r | |
218 | });\r | |
219 | \r | |
220 | describe("callbacks", function() {\r | |
221 | it("should trigger when setting completed", function() {\r | |
222 | var called = false;\r | |
223 | makeOperation({\r | |
224 | callback: function() {\r | |
225 | called = true;\r | |
226 | }\r | |
227 | });\r | |
228 | op.execute();\r | |
229 | op.setSuccessful(true);\r | |
230 | expect(called).toBe(true);\r | |
231 | });\r | |
232 | \r | |
233 | it("should trigger when setting an exception", function() {\r | |
234 | var called = false;\r | |
235 | makeOperation({\r | |
236 | callback: function() {\r | |
237 | called = true;\r | |
238 | }\r | |
239 | });\r | |
240 | op.execute();\r | |
241 | op.setException('Failed');\r | |
242 | expect(called).toBe(true);\r | |
243 | });\r | |
244 | \r | |
245 | it("should default the scope to the operation", function() {\r | |
246 | var scope;\r | |
247 | makeOperation({\r | |
248 | callback: function() {\r | |
249 | scope = this;\r | |
250 | }\r | |
251 | });\r | |
252 | op.execute();\r | |
253 | op.setSuccessful(true);\r | |
254 | expect(scope).toBe(op);\r | |
255 | });\r | |
256 | \r | |
257 | it("should use a passed scope", function() {\r | |
258 | var o = {},\r | |
259 | scope;\r | |
260 | \r | |
261 | makeOperation({\r | |
262 | scope: o,\r | |
263 | callback: function() {\r | |
264 | scope = this;\r | |
265 | }\r | |
266 | });\r | |
267 | op.execute();\r | |
268 | op.setSuccessful(true);\r | |
269 | expect(scope).toBe(o);\r | |
270 | });\r | |
271 | \r | |
272 | it("should pass the records, operation and success state", function() {\r | |
273 | var callback = jasmine.createSpy();\r | |
274 | makeOperation({\r | |
275 | callback: callback\r | |
276 | }); \r | |
277 | op.execute();\r | |
278 | op.setSuccessful(true);\r | |
279 | expect(callback).toHaveBeenCalledWith(op.getRecords(), op, true);\r | |
280 | });\r | |
281 | });\r | |
282 | \r | |
283 | describe("process", function() {\r | |
284 | var empty = Ext.data.reader.Reader.prototype.nullResultSet,\r | |
285 | response, request, resultSet, Model;\r | |
286 | \r | |
287 | beforeEach(function() {\r | |
288 | Model = Ext.define(null, {\r | |
289 | extend: 'Ext.data.Model',\r | |
290 | fields: ['id']\r | |
291 | });\r | |
292 | \r | |
293 | response = {};\r | |
294 | request = new Ext.data.Request();\r | |
295 | makeOperation();\r | |
296 | op.setRecords([new Model()]);\r | |
297 | });\r | |
298 | \r | |
299 | afterEach(function() {\r | |
300 | Model = response = request = resultSet = null; \r | |
301 | });\r | |
302 | \r | |
303 | it("should set the resultSet", function() {\r | |
304 | op.process(empty, request, response);\r | |
305 | expect(op.getResultSet()).toBe(empty);\r | |
306 | });\r | |
307 | \r | |
308 | it("should set the response", function() {\r | |
309 | op.process(empty, request, response);\r | |
310 | expect(op.getResponse()).toBe(response);\r | |
311 | });\r | |
312 | \r | |
313 | describe("result set with success: false", function() {\r | |
314 | it("should set an exception ", function() {\r | |
315 | op.process(new Ext.data.ResultSet({\r | |
316 | success: false\r | |
317 | }), request, response); \r | |
318 | expect(op.hasException()).toBe(true);\r | |
319 | expect(op.wasSuccessful()).toBe(false);\r | |
320 | });\r | |
321 | \r | |
322 | it("should set the error to the message returned by the result set", function() {\r | |
323 | op.process(new Ext.data.ResultSet({\r | |
324 | success: false,\r | |
325 | message: 'Failed'\r | |
326 | }), request, response); \r | |
327 | expect(op.getError()).toBe('Failed');\r | |
328 | });\r | |
329 | });\r | |
330 | \r | |
331 | describe("result set with success: true", function() {\r | |
332 | it("should set success if the result set is successful", function() {\r | |
333 | op.process(empty, request, response);\r | |
334 | expect(op.wasSuccessful()).toBe(true); \r | |
335 | });\r | |
336 | \r | |
337 | it("should call doProcess", function() {\r | |
338 | spyOn(op, 'doProcess');\r | |
339 | op.process(empty, request, response);\r | |
340 | expect(op.doProcess).toHaveBeenCalledWith(empty, request, response); \r | |
341 | });\r | |
342 | });\r | |
343 | });\r | |
344 | \r | |
345 | describe("retrying an operation", function() {\r | |
346 | beforeEach(function() {\r | |
347 | makeOperation();\r | |
348 | op.doExecute = function() {\r | |
349 | return new Ext.data.Request();\r | |
350 | };\r | |
351 | op.setException('Err');\r | |
352 | op.execute();\r | |
353 | }); \r | |
354 | \r | |
355 | it("should clear any error", function() {\r | |
356 | expect(op.getError()).toBeUndefined();\r | |
357 | }); \r | |
358 | \r | |
359 | it("should clear the success flag", function() {\r | |
360 | expect(op.wasSuccessful()).toBe(false);\r | |
361 | }); \r | |
362 | \r | |
363 | it("should clear the complete flag", function() {\r | |
364 | expect(op.isComplete()).toBe(false); \r | |
365 | });\r | |
366 | \r | |
367 | it("should clear the exception flag", function() {\r | |
368 | expect(op.hasException()).toBe(false); \r | |
369 | });\r | |
370 | });\r | |
371 | \r | |
372 | });\r |