1 describe("Ext.data.proxy.Server", function() {
3 ServerProxy
= Ext
.data
.proxy
.Server
,
4 reader
= new Ext
.data
.reader
.Reader(),
5 writer
= new Ext
.data
.writer
.Writer();
7 beforeEach(function() {
8 Ext
.ClassManager
.enableNamespaceParseCache
= false;
12 Ext
.ClassManager
.enableNamespaceParseCache
= true;
13 Ext
.data
.Model
.schema
.clear();
14 Ext
.undefine('spec.SomeModel');
17 describe("instantiation", function() {
19 beforeEach(function(){
28 proxy
= new ServerProxy(config
);
31 it("should extend Ext.data.proxy.Proxy", function() {
32 expect(proxy
.superclass
).toEqual(Ext
.data
.proxy
.Proxy
.prototype);
35 it("should have caching disabled", function() {
36 expect(proxy
.getNoCache()).toBe(true);
39 it("should have cacheString equal to _dc", function() {
40 expect(proxy
.getCacheString()).toBe("_dc");
43 it("should have timeout equal to 30000", function() {
44 expect(proxy
.getTimeout()).toBe(30000);
47 it("should have extraParams", function() {
48 expect(proxy
.getExtraParams()).toEqual(config
.extraParams
);
51 it("should have reader", function() {
52 expect(proxy
.getReader()).toBe(config
.reader
);
55 it("should have writer", function() {
56 expect(proxy
.getWriter()).toBe(config
.writer
);
60 describe("CRUD Operations", function() {
61 beforeEach(function() {
62 proxy
= new ServerProxy();
63 proxy
.doRequest
= jasmine
.createSpy();
66 describe("create", function() {
67 it("should do a request", function() {
68 proxy
.read('create', 'create');
69 expect(proxy
.doRequest
).toHaveBeenCalledWith('create', 'create');
73 describe("read", function() {
74 it("should do a request", function() {
75 proxy
.read('read', 'read');
76 expect(proxy
.doRequest
).toHaveBeenCalledWith('read', 'read');
80 describe("update", function() {
81 it("should do a request", function() {
82 proxy
.read('update', 'update');
83 expect(proxy
.doRequest
).toHaveBeenCalledWith('update', 'update');
87 describe("destroy", function() {
88 it("should do a request", function() {
89 proxy
.read('destroy', 'destroy');
90 expect(proxy
.doRequest
).toHaveBeenCalledWith('destroy', 'destroy');
95 describe("buildRequest", function() {
96 describe("url", function() {
97 it("should use the proxy url", function() {
98 proxy
= new ServerProxy({
102 var request
= proxy
.buildRequest(new Ext
.data
.operation
.Read());
103 expect(request
.getUrl()).toBe('foo');
106 it("should prefer the operation url", function() {
107 proxy
= new ServerProxy({
111 var request
= proxy
.buildRequest(new Ext
.data
.operation
.Read({
114 expect(request
.getUrl()).toBe('bar');
119 describe("buildUrl", function() {
120 var request
= new Ext
.data
.Request({
123 configWithNoCache
= {
126 configWithCacheString
= {
130 beforeEach(function() {
131 spyOn(Ext
.Date
, "now").andReturn('bro');
134 it("should return keep?_dc=bro with an empty config", function() {
135 proxy
= new ServerProxy({});
136 expect(proxy
.buildUrl(request
), 'keep?_dc=bro');
139 it("should disable caching", function() {
140 proxy
= new ServerProxy(configWithNoCache
);
141 expect(proxy
.buildUrl(request
), request
.url
);
144 it("should use cacheString", function() {
145 proxy
= new ServerProxy(configWithCacheString
);
146 expect(proxy
.buildUrl(request
), 'keep?_cool=bro');
149 describe("url precedence", function(){
150 it("should use the url on the proxy as a default", function(){
151 proxy
= new ServerProxy({
155 expect(proxy
.buildUrl(new Ext
.data
.Request())).toBe('proxy');
158 it("should use the specified api by default", function(){
159 proxy
= new ServerProxy({
165 expect(proxy
.buildUrl(new Ext
.data
.Request({
170 it("should use the url on the request by default", function(){
171 proxy
= new ServerProxy({
174 expect(proxy
.buildUrl(new Ext
.data
.Request({
177 }))).toBe('request');
180 it("should use proxy url if the item in the proxy is undefined", function(){
181 proxy
= new ServerProxy({
188 expect(proxy
.buildUrl(new Ext
.data
.Request({
193 it("should favour the api over the proxy url", function(){
194 proxy
= new ServerProxy({
201 expect(proxy
.buildUrl(new Ext
.data
.Request({
206 it("should favour the request url over the proxy", function(){
207 proxy
= new ServerProxy({
211 expect(proxy
.buildUrl(new Ext
.data
.Request({
214 }))).toBe('request');
217 it("should favour the request url over the api", function(){
218 proxy
= new ServerProxy({
224 expect(proxy
.buildUrl(new Ext
.data
.Request({
227 }))).toBe('request');
230 it("should favour the request url over proxy & api", function(){
231 proxy
= new ServerProxy({
238 expect(proxy
.buildUrl(new Ext
.data
.Request({
241 }))).toBe('request');
246 describe("doRequest", function() {
247 it("should throw an error", function() {
248 expect(ServerProxy
.prototype.doRequest
).toThrow();
252 describe("getParams", function() {
253 var params
, sorters
, filters
, grouper
;
255 function createProxy(config
) {
256 return new Ext
.data
.proxy
.Server(config
|| {});
259 function createOperation(config
) {
260 return new Ext
.data
.operation
.Read(Ext
.apply({}, config
, {
271 function getParams(proxyConfig
, operationConfig
) {
272 var proxy
= createProxy(proxyConfig
),
273 operation
= createOperation(operationConfig
);
275 return proxy
.getParams(operation
);
278 beforeEach(function() {
279 sorters
= [new Ext
.util
.Sorter({property
: 'name', direction
: 'ASC'})];
280 filters
= [new Ext
.util
.Filter({property
: 'name', value
: 'Ed'})];
281 grouper
= new Ext
.util
.Grouper({property
: 'name', direction
: 'ASC'});
284 describe("the page param", function() {
285 it("should default to 'page'", function() {
286 params
= getParams();
288 expect(params
.page
).toBe(10);
291 it("should be customizable", function() {
292 params
= getParams({pageParam
: 'thePage'});
294 expect(params
.thePage
).toBe(10);
297 it("should not be sent if undefined", function() {
298 params
= getParams({pageParam
: undefined});
300 expect(params
.page
).toBeUndefined();
304 describe("the start param", function() {
305 it("should default to 'start'", function() {
306 params
= getParams();
308 expect(params
.start
).toBe(100);
311 it("should be customizable", function() {
312 params
= getParams({startParam
: 'theStart'});
314 expect(params
.theStart
).toBe(100);
317 it("should not be sent if undefined", function() {
318 params
= getParams({startParam
: undefined});
320 expect(params
.start
).toBeUndefined();
323 it("should send a startParam of 0", function() {
324 params
= getParams(undefined, {
328 expect(params
.start
).toBe(0);
332 describe("the limit param", function() {
333 it("should default to 'limit'", function() {
334 params
= getParams();
336 expect(params
.limit
).toBe(10);
339 it("should be customizable", function() {
340 params
= getParams({limitParam
: 'theLimit'});
342 expect(params
.theLimit
).toBe(10);
345 it("should not be sent if undefined", function() {
346 params
= getParams({limitParam
: undefined});
348 expect(params
.limit
).toBeUndefined();
352 describe("the group param", function() {
354 it("should default to 'group'", function() {
355 params
= getParams();
356 expect(params
.group
).toBe('{"property":"name","direction":"ASC"}');
359 it("should be customizable", function() {
360 params
= getParams({groupParam
: 'theGroup'});
362 expect(params
.theGroup
).toBe('{"property":"name","direction":"ASC"}');
365 it("should not be sent if undefined", function() {
366 params
= getParams({groupParam
: undefined});
368 expect(params
.group
).toBeUndefined();
371 it("should not be set if there is no group defined", function() {
372 params
= getParams({}, {grouper
: undefined});
374 expect(params
.group
).toBeUndefined();
378 describe("the sort param", function() {
379 beforeEach(function() {
380 spyOn(Ext
.data
.proxy
.Server
.prototype, 'encodeSorters').andReturn("sorters");
383 it("should default to 'sort'", function() {
384 params
= getParams();
386 expect(params
.sort
).toBe("sorters");
389 it("should be customizable", function() {
390 params
= getParams({sortParam
: 'theSorters'});
392 expect(params
.theSorters
).toBe("sorters");
395 it("should not be sent if undefined", function() {
396 params
= getParams({sortParam
: undefined});
398 expect(params
.sort
).toBeUndefined();
401 it("should encode the sorters", function() {
404 expect(Ext
.data
.proxy
.Server
.prototype.encodeSorters
).toHaveBeenCalledWith(sorters
);
407 it("should not be set if there are no sorters", function() {
408 params
= getParams({}, {sorters
: []});
410 expect(params
.sort
).toBeUndefined();
414 describe("the filter param", function() {
415 beforeEach(function() {
416 spyOn(Ext
.data
.proxy
.Server
.prototype, 'encodeFilters').andReturn("filters");
419 it("should default to 'filter'", function() {
420 params
= getParams();
422 expect(params
.filter
).toBe("filters");
425 it("should be customizable", function() {
426 params
= getParams({filterParam
: 'theFilters'});
428 expect(params
.theFilters
).toBe("filters");
431 it("should not be sent if undefined", function() {
432 params
= getParams({filterParam
: undefined});
434 expect(params
.filter
).toBeUndefined();
437 it("should encode the filters", function() {
440 expect(Ext
.data
.proxy
.Server
.prototype.encodeFilters
).toHaveBeenCalledWith(filters
);
443 it("should not be set if there are no filters", function() {
444 params
= getParams({}, {filters
: []});
446 expect(params
.filter
).toBeUndefined();
451 describe("encoding sorters", function() {
452 it("should provide a default encoded string", function() {
453 var sorter1
= new Ext
.util
.Sorter({
458 var sorter2
= new Ext
.util
.Sorter({
463 proxy
= new Ext
.data
.proxy
.Server();
465 expect(Ext
.decode(proxy
.encodeSorters([sorter1
, sorter2
]))).toEqual([{
475 describe("encoding filters", function() {
476 it("should provide a default encoded string, operator should be excluded by default", function() {
477 var filter1
= new Ext
.util
.Filter({
482 var filter2
= new Ext
.util
.Filter({
488 proxy
= new Ext
.data
.proxy
.Server();
490 expect(Ext
.decode(proxy
.encodeFilters([filter1
, filter2
]))).toEqual([{
501 describe("encoding group data", function() {
502 it("should JSON encode the data", function() {
503 var proxy
= new Ext
.data
.proxy
.Server(),
504 grouper
= new Ext
.util
.Grouper({property
: 'name', direction
: 'ASC'});
506 expect(Ext
.decode(proxy
.encodeSorters([grouper
], true))).toEqual({
513 describe("reader accessors", function() {
516 defaultReaderType
= 'xml',
517 modelName
= 'spec.SomeModel',
521 beforeEach(function(){
522 model
= Ext
.define(modelName
, {
523 extend
: 'Ext.data.Model',
528 describe("set the proxy's reader by reader instance", function() {
529 beforeEach(function(){
533 proxy
= new ServerProxy(config
);
536 it("should not create a new reader instance", function() {
538 spyOn(Ext
, "createByAlias").andCallFake(function(name
) {
539 if (name
=== 'reader.json') {
543 expect(called
).toBe(false);
546 it("should have a reader set", function() {
547 expect(proxy
.getReader()).toEqual(reader
);
551 describe("set the proxy's reader by string", function() {
552 beforeEach(function(){
557 defaultReaderType
: defaultReaderType
559 proxy
= new ServerProxy(config
);
562 it("should create a new reader instance", function() {
563 expect(proxy
.getReader().isReader
).toBe(true);
566 it("should have a reader set", function() {
567 expect(proxy
.getReader().$className
).toBe('Ext.data.reader.Xml');
572 describe("writer accessors", function() {
575 defaultWriterType
= 'xml',
576 modelName
= 'spec.SomeModel', model
;
578 beforeEach(function(){
579 model
= Ext
.define(modelName
, {
580 extend
: 'Ext.data.Model',
585 describe("set the proxy's writer by writer instance", function() {
586 beforeEach(function(){
590 proxy
= new ServerProxy(config
);
593 it("should not create a new writer instance", function() {
595 spyOn(Ext
, "createByAlias").andCallFake(function(name
) {
596 if (name
=== 'writer.json') {
600 expect(called
).toBe(false);
603 it("should have a writer set", function() {
604 expect(proxy
.getWriter()).toEqual(writer
);
608 describe("set the proxy's writer by string", function() {
609 beforeEach(function(){
613 defaultWriterType
: defaultWriterType
615 proxy
= new ServerProxy(config
);
618 it("should create a new writer instance", function() {
619 expect(proxy
.getWriter().isWriter
).toBe(true);
622 it("should have a writer set", function() {
623 expect(proxy
.getWriter().$className
).toBe('Ext.data.writer.Xml');
628 describe("destroy", function(){
631 beforeEach(function(){
636 proxy
= new ServerProxy(config
);
639 it('should destroy reader and writer', function(){
640 spy
= spyOn(Ext
, "destroy");
642 expect(spy
).toHaveBeenCalledWith(reader
, writer
);