]>
git.proxmox.com Git - extjs.git/blob - extjs/packages/core/test/specs/direct/RemotingProvider.js
1 describe("Ext.direct.RemotingProvider", function() {
2 var RP
= Ext
.direct
.RemotingProvider
,
18 name
: 'directMetaNamed',
25 name
: 'directMetaOrdered',
30 name
: 'directMetaFormNamed',
37 name
: 'directMetaFormOrdered',
49 "TestAction.Foo.Bar": [{
54 "TestAction.Foo.Bar.Baz": [{
59 "TestAction.Foo.Qux": [{
65 namespace: "Direct.foo.bar",
72 echo: function(value
) {
76 directFail: function(value
) {
82 directForm: function(form
) {
89 directMetaNamed: function(data
, metadata
) {
96 directMetaOrdered: function(data
, metadata
) {
103 directMetaFormNamed: function(form
, metadata
) {
111 directMetaFormOrdered: function(form
, metadata
) {
136 // This simulation stub is *asynchronous*
137 function simulateDirectRequest(options
) {
138 var callback
= options
.callback
,
139 scope
= options
.scope
,
140 transaction
= options
.transaction
,
141 isForm
= options
.form
!== undefined,
142 isUpload
= options
.isUpload
,
144 data
, tid
, action
, method
, arg
, fn
, success
,
145 result
, response
, xhr
, opt
, metadata
;
148 data
= options
.params
;
150 action
= data
.extAction
;
151 method
= data
.extMethod
;
152 metadata
= data
.extMetadata
;
154 // Collect the input field values
155 Ext
.fly(options
.form
).select('input').each(function(el
, c
, idx
) {
156 this[el
.dom
.name
] = el
.dom
.value
;
161 data
= options
.jsonData
;
163 action
= data
.action
;
164 method
= data
.method
;
165 arg
= data
.data
|| [];
166 metadata
= data
.metadata
;
169 fn
= directMethods
[method
];
171 // TODO Come up with something less hacky
172 if (/^directMeta/.test(method
)) {
176 if (options
.timeout
=== 666) {
180 message
: "Can't connect to the server"
187 result
= fn
.apply({}, arg
);
197 // Direct exception handling here
201 message
: e
.toString(),
206 // Success only means *communication* success
211 responseText
: Ext
.encode(response
)
215 transaction
: transaction
218 Ext
.callback(callback
, scope
, [opt
, success
, xhr
], 1);
221 beforeEach(function() {
222 provider
= new RP(api
);
225 afterEach(function() {
230 Ext
.direct
.Manager
.clearAllMethods();
235 delete window
.Direct
;
238 window
.Direct
= undefined;
242 describe("handles namespaces:", function() {
245 it("creates namespace for itself if passed a string", function() {
246 expect(Direct
.foo
.bar
).toBeDefined();
249 it("doesn't create nested objects until it's connected", function() {
250 expect(Direct
.foo
.bar
).toEqual({});
253 describe("creates nested namespaces after it's connected:", function() {
254 beforeEach(function() {
259 it("creates TestAction", function() {
260 expect(ns
.TestAction
).toBeDefined();
263 it("creates TestAction.Foo", function() {
264 expect(ns
.TestAction
.Foo
).toBeDefined();
267 it("creates TestAction.Foo.Bar", function() {
268 expect(ns
.TestAction
.Foo
.Bar
).toBeDefined();
271 it("creates TestAction.Foo.Bar.Baz", function() {
272 expect(ns
.TestAction
.Foo
.Bar
.Baz
).toBeDefined();
275 it("creates TestAction.Foo.Qux", function() {
276 expect(ns
.TestAction
.Foo
.Qux
).toBeDefined();
280 describe("handles nested namespaces the old way:", function() {
281 beforeEach(function() {
282 provider
.disableNestedActions
= true;
287 it("creates TestAction", function() {
288 expect(ns
.TestAction
).toBeDefined();
291 it("creates TestAction.Foo", function() {
292 expect(ns
['TestAction.Foo']).toBeDefined();
294 expect(ns
.TestAction
.Foo
).not
.toBeDefined();
297 it("creates TestAction.Foo.Bar", function() {
298 expect(ns
['TestAction.Foo.Bar']).toBeDefined();
301 it("creates TestAction.Foo.Bar.Baz", function() {
302 expect(ns
['TestAction.Foo.Bar.Baz']).toBeDefined();
305 it("creates TestAction.Foo.Qux", function() {
306 expect(ns
['TestAction.Foo.Qux']).toBeDefined();
311 describe("handles remoting methods:", function() {
314 function checkFn(fn
) {
315 expect( Ext
.isFunction(fn
) ).toBeTruthy();
318 beforeEach(function() {
323 it("has Foo.foo", function() {
324 checkFn(ns
.TestAction
.Foo
.foo
);
327 it("has Foo.Bar.bar", function() {
328 checkFn(ns
.TestAction
.Foo
.Bar
.bar
);
331 it("has Foo.Bar.Baz.baz", function() {
332 checkFn(ns
.TestAction
.Foo
.Bar
.Baz
.baz
);
335 it("has Foo.Qux.qux", function() {
336 checkFn(ns
.TestAction
.Foo
.Qux
.qux
);
340 describe("runs remoting methods:", function() {
341 var ns
, echo
, options
, handler
;
343 function echoStatus(result
, event
) {
344 this.echo
= event
.status
;
347 function echoResult(result
, event
) {
353 function echoFormResult(request
, result
) {
354 this.echo
= result
.result
;
357 function echoResultAndOptions(result
, event
, success
, options
) {
360 this.options
= options
;
364 function returnFalse() {
368 function checkEcho() {
369 return Ext
.isDefined(this.echo
);
372 function checkHandler() {
373 return !!handler
.callCount
;
376 function waitForEcho(fn
, desc
, timeout
) {
377 fn
= fn
|| checkEcho
;
378 desc
= desc
|| 'callback never fired';
379 timeout
= timeout
!= null ? timeout
: 100;
381 waitsFor(fn
, desc
, timeout
);
384 function expectEcho(want
) {
386 expect(this.echo
).toEqual(want
);
390 beforeEach(function() {
394 spyOn(Ext
.Ajax
, 'request').andCallFake(simulateDirectRequest
);
399 handler
= jasmine
.createSpy('event handler');
402 afterEach(function() {
406 describe("handles call mechanics", function() {
407 describe("call batching", function() {
408 afterEach(function() {
409 // Transactions in this suite have no chance of finishing,
410 // so we clean them up manually
411 if (provider
.callTask
) {
412 provider
.callTask
.cancel();
415 Ext
.direct
.Manager
.transactions
.clear();
418 it("should batch calls within specified enableBuffer timeout", function() {
419 var options
, baseTid
;
422 Ext
.Ajax
.request
.andCallFake(function(opt
) {
426 baseTid
= Ext
.direct
.Transaction
.TRANSACTION_ID
;
428 ns
.TestAction
.echo('foo', Ext
.emptyFn
);
429 ns
.TestAction
.echo('bar', Ext
.emptyFn
);
432 waitsFor(function() { return !!options
}, 'options never modified', 20);
435 expect(options
.jsonData
).toEqual([{
436 action
: 'TestAction',
442 action
: 'TestAction',
451 it("should run calls with specified timeout w/o batching", function() {
456 Ext
.Ajax
.request
.andCallFake(function(opt
) {
460 provider
.enableBuffer
= 200;
461 baseTid
= Ext
.direct
.Transaction
.TRANSACTION_ID
;
463 ns
.TestAction
.echo('baz', Ext
.emptyFn
);
464 ns
.TestAction
.echo('qux', Ext
.emptyFn
, this, { timeout
: 1 });
467 waitsFor(function() { return !!options
}, 'options never modified', 20);
470 expect(options
.length
).toBe(1);
472 expect(options
[0].jsonData
).toEqual({
473 action
: 'TestAction',
482 it("should run calls instantly with disableBatching", function() {
487 Ext
.Ajax
.request
.andCallFake(function(opt
) {
491 provider
.enableBuffer
= 200;
492 baseTid
= Ext
.direct
.Transaction
.TRANSACTION_ID
;
494 ns
.TestAction
.echo('baz', Ext
.emptyFn
);
496 ns
.TestAction
.echo
.$directCfg
.method
.disableBatching
= true;
497 ns
.TestAction
.echo('qux', Ext
.emptyFn
);
500 waitsFor(function() { return !!options
}, 'options never modified', 20);
503 expect(options
.length
).toBe(1);
505 expect(options
[0].jsonData
).toEqual({
506 action
: 'TestAction',
515 it("should run calls instantly with enableBuffer = false", function() {
518 Ext
.Ajax
.request
.andCallFake(function(opt
) {
522 provider
.enableBuffer
= false;
523 baseTid
= Ext
.direct
.Transaction
.TRANSACTION_ID
;
525 ns
.TestAction
.echo('fred', Ext
.emptyFn
);
527 expect(options
.jsonData
).toEqual({
528 action
: 'TestAction',
536 describe("bufferLimit", function() {
537 var options
, baseTid
;
539 beforeEach(function() {
542 Ext
.Ajax
.request
.andCallFake(function(opt
) {
546 provider
.enableBuffer
= 200;
547 provider
.bufferLimit
= 3;
549 baseTid
= Ext
.direct
.Transaction
.TRANSACTION_ID
;
552 it("should batch calls up to bufferLimit", function() {
554 ns
.TestAction
.echo('fee', Ext
.emptyFn
);
555 ns
.TestAction
.echo('fie', Ext
.emptyFn
);
556 ns
.TestAction
.echo('foe', Ext
.emptyFn
);
557 ns
.TestAction
.echo('foo', Ext
.emptyFn
);
560 waitsFor(function() {
561 return !!options
.length
;
562 }, 'options never modified', 20);
565 expect(options
.length
).toBe(1);
567 expect(options
[0].jsonData
).toEqual([{
568 action
: 'TestAction',
574 action
: 'TestAction',
580 action
: 'TestAction',
589 it("should make 2 batched requests for 6 calls", function() {
591 ns
.TestAction
.echo('frobbe', Ext
.emptyFn
);
592 ns
.TestAction
.echo('throbbe', Ext
.emptyFn
);
593 ns
.TestAction
.echo('gurgle', Ext
.emptyFn
);
594 ns
.TestAction
.echo('bonzo', Ext
.emptyFn
);
595 ns
.TestAction
.echo('mymse', Ext
.emptyFn
);
596 ns
.TestAction
.echo('splurge', Ext
.emptyFn
);
599 waitsFor(function() {
600 return !!options
.length
;
601 }, 'options never modified', 20);
604 expect(options
.length
).toBe(2);
606 expect(options
[0].jsonData
).toEqual([{
607 action
: 'TestAction',
613 action
: 'TestAction',
619 action
: 'TestAction',
626 expect(options
[1].jsonData
).toEqual([{
627 action
: 'TestAction',
633 action
: 'TestAction',
639 action
: 'TestAction',
648 it("should make 3 batched requests for 7 calls", function() {
650 ns
.TestAction
.echo('Grumpy', Ext
.emptyFn
);
651 ns
.TestAction
.echo('Sleepy', Ext
.emptyFn
);
652 ns
.TestAction
.echo('Dopey', Ext
.emptyFn
);
653 ns
.TestAction
.echo('Bashful', Ext
.emptyFn
);
654 ns
.TestAction
.echo('Sneezy', Ext
.emptyFn
);
655 ns
.TestAction
.echo('Happy', Ext
.emptyFn
);
656 ns
.TestAction
.echo('Doc', Ext
.emptyFn
);
659 waitsFor(function() {
660 return options
.length
=== 3;
661 }, '3 Ajax requests', 300);
664 expect(options
.length
).toBe(3);
666 expect(options
[0].jsonData
).toEqual([{
667 action
: 'TestAction',
673 action
: 'TestAction',
679 action
: 'TestAction',
686 expect(options
[1].jsonData
).toEqual([{
687 action
: 'TestAction',
693 action
: 'TestAction',
699 action
: 'TestAction',
706 expect(options
[2].jsonData
).toEqual({
707 action
: 'TestAction',
718 describe("call related events", function() {
719 it("fires 'beforecall' event", function() {
721 provider
.on('beforecall', handler
);
723 ns
.TestAction
.echo('fred', Ext
.emptyFn
);
726 waitsFor(checkHandler
, 'event handler never fired', 20);
729 expect(handler
).toHaveBeenCalled();
735 it("fires 'call' event", function() {
737 provider
.on('call', handler
);
739 ns
.TestAction
.echo('plugh', Ext
.emptyFn
);
742 waitsFor(checkHandler
, 'event handler never fired', 20);
745 expect(handler
).toHaveBeenCalled();
751 it("cancels request when 'beforecall' handler returns false", function() {
753 handler
.andCallFake(returnFalse
);
755 provider
.on('beforecall', handler
);
757 ns
.TestAction
.echo('mymse', Ext
.emptyFn
);
760 waitsFor(checkHandler
, 'event handler never fired', 200);
762 // Additional timeout for callbacks to queue and fire
766 expect(options
).toBeUndefined();
774 describe("with connection failed", function() {
775 it("retries failed transactions", function() {
776 var proto
= Ext
.direct
.Transaction
.prototype;
779 spyOn(proto
, 'retry').andCallThrough();
781 ns
.TestAction
.echo('foo', Ext
.emptyFn
, this, { timeout
: 666 });
784 waitsFor(function() {
785 return proto
.retry
.callCount
=== 1;
786 }, 'transaction.retry() never called', 200);
789 expect(proto
.retry
).toHaveBeenCalled();
793 it("fires exception when retry count is exceeded", function() {
795 provider
.on('data', handler
);
797 ns
.TestAction
.echo('bar', Ext
.emptyFn
, this, { timeout
: 666 });
800 waitsFor(checkHandler
, 'event handler never fired', 200);
803 expect(handler
).toHaveBeenCalled();
807 describe("handles callback:", function() {
808 it("fires 'beforecallback' event", function() {
810 provider
.on('beforecallback', handler
);
812 ns
.TestAction
.echo('baz', Ext
.emptyFn
, this, { timeout
: 666 });
815 waitsFor(checkHandler
, 'event handler never fired', 200);
818 expect(handler
).toHaveBeenCalled();
822 it("cancels callback when 'beforecallback' handler returns false", function() {
823 var cb
= jasmine
.createSpy('callback');
826 handler
.andCallFake(returnFalse
);
828 provider
.on('beforecallback', handler
);
830 ns
.TestAction
.echo('qux', cb
, this, { timeout
: 666 });
833 waitsFor(checkHandler
, 'event handler never fired', 200);
835 // Additional timeout for callback to be handled
839 expect(handler
).toHaveBeenCalled();
841 expect(cb
).not
.toHaveBeenCalled();
845 it("fires callback when retry count is exceeded", function() {
847 ns
.TestAction
.echo('plugh', echoStatus
, this, { timeout
: 666 });
850 waitsFor(checkEcho
, 'callback never fired', 200);
853 expect(this.echo
).toBe(false);
859 describe("successfully connected:", function() {
860 it("fires 'data' event", function() {
862 provider
.on('data', handler
);
864 ns
.TestAction
.echo('foo', echoResult
, this);
870 expect(handler
).toHaveBeenCalled();
874 describe("handles callback:", function() {
875 it("fires 'beforecallback' event", function() {
877 provider
.on('beforecallback', handler
);
879 ns
.TestAction
.echo('foo', echoResult
, this);
882 waitsFor(checkEcho
, 'event handler never fired', 100);
885 expect(handler
).toHaveBeenCalled();
889 it("cancels callback when 'beforecallback' handler returns false", function() {
890 var cb
= jasmine
.createSpy('callback');
893 handler
.andCallFake(returnFalse
);
895 provider
.on('beforecallback', handler
);
897 ns
.TestAction
.echo('bar', cb
, this);
900 waitsFor(checkHandler
, 'event handler never fired', 100);
902 // Additional timeout for callback to be handled
906 expect(handler
).toHaveBeenCalled();
908 expect(cb
).not
.toHaveBeenCalled();
912 it('runs w/o additional options', function() {
914 ns
.TestAction
.echo('foo', echoResult
, this);
922 it('runs w/ additional options', function() {
924 ns
.TestAction
.echo('bar', echoResultAndOptions
, this, {
932 expect(this.echo
).toEqual('bar');
933 expect(this.options
).toBeDefined();
934 expect(this.options
.victory
).toEqual('Huzzah!');
938 it('runs in nested namespaces', function() {
940 ns
.TestAction
.Foo
.foo(echoResult
, this);
948 it('runs in deeply nested namespaces', function() {
950 ns
.TestAction
.Foo
.Bar
.bar(echoResult
, this);
958 it('runs in really truly deeply nested namespaces', function() {
960 ns
.TestAction
.Foo
.Bar
.Baz
.baz(echoResult
, this);
969 describe("metadata", function() {
970 it("will pass named metadata", function() {
972 ns
.TestAction
.directMetaNamed('foo', echoResult
, this, {
981 expectEcho({ data
: 'foo', metadata
: { bleh
: 'blah' } });
984 it("will pass ordered metadata", function() {
986 ns
.TestAction
.directMetaOrdered('bar', echoResult
, this, {
987 metadata
: ['blerg', 'blam', 'frob']
993 // Metadata len === 2, so 3rd argument should be cut off
994 expectEcho({ data
: 'bar', metadata
: ['blerg', 'blam'] });
999 (Ext
.toolkit
=== 'classic' ? describe
: xdescribe
)("form calls:", function() {
1002 function createForm(config
) {
1003 config
= Ext
.apply({
1005 renderTo
: document
.body
,
1011 submit
: 'TestAction.directForm'
1015 xtype
: 'hiddenfield',
1017 value
: 'hide the sacred foo from infoodels!'
1021 value
: 'behold the false, deceitful overt foo'
1025 form
= Ext
.widget(config
);
1028 beforeEach(function() {
1032 afterEach(function() {
1038 describe("submit", function() {
1039 it("should pass field values to direct fn", function() {
1042 success
: echoFormResult
,
1047 // Callbacks are a bit slow but 2 sec is enough
1048 waitsFor(checkEcho
, 'callback that never fired', 2000);
1051 expect(this.echo
).toEqual({
1054 hidden_foo
: 'hide the sacred foo from infoodels!',
1055 overt_foo
: 'behold the false, deceitful overt foo'
1061 it("should pass extra params to direct fn", function() {
1067 success
: echoFormResult
,
1072 waitsFor(checkEcho
, 'callback that never fired', 2000);
1075 expect(this.echo
).toEqual({
1078 hidden_foo
: 'hide the sacred foo from infoodels!',
1079 overt_foo
: 'behold the false, deceitful overt foo',
1086 it("should pass form baseParams to direct fn", function() {
1088 form
.getForm().baseParams
= {
1089 MEGA_FOO
: 'ALL YOUR FOO ARE BELONG TO US!'
1093 success
: echoFormResult
,
1098 waitsFor(checkEcho
, 'callback that never fired', 2000);
1101 expect(this.echo
).toEqual({
1104 hidden_foo
: 'hide the sacred foo from infoodels!',
1105 overt_foo
: 'behold the false, deceitful overt foo',
1106 MEGA_FOO
: 'ALL YOUR FOO ARE BELONG TO US!'
1112 it("should pass named metadata", function() {
1114 form
.getForm().api
.submit
= 'TestAction.directMetaFormNamed';
1116 form
.getForm().metadata
= {
1121 success
: echoFormResult
,
1126 waitsFor(checkEcho
, 'callback that never fired', 2000);
1129 expect(this.echo
).toEqual({
1132 hidden_foo
: 'hide the sacred foo from infoodels!',
1133 overt_foo
: 'behold the false, deceitful overt foo'
1137 metadata
: '{"foo":"bargh!"}'
1142 it("should pass ordered metadata", function() {
1144 form
.getForm().api
.submit
= 'TestAction.directMetaFormOrdered';
1146 form
.getForm().metadata
= ['bram', 'blam', 'qux?'];
1149 success
: echoFormResult
,
1154 waitsFor(checkEcho
, 'callback that never fired', 2000);
1157 expect(this.echo
).toEqual({
1160 hidden_foo
: 'hide the sacred foo from infoodels!',
1161 overt_foo
: 'behold the false, deceitful overt foo'
1165 metadata
: '["bram","blam"]'