]> git.proxmox.com Git - extjs.git/blame - extjs/classic/classic/test/specs/form/action/Load.js
add extjs 6.0.1 sources
[extjs.git] / extjs / classic / classic / test / specs / form / action / Load.js
CommitLineData
6527f429
DM
1describe("Ext.form.action.Load", function() {\r
2\r
3 var action;\r
4\r
5 function createAction(config) {\r
6 config = config || {};\r
7 if (!config.form) {\r
8 config.form = {};\r
9 }\r
10 action = new Ext.form.action.Load(config);\r
11 }\r
12\r
13 afterEach(function() {\r
14 action = undefined;\r
15 });\r
16\r
17\r
18\r
19 it("should be registered in the action manager under the alias 'formaction.load'", function() {\r
20 var inst = Ext.ClassManager.instantiateByAlias('formaction.load', {});\r
21 expect(inst instanceof Ext.form.action.Load).toBeTruthy();\r
22 });\r
23\r
24 describe("AJAX call parameters", function() {\r
25 var ajaxRequestCfg;\r
26\r
27 beforeEach(function() {\r
28 spyOn(Ext.Ajax, 'request').andCallFake(function() {\r
29 // store what was passed to the request call for later inspection\r
30 expect(arguments.length).toEqual(1);\r
31 ajaxRequestCfg = arguments[0];\r
32 });\r
33 });\r
34\r
35 it("should invoke Ext.Ajax.request", function() {\r
36 createAction();\r
37 action.run();\r
38 expect(Ext.Ajax.request).toHaveBeenCalled();\r
39 });\r
40\r
41 it("should use 'POST' as the ajax call method by default", function() {\r
42 createAction();\r
43 action.run();\r
44 expect(ajaxRequestCfg.method).toEqual('POST');\r
45 });\r
46\r
47 it("should use the BasicForm's 'method' config as the ajax call method if specified", function() {\r
48 createAction({ form: {method: 'FORMMETHOD'} });\r
49 action.run();\r
50 expect(ajaxRequestCfg.method).toEqual('FORMMETHOD');\r
51 });\r
52\r
53 it("should use the Action's 'method' config as the ajax call method if specified", function() {\r
54 createAction({ method: 'actionmethod' });\r
55 action.run();\r
56 expect(ajaxRequestCfg.method).toEqual('ACTIONMETHOD');\r
57 });\r
58\r
59 it("should use the BasicForm's 'url' config as the ajax call url if specified", function() {\r
60 createAction({ form: {url: '/url-from-form'} });\r
61 action.run();\r
62 expect(ajaxRequestCfg.url).toEqual('/url-from-form');\r
63 });\r
64\r
65 it("should use the Action's 'url' config as the ajax call url if specified", function() {\r
66 createAction({ url: '/url-from-action' });\r
67 action.run();\r
68 expect(ajaxRequestCfg.url).toEqual('/url-from-action');\r
69 });\r
70\r
71 it("should use the Action's 'headers' config as the ajax call headers if specified", function() {\r
72 var headers = {foo: 'bar'};\r
73 createAction({ headers: headers });\r
74 action.run();\r
75 expect(ajaxRequestCfg.headers).toBe(headers);\r
76 });\r
77\r
78 it("should default to sending no params to the ajax call", function() {\r
79 createAction();\r
80 action.run();\r
81 expect(ajaxRequestCfg.params).toEqual({});\r
82 });\r
83\r
84 it("should add the BasicForm's 'baseParams' config to the ajax call params if specified", function() {\r
85 var params = {one: '1', two: '2'};\r
86 createAction({ form: {baseParams: params} });\r
87 action.run();\r
88 expect(ajaxRequestCfg.params).toEqual(params);\r
89 });\r
90\r
91 it("should use the Action's 'params' config for the ajax call params if specfied (as an Object)", function() {\r
92 var params = {one: '1', two: '2'};\r
93 createAction({ params: params });\r
94 action.run();\r
95 expect(ajaxRequestCfg.params).toEqual(params);\r
96 });\r
97\r
98 it("should use the Action's 'params' config for the ajax call params if specfied (as a String)", function() {\r
99 var params = 'one=1&two=2';\r
100 createAction({ params: params });\r
101 action.run();\r
102 expect(ajaxRequestCfg.params).toEqual({one: '1', two: '2'});\r
103 });\r
104\r
105 it("should concatenate the Action's 'params' config (as an Object) with the BasicForm's 'baseParams' config", function() {\r
106 createAction({ params: {one: '1', two: '2'}, form: {baseParams: {three: '3', four: '4'} } });\r
107 action.run();\r
108 expect(ajaxRequestCfg.params).toEqual({one: '1', two: '2', three: '3', four: '4'});\r
109 });\r
110\r
111 it("should concatenate the Action's 'params' config (as a String) with the BasicForm's 'baseParams' config", function() {\r
112 createAction({ params: 'one=1&two=2', form: {baseParams: {three: '3', four: '4'} } });\r
113 action.run();\r
114 expect(ajaxRequestCfg.params).toEqual({one: '1', two: '2', three: '3', four: '4'});\r
115 });\r
116\r
117 it("should use the BasicForm's 'timeout' config as the ajax call timeout if specified", function() {\r
118 createAction({ form: {timeout: 123} });\r
119 action.run();\r
120 expect(ajaxRequestCfg.timeout).toEqual(123000);\r
121 });\r
122\r
123 it("should use the Action's 'timeout' config as the ajax call timeout if specified", function() {\r
124 createAction({ timeout: 123 });\r
125 action.run();\r
126 expect(ajaxRequestCfg.timeout).toEqual(123000);\r
127 });\r
128\r
129 it("should use the Action instance as the ajax call 'scope' parameter", function() {\r
130 createAction();\r
131 action.run();\r
132 expect(ajaxRequestCfg.scope).toBe(action);\r
133 });\r
134 });\r
135\r
136\r
137 describe("ajax request error", function() {\r
138 var wantResponse = { responseText: '{}' };\r
139\r
140 function run(response, form) {\r
141 response = response || wantResponse;\r
142 \r
143 spyOn(Ext.Ajax, 'request').andCallFake(function(config) {\r
144 // call the configured failure handler\r
145 config.failure.call(config.scope, response);\r
146 });\r
147 createAction({\r
148 form: Ext.apply({\r
149 afterAction: jasmine.createSpy('afterAction')\r
150 }, form)\r
151 });\r
152 action.run();\r
153 }\r
154\r
155 it("should set the Action's failureType property to CONNECT_FAILURE", function() {\r
156 run();\r
157 \r
158 expect(action.failureType).toEqual(Ext.form.action.Action.CONNECT_FAILURE);\r
159 });\r
160\r
161 it("should set the Action's response property to the ajax response", function() {\r
162 run();\r
163 \r
164 expect(action.response).toEqual(wantResponse);\r
165 });\r
166\r
167 it("should call the BasicForm's afterAction method with a false success param", function() {\r
168 run();\r
169 \r
170 expect(action.form.afterAction).toHaveBeenCalledWith(action, false);\r
171 });\r
172 \r
173 it("should not call afterAction if the form is destroying", function() {\r
174 run(null, { destroying: true });\r
175 \r
176 expect(action.form.afterAction).not.toHaveBeenCalled();\r
177 });\r
178 \r
179 it("should not call afterAction if the form is already destroyed", function() {\r
180 run(null, { destroyed: true });\r
181 \r
182 expect(action.form.afterAction).not.toHaveBeenCalled();\r
183 });\r
184 });\r
185 \r
186 describe("load failure", function() {\r
187\r
188 function run(response, form) {\r
189 spyOn(Ext.Ajax, 'request').andCallFake(function(config) {\r
190 // manually call the configured success handler\r
191 config.success.call(config.scope, response);\r
192 });\r
193 createAction({\r
194 form: Ext.apply({\r
195 afterAction: jasmine.createSpy('afterAction')\r
196 }, form)\r
197 });\r
198 action.run();\r
199 }\r
200\r
201 // effects\r
202 it("should set the Action's failureType property to LOAD_FAILURE", function() {\r
203 run({});\r
204 expect(action.failureType).toEqual(Ext.form.action.Action.LOAD_FAILURE);\r
205 });\r
206 it("should call the BasicForm's afterAction method with a false success param", function() {\r
207 run({});\r
208 expect(action.form.afterAction).toHaveBeenCalledWith(action, false);\r
209 });\r
210\r
211 // causes\r
212 it("should fail if either the responseText or responseXML are populated", function() {\r
213 run({});\r
214 expect(action.failureType).toBeDefined();\r
215 });\r
216 it("should require the result object to have success=true", function() {\r
217 run({responseText: '{"success":false, "data":{}}'});\r
218 expect(action.failureType).toBeDefined();\r
219 });\r
220 it("should require the result object to have a data property", function() {\r
221 run({responseText: '{"success":true}'});\r
222 expect(action.failureType).toBeDefined();\r
223 });\r
224 \r
225 it("should not call afterAction if the form is destroying", function() {\r
226 run({}, { destroying: true });\r
227 \r
228 expect(action.form.afterAction).not.toHaveBeenCalled();\r
229 });\r
230 \r
231 it("should not call afterAction if the form is already destroyed", function() {\r
232 run({}, { destroyed: true });\r
233 \r
234 expect(action.form.afterAction).not.toHaveBeenCalled();\r
235 });\r
236 });\r
237\r
238\r
239 describe("load success", function() {\r
240 function run(response, reader, form) {\r
241 spyOn(Ext.Ajax, 'request').andCallFake(function(config) {\r
242 // manually call the configured success handler\r
243 config.success.call(config.scope, response);\r
244 });\r
245 createAction({\r
246 form: Ext.apply({\r
247 reader: reader,\r
248 clearInvalid: jasmine.createSpy(),\r
249 setValues: jasmine.createSpy(),\r
250 afterAction: jasmine.createSpy('afterAction')\r
251 }, form)\r
252 });\r
253 action.run();\r
254 }\r
255\r
256 it("should call the BasicForm's clearInvalid method", function() {\r
257 run({responseText: '{"success":true,"data":{"from":"responseText"}}'});\r
258 expect(action.form.clearInvalid).toHaveBeenCalled();\r
259 });\r
260\r
261 it("should call the BasicForm's setValues method", function() {\r
262 run({responseText: '{"success":true,"data":{"from":"responseText"}}'});\r
263 expect(action.form.setValues).toHaveBeenCalled();\r
264 });\r
265\r
266 it("should invoke the BasicForm's afterAction method with a true success param", function() {\r
267 run({responseText: '{"success":true,"data":{"from":"responseText"}}'});\r
268 expect(action.form.afterAction).toHaveBeenCalledWith(action, true);\r
269 });\r
270\r
271 it("should parse the responseText as JSON", function() {\r
272 run({responseText: '{"success":true,"data":{"from":"responseText"}}'});\r
273 expect(action.form.setValues).toHaveBeenCalledWith({from: "responseText"});\r
274 });\r
275\r
276 it("should use the BasicForm's configured Reader to parse the response if present", function() {\r
277 var response = {responseText: '{}'};\r
278 run(response, {\r
279 read: jasmine.createSpy().andReturn({\r
280 success: true,\r
281 records: [\r
282 {data: {from: 'reader'}}\r
283 ]\r
284 })\r
285 });\r
286 expect(action.form.reader.read).toHaveBeenCalledWith(response);\r
287 expect(action.form.setValues).toHaveBeenCalledWith({from: "reader"});\r
288 });\r
289 \r
290 it("should not call afterAction if the form is destroying", function() {\r
291 run({responseText: '{"success":true,"data":{"from":"responseText"}}'}, undefined, { destroying: true });\r
292 \r
293 expect(action.form.afterAction).not.toHaveBeenCalled();\r
294 });\r
295 \r
296 it("should not call afterAction if the form is already destroyed", function() {\r
297 run({responseText: '{"success":true,"data":{"from":"responseText"}}'}, undefined, { destroyed: true });\r
298 \r
299 expect(action.form.afterAction).not.toHaveBeenCalled();\r
300 });\r
301 });\r
302});\r