]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | describe("Ext.form.action.DirectLoad", function() {\r |
2 | \r | |
3 | var action;\r | |
4 | \r | |
5 | function createAction(config, loadConfig, submitConfig) {\r | |
6 | config = config || {};\r | |
7 | if (!config.form) {\r | |
8 | config.form = {};\r | |
9 | }\r | |
10 | \r | |
11 | var loadFn = function(){\r | |
12 | \r | |
13 | }, submitFn = function(){\r | |
14 | \r | |
15 | }, cfg;\r | |
16 | \r | |
17 | loadConfig = Ext.apply({}, loadConfig);\r | |
18 | submitConfig = Ext.apply({}, submitConfig);\r | |
19 | \r | |
20 | cfg = Ext.applyIf(loadConfig, {\r | |
21 | ordered: true,\r | |
22 | len: 0\r | |
23 | });\r | |
24 | \r | |
25 | loadFn.directCfg = {\r | |
26 | method: new Ext.direct.RemotingMethod(cfg)\r | |
27 | };\r | |
28 | \r | |
29 | cfg = Ext.applyIf(submitConfig, {\r | |
30 | ordered: true,\r | |
31 | len: 0\r | |
32 | });\r | |
33 | \r | |
34 | submitFn.directCfg = {\r | |
35 | method: new Ext.direct.RemotingMethod(cfg)\r | |
36 | };\r | |
37 | \r | |
38 | TestDirect = {\r | |
39 | load: loadFn,\r | |
40 | submit: submitFn\r | |
41 | };\r | |
42 | \r | |
43 | Ext.applyIf(config.form, {\r | |
44 | clearInvalid: Ext.emptyFn,\r | |
45 | setValues: Ext.emptyFn,\r | |
46 | afterAction: Ext.emptyFn,\r | |
47 | api: {\r | |
48 | load: config.stringMethods ? 'TestDirect.load' : loadFn,\r | |
49 | submit: config.stringMethods ? 'TestDirect.submit' : submitFn\r | |
50 | }\r | |
51 | });\r | |
52 | action = new Ext.form.action.DirectLoad(config);\r | |
53 | return action;\r | |
54 | }\r | |
55 | \r | |
56 | function createActionWithCallbackArgs(config, result, trans) {\r | |
57 | createAction(config);\r | |
58 | var load = action.form.api.load;\r | |
59 | spyOn(action.form.api, 'load').andCallFake(function() {\r | |
60 | var cb = arguments[arguments.length - 2],\r | |
61 | scope = arguments[arguments.length - 1];\r | |
62 | cb.call(scope, result, trans);\r | |
63 | });\r | |
64 | action.form.api.load.directCfg = load.directCfg;\r | |
65 | }\r | |
66 | \r | |
67 | afterEach(function() {\r | |
68 | Ext.direct.Manager.clearAllMethods();\r | |
69 | \r | |
70 | try {\r | |
71 | delete window.TestDirect;\r | |
72 | } catch(e) {\r | |
73 | window.TestDirect = undefined;\r | |
74 | }\r | |
75 | \r | |
76 | if (action) {\r | |
77 | Ext.destroy(action);\r | |
78 | }\r | |
79 | });\r | |
80 | \r | |
81 | it("should be registered in the action manager under the alias 'formaction.directload'", function() {\r | |
82 | var inst = Ext.ClassManager.instantiateByAlias('formaction.directload', {});\r | |
83 | expect(inst instanceof Ext.form.action.DirectLoad).toBeTruthy();\r | |
84 | });\r | |
85 | \r | |
86 | describe("run", function() {\r | |
87 | it("should not resolve 'load' method before first invocation", function() {\r | |
88 | createAction({ stringMethods: true });\r | |
89 | \r | |
90 | expect(action.form.api.load).toBe('TestDirect.load');\r | |
91 | });\r | |
92 | \r | |
93 | it("should resolve 'load' method on first invocation", function() {\r | |
94 | createAction({ stringMethods: true });\r | |
95 | action.run();\r | |
96 | \r | |
97 | expect(Ext.isFunction(action.form.api.load)).toBeTruthy();\r | |
98 | });\r | |
99 | \r | |
100 | it("should raise an error if it cannot resolve 'load' method", function() {\r | |
101 | createAction({ stringMethods: true });\r | |
102 | TestDirect = null;\r | |
103 | \r | |
104 | var ex = 'Cannot resolve Ext Direct API method TestDirect.load';\r | |
105 | \r | |
106 | expect(function() { action.run(); }).toThrow(ex);\r | |
107 | });\r | |
108 | \r | |
109 | it("should invoke the 'load' function in the BasicForm's 'api' config", function() {\r | |
110 | createAction();\r | |
111 | var load = action.form.api.load;\r | |
112 | spyOn(action.form.api, 'load');\r | |
113 | action.form.api.load.directCfg = load.directCfg;\r | |
114 | action.run();\r | |
115 | expect(action.form.api.load).toHaveBeenCalled();\r | |
116 | });\r | |
117 | \r | |
118 | it("should pass the params as a single object argument if 'paramsAsHash' is true", function() {\r | |
119 | createAction({form: {paramsAsHash: true}, params: {foo: 'bar'}}, {\r | |
120 | len: 1\r | |
121 | });\r | |
122 | var load = action.form.api.load;\r | |
123 | spyOn(action.form.api, 'load');\r | |
124 | action.form.api.load.directCfg = load.directCfg;\r | |
125 | action.run();\r | |
126 | expect(action.form.api.load.mostRecentCall.args[0]).toEqual({foo: 'bar'});\r | |
127 | });\r | |
128 | \r | |
129 | it("should pass the param values as separate arguments in the 'paramOrder' order if specified", function() {\r | |
130 | createAction({form: {paramOrder: ['one', 'two']}, params: {one: 'foo', two: 'bar'}}, {\r | |
131 | len: 2\r | |
132 | });\r | |
133 | var load = action.form.api.load;\r | |
134 | spyOn(action.form.api, 'load');\r | |
135 | action.form.api.load.directCfg = load.directCfg;\r | |
136 | action.run();\r | |
137 | var args = action.form.api.load.mostRecentCall.args;\r | |
138 | expect(args[0]).toEqual('foo');\r | |
139 | expect(args[1]).toEqual('bar');\r | |
140 | });\r | |
141 | \r | |
142 | it("should grab params from the action's 'params' config and the BasicForm's 'baseParams' config", function() {\r | |
143 | createAction({form: {paramsAsHash: true, baseParams: {baseOne: '1', baseTwo: '2'}}, params: {one: '1', two: '2'}}, {\r | |
144 | len: 1\r | |
145 | });\r | |
146 | var load = action.form.api.load;\r | |
147 | spyOn(action.form.api, 'load');\r | |
148 | action.form.api.load.directCfg = load.directCfg;\r | |
149 | action.run();\r | |
150 | expect(action.form.api.load.mostRecentCall.args[0]).toEqual({baseOne: '1', baseTwo: '2', one: '1', two: '2'});\r | |
151 | });\r | |
152 | \r | |
153 | it("should pass the onSuccess callback function and the callback scope as the final 2 arguments", function() {\r | |
154 | createAction({form: {paramsAsHash: true}, params: {foo: 'bar'}}, {\r | |
155 | len: 1\r | |
156 | });\r | |
157 | var load = action.form.api.load;\r | |
158 | spyOn(action.form.api, 'load');\r | |
159 | action.form.api.load.directCfg = load.directCfg;\r | |
160 | action.run();\r | |
161 | var args = action.form.api.load.mostRecentCall.args;\r | |
162 | expect(typeof args[args.length - 2]).toEqual('function');\r | |
163 | expect(args[args.length - 1]).toBe(action);\r | |
164 | });\r | |
165 | \r | |
166 | describe("metadata", function() {\r | |
167 | var loadSpy;\r | |
168 | \r | |
169 | beforeEach(function() {\r | |
170 | createAction(\r | |
171 | // action\r | |
172 | {\r | |
173 | form: {\r | |
174 | metadata: { foo: 42, bar: false }\r | |
175 | }\r | |
176 | },\r | |
177 | // load fn direct config\r | |
178 | {\r | |
179 | metadata: {\r | |
180 | params: ['foo', 'bar']\r | |
181 | }\r | |
182 | }\r | |
183 | );\r | |
184 | \r | |
185 | // TODO Replace this cruft with makeSpy a la Direct proxy tests\r | |
186 | var load = action.form.api.load;\r | |
187 | \r | |
188 | loadSpy = spyOn(action.form.api, 'load');\r | |
189 | loadSpy.directCfg = load.directCfg;\r | |
190 | });\r | |
191 | \r | |
192 | it("should override form metadata with options values", function() {\r | |
193 | // Form.load(options) will apply options via Action constructor\r | |
194 | Ext.apply(action, { metadata: { foo: -1, bar: true } });\r | |
195 | \r | |
196 | action.run();\r | |
197 | \r | |
198 | expect(loadSpy.mostRecentCall.args[2]).toEqual({\r | |
199 | metadata: { foo: -1, bar: true }\r | |
200 | });\r | |
201 | });\r | |
202 | \r | |
203 | it("should default to form metadata", function() {\r | |
204 | action.run();\r | |
205 | \r | |
206 | expect(loadSpy.mostRecentCall.args[2]).toEqual({\r | |
207 | metadata: { foo: 42, bar: false }\r | |
208 | });\r | |
209 | });\r | |
210 | });\r | |
211 | });\r | |
212 | \r | |
213 | \r | |
214 | describe("load failure", function() {\r | |
215 | // effects\r | |
216 | it("should set the Action's failureType property to LOAD_FAILURE", function() {\r | |
217 | createActionWithCallbackArgs({}, {}, {});\r | |
218 | action.run();\r | |
219 | expect(action.failureType).toEqual(Ext.form.action.Action.LOAD_FAILURE);\r | |
220 | });\r | |
221 | \r | |
222 | it("should call the BasicForm's afterAction method with a false success param", function() {\r | |
223 | createActionWithCallbackArgs({}, {}, {});\r | |
224 | spyOn(action.form, 'afterAction');\r | |
225 | action.run();\r | |
226 | expect(action.form.afterAction).toHaveBeenCalledWith(action, false);\r | |
227 | });\r | |
228 | \r | |
229 | //causes\r | |
230 | it("should fail if the callback is passed an exception with type=Ext.direct.Manager.exceptions.SERVER", function() {\r | |
231 | createActionWithCallbackArgs({}, {}, {type: Ext.direct.Manager.exceptions.SERVER});\r | |
232 | action.run();\r | |
233 | expect(action.failureType).toEqual(Ext.form.action.Action.LOAD_FAILURE);\r | |
234 | });\r | |
235 | \r | |
236 | it("should fail if the result object does not have success=true", function() {\r | |
237 | createActionWithCallbackArgs({}, {success: false, data: {}}, {});\r | |
238 | action.run();\r | |
239 | expect(action.failureType).toEqual(Ext.form.action.Action.LOAD_FAILURE);\r | |
240 | });\r | |
241 | \r | |
242 | it("should fail if the result object does not have a data member", function() {\r | |
243 | createActionWithCallbackArgs({}, {success: true}, {});\r | |
244 | action.run();\r | |
245 | expect(action.failureType).toEqual(Ext.form.action.Action.LOAD_FAILURE);\r | |
246 | });\r | |
247 | });\r | |
248 | \r | |
249 | \r | |
250 | describe("load success", function() {\r | |
251 | beforeEach(function() {\r | |
252 | createActionWithCallbackArgs({}, {success: true, data: {foo: 'bar'}}, {});\r | |
253 | });\r | |
254 | \r | |
255 | it("should call the BasicForm's clearInvalid method", function() {\r | |
256 | spyOn(action.form, 'clearInvalid');\r | |
257 | action.run();\r | |
258 | expect(action.form.clearInvalid).toHaveBeenCalled();\r | |
259 | });\r | |
260 | \r | |
261 | it("should call the BasicForm's setValues method with the result data object", function() {\r | |
262 | spyOn(action.form, 'setValues');\r | |
263 | action.run();\r | |
264 | expect(action.form.setValues).toHaveBeenCalledWith({foo: 'bar'});\r | |
265 | });\r | |
266 | \r | |
267 | it("should invoke the BasicForm's afterAction method with a true success param", function() {\r | |
268 | spyOn(action.form, 'afterAction');\r | |
269 | action.run();\r | |
270 | expect(action.form.afterAction).toHaveBeenCalledWith(action, true);\r | |
271 | });\r | |
272 | });\r | |
273 | \r | |
274 | });\r |