source: Dev/trunk/src/client/dojox/mvc/tests/doh/StoreRefControllerTest.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 7.2 KB
Line 
1define([
2        "doh",
3        "dojo/_base/array",
4        "dojo/_base/config",
5        "dojo/_base/declare",
6        "dojo/_base/lang",
7        "dojo/Deferred",
8        "dojo/store/util/QueryResults",
9        "dijit/_WidgetBase",
10        "dojox/mvc/at",
11        "dijit/_TemplatedMixin",
12        "dijit/_WidgetsInTemplateMixin",
13        "dijit/_Container",
14        "dijit/form/TextBox",
15        "dojox/mvc/getStateful",
16    "dojox/mvc/StoreRefController",
17    "dojox/mvc/EditStoreRefListController",     
18    "dojo/store/JsonRest",
19    "dojo/store/Memory",
20    "dojo/store/Observable",
21    "dojo/when",
22        "dojox/mvc/WidgetList",
23        "dojo/text!dojox/mvc/tests/test_WidgetList_WidgetListInTemplate.html",
24        "dojo/text!dojox/mvc/tests/test_WidgetList_childTemplate.html",
25        "dojo/text!dojox/mvc/tests/test_WidgetList_childBindings.json"
26], function(doh, array, config, declare, lang, Deferred, QueryResults, _WidgetBase, at, _TemplatedMixin, _WidgetsInTemplateMixin, _Container,
27        _TextBox, getStateful, StoreRefController, EditStoreRefListController, JsonRest, Memory, Observable, when, WidgetList, template, childTemplate, childBindings){7
28    var data = {
29        "identifier": "Serial",
30        "items": [
31            {
32                "Serial"  : "A111",
33                "First"   : "Anne",
34                "Last"    : "Ackerman",
35                "Email"   : "a.a@test.com"
36            },
37            {
38                "Serial"  : "B111",
39                "First"   : "Ben",
40                "Last"    : "Beckham",
41                "Email"   : "b.b@test.com"
42            },
43            {
44                "Serial"  : "I111",
45                "First"   : "Irene",
46                "Last"    : "Ira",
47                "Email"   : "i.i@test.com"
48            },
49            {
50                "Serial"  : "J111",
51                "First"   : "John",
52                "Last"    : "Jacklin",
53                "Email"   : "j.j@test.com"
54            }
55        ]
56    };
57
58
59        ctrl = new EditStoreRefListController({
60                store : new Memory({
61                        data : data
62                })
63        });
64        when(ctrl.getStore("A111"), function(value) {
65                doh.register("dojox.mvc.tests.doh.StoreRefControllerTest", [
66                        function getStore(){
67                                doh.is(value.Serial, "A111", "Serial should be set");
68                                doh.is(value.First, "Anne", "First should be set");
69                                doh.is(value.Last, "Ackerman", "Last should be set");
70                                doh.is(value.Email, "a.a@test.com", "Email should be set");
71                        },
72                        function queryStore(){
73                                when(ctrl.queryStore(), function(results) {
74                                        doh.is(results[0].Serial, "A111", "Serial should be set");
75                                        doh.is(results[0].First, "Anne", "First should be set");
76                                        doh.is(results[0].Last, "Ackerman", "Last should be set");
77                                        doh.is(results[0].Email, "a.a@test.com", "Email should be set");
78                                });
79                        },
80                        function addStore(){
81                                var newId2 = "newObj222-" + Math.random();
82                                var newObj2 = {
83                                        "Serial" : newId2,
84                                        "First" : "newObj2",
85                                        "Last" : "newObj2 Last",
86                                        "Email" : "new.obj2@test.com"
87                                };
88                                when(ctrl.addStore(newObj2), function(results) {
89                                        doh.is(results, newId2, "id should be returned");
90                                        when(ctrl.getStore(newId2), function(value) {
91                                                doh.is(value.Serial, newId2, "Serial should be set");
92                                                doh.is(value.First, "newObj2", "First should be set");
93                                                doh.is(value.Last, "newObj2 Last", "Last should be set");
94                                                doh.is(value.Email, "new.obj2@test.com", "Email should be set");
95                                        });
96                                });
97                        },
98                        function putStore(){
99                                var newId1 = "newObj111-" + Math.random();
100                                var newObj = {
101                                        "Serial" : newId1,
102                                        "First" : "newObj",
103                                        "Last" : "newObj Last",
104                                        "Email" : "new.obj@test.com"
105                                };
106                                when(ctrl.putStore(newObj), function(results) {
107                                        doh.is(results, newId1, "id should be returned");
108                                        when(ctrl.getStore(results), function(value) {
109                                                doh.is(value.Serial, newId1, "Serial should be set");
110                                                doh.is(value.First, "newObj", "First should be set");
111                                                doh.is(value.Last, "newObj Last", "Last should be set");
112                                                doh.is(value.Email, "new.obj@test.com", "Email should be set");
113                                        });
114                                });
115                        },
116                        function removeStore(){
117                                when(ctrl.queryStore(), function(results) {
118                                        var remObjId = results[1].Serial;
119                                        when(ctrl.removeStore(remObjId), function(results) {
120                                                doh.is(results, true, "should return true from removeStore");
121                                        });
122                                });
123                        },
124                        function observableAsyncStore(){
125                                var AsyncMemoryStore = declare(Memory, {});
126                                for(var s in {put: 1, remove: 1, query: 1}){
127                                        (function(s){
128                                                AsyncMemoryStore.prototype[s] = function(){
129                                                        var args = arguments,
130                                                         dfd = new Deferred(),
131                                                         _self = this;
132                                                        setTimeout(function(){
133                                                                dfd.resolve(Memory.prototype[s].apply(_self, args));
134                                                        }, 500);
135                                                        return QueryResults(dfd.promise);
136                                                };
137                                        })(s);
138                                }
139                                var ctrl = new EditStoreRefListController({store: Observable(new AsyncMemoryStore({idProperty: "Serial", data: data}))}),
140                                 updates = [],
141                                 dfd = new doh.Deferred();
142                                ctrl.queryStore({Last: "Ackerman"}).observe(dfd.getTestErrback(function(object, previousIndex, newIndex){
143                                        updates.push(lang.delegate(object, {
144                                                previousIndex: previousIndex,
145                                                newIndex: newIndex
146                                        }));
147                                        if(updates.length == 2){
148                                                doh.is({
149                                                        Serial: "D111",
150                                                        First: "David",
151                                                        Last: "Ackerman",
152                                                        Email: "d.a@test.com",
153                                                        previousIndex: -1,
154                                                        newIndex: 1
155                                                }, updates[0], "The observable callback should catch the addition");
156                                                doh.is({
157                                                        Serial: "A111",
158                                                        First: "Anne",
159                                                        Last: "Ackerman",
160                                                        Email: "a.a@test.com",
161                                                        previousIndex: 0,
162                                                        newIndex: -1
163                                                }, updates[1], "The observable callback should catch the removal");
164                                                dfd.callback(1);
165                                        }
166                                }));
167                                ctrl.addStore({
168                                        Serial: "D111",
169                                        First: "David",
170                                        Last: "Ackerman",
171                                        Email: "d.a@test.com"
172                                });
173                                ctrl.removeStore("A111");
174                                return dfd;
175                        },
176                        function observeJsonRest(){
177                                // Test we can observe results from a store that DOES defer results.
178                                var store = new JsonRest({
179                                        target: require.toUrl("dojo/tests/store/"),
180                                        put: function(o){ var dfd = new Deferred(); setTimeout(function(){ dfd.resolve(o.id); }, 500); return dfd.promise; }, // Intead of making REST call, just return the ID asynchronously
181                                        remove: function(){ var dfd = new Deferred(); setTimeout(function(){ dfd.resolve(true); }, 500); return dfd.promise; } // Intead of making REST call, just return true asynchronously
182                                });
183
184                                var ctrl = new StoreRefController({store : new Observable(store)}),
185                                 dfd = new doh.Deferred(),
186                                 updates = [];
187
188                                ctrl.queryStore("treeTestRoot").observe(dfd.getTestErrback(function(object, previousIndex, newIndex){
189                                        updates.push(lang.delegate(object, {
190                                                previousIndex: previousIndex,
191                                                newIndex: newIndex
192                                        }));
193                                        if(updates.length == 2){
194                                                doh.is({
195                                                        id: "node6",
196                                                        name: "node6",
197                                                        someProperty: "somePropertyA",
198                                                        previousIndex: -1,
199                                                        newIndex: 0
200                                                }, updates[0], "The observable callback should catch the addition");
201                                                doh.is({
202                                                        id: "node4",
203                                                        name: "node4",
204                                                        someProperty: "somePropertyA",
205                                                        previousIndex: 4,
206                                                        newIndex: -1
207                                                }, updates[1], "The observable callback should catch the removal");
208                                                dfd.callback(1);
209                                        }
210                                }));
211                                ctrl.addStore({
212                                        id: "node6",
213                                        name: "node6",
214                                        someProperty: "somePropertyA"
215                                });
216                                ctrl.removeStore("node4");
217                                return dfd;
218                        }
219                ]);
220        });
221});
Note: See TracBrowser for help on using the repository browser.