nnnjjj123
2020-11-17 1b2c1edb61190eeb19f465ff031eaa3b2a1b8dbc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
define([
    'intern!tdd',
    'intern/chai!assert',
    'dgrid/Grid',
    'dgrid/OnDemandGrid',
    'dgrid/_StoreMixin',
    'dgrid/Tree',
    'dojo/_base/declare',
    'dojo/_base/lang',
    'dojo/_base/array',
    'dojo/Deferred',
    'dojo/on',
    'dstore/Memory',
    'dstore/Tree',
    'dstore/QueryResults',
    'dojo/query',
    '../addCss!'
], function (test, assert, Grid, OnDemandGrid, _StoreMixin, Tree, declare, lang, arrayUtil, Deferred, on,
        Memory, TreeStore, QueryResults, query) {
 
    test.suite('tree (expand + promise)', function () {
        var grid,
            SyncTreeStore = declare([ Memory, TreeStore ], {
                mayHaveChildren: function () {
                    return true;
                }
            }),
            AsyncTreeStore = declare(SyncTreeStore, {
                // TreeStore with an asynchronous fetch method.
                fetch: function () {
                    return asyncFetch.call(this);
                },
                fetchRange: function (kwArgs) {
                    return asyncFetch.call(this, kwArgs);
                },
                resolve: function () {
                    // Allows the test to control when the store query is resolved.
                    this.dfd.resolve(this.results);
                },
                reject: function (error) {
                    // Allows the test to control when the store query is rejected.
                    this.dfd.reject(error);
                }
            }),
            StoreMixinGrid = declare([Grid, _StoreMixin, Tree]),
            syncStore = new SyncTreeStore({ data: createData() }),
            asyncStore = new AsyncTreeStore({ data: createData() });
 
        function asyncFetch(kwArgs) {
            // Setting dfd on the prototype because collection chaining means we can't set it on an instance.
            // It isn't great, but in practice, it is not much different than before.
            var dfd = AsyncTreeStore.prototype.dfd = new Deferred();
            var results = AsyncTreeStore.prototype.results = kwArgs ?
                this.fetchSync().slice(kwArgs.start, kwArgs.end) : this.fetchSync();
            results.totalLength = dfd.then(function () {
                return results.length;
            });
            return new QueryResults(dfd.promise);
        }
 
        function createData() {
            return [
                { id: 1, node: 'Node 1', value: 'Value 1', parent: null },
                { id: 2, node: 'Node 2', value: 'Value 2', parent: 1 },
                { id: 3, node: 'Node 3', value: 'Value 3', parent: 2 },
                { id: 4, node: 'Node 4', value: 'Value 4', parent: 2 },
                { id: 5, node: 'Node 5', value: 'Value 5', parent: null }
            ];
        }
 
        function createGrid(store) {
            grid = new (declare([ OnDemandGrid, Tree ]))({
                columns: [
                    {renderExpando: true, field: 'node', label: 'Node'},
                    {field: 'value', label: 'Value'}
                ],
                enableTreeTransitions: false
            });
            document.body.appendChild(grid.domNode);
            grid.startup();
            grid.set('collection', store.getRootCollection());
        }
 
        function createNoRenderQueryGrid(store) {
            grid = new StoreMixinGrid({
                collection: store.getRootCollection(),
                columns: [
                    {renderExpando: true, field: 'node', label: 'Node'},
                    {field: 'value', label: 'Value'}
                ],
                enableTreeTransitions: false
            });
            document.body.appendChild(grid.domNode);
            grid.startup();
            grid.renderQueryResults(grid.collection.fetch());
        }
 
        function destroyGrid() {
            if (grid) {
                grid.destroy();
                grid = null;
            }
        }
 
        function createOnPromise(target, event) {
            // Creates a promise based on an on.once call.
            // Resolves to the event passed to the handler function.
            var dfd = new Deferred(function () {
                    handle.remove();
                }),
                handle = on.once(target, event, function (event) {
                    dfd.resolve(event);
                });
 
            return dfd.promise;
        }
 
        function delayedResolve() {
            setTimeout(function () {
                grid.collection.resolve();
            }, 10);
        }
 
        test.suite('tree + sync store', function () {
            test.beforeEach(function () {
                createGrid(syncStore);
            });
            test.afterEach(destroyGrid);
 
            // Tests
            test.test('expand + no callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                grid.expand(1);
                assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
            });
 
            test.test('expand + callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                return grid.expand(1).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
                });
            });
 
            test.test('expand + multiple callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                return grid.expand(1).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
                    return grid.expand(2);
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length, 'Grid should have 5 rows');
                    return grid.expand(4);
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length, 'Grid should have 5 rows');
                });
            });
 
            test.test('duplicate expand + callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                return grid.expand(1).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
                    return grid.expand(1);
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows (no query)');
                });
            });
        });
 
        test.suite('tree + async store', function () {
            test.beforeEach(function () {
                createGrid(asyncStore);
            });
            test.afterEach(destroyGrid);
 
            test.test('expand + callback', function () {
                var promise = createOnPromise(grid, 'dgrid-refresh-complete').then(function () {
                    // Start testing when the grid is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows before expand resolves');
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
 
            test.test('expand + multiple callback', function () {
                var promise = createOnPromise(grid, 'dgrid-refresh-complete').then(function () {
                    // Start testing when the grid is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows before expand resolves');
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows');
                    var promise = grid.expand(2);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 3 rows before expand resolves');
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 5 rows');
                    var promise = grid.expand(4);
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 5 rows after expanding item with no children');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
 
            test.test('duplicate expand + callback', function () {
                var promise = createOnPromise(grid, 'dgrid-refresh-complete').then(function () {
                    // Start testing when the grid is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows');
                    return grid.expand(1);
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 3 rows (no query)');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
 
            test.test('expand + callback, rejecting', function () {
                var errorCount = 0;
 
                grid.on('dgrid-error', function (event) {
                    event.preventDefault(); // Suppress log message
                    errorCount++;
                });
 
                var promise = createOnPromise(grid, 'dgrid-refresh-complete').then(function () {
                    // Start testing when the grid is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows before expand resolves');
                    setTimeout(function () {
                        grid.collection.reject('Rejected');
                    }, 10);
                    return promise;
                }).then(function () {
                    throw new Error('Promise should have been rejected');
                }, function () {
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows after rejected promise');
                    assert.strictEqual(1, errorCount,
                        'The grid should have emitted a single error event');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
        });
 
        test.suite('tree + no renderQuery + sync store', function () {
            test.beforeEach(function () {
                createNoRenderQueryGrid(syncStore);
            });
            test.afterEach(destroyGrid);
 
            test.test('expand + callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                grid.expand(1);
                assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
            });
 
            test.test('expand + callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                return grid.expand(1).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
                });
            });
 
            test.test('expand + multiple callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                return grid.expand(1).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
                    return grid.expand(2);
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length, 'Grid should have 5 rows');
                    return grid.expand(4);
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length, 'Grid should have 5 rows');
                });
            });
 
            test.test('duplicate expand + callback', function () {
                assert.strictEqual(2, query('.dgrid-row', grid.domNode).length, 'Grid should have 2 rows');
                return grid.expand(1).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length, 'Grid should have 3 rows');
                    return grid.expand(1);
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows (no query)');
                });
            });
        });
 
        test.suite('tree + no renderQuery + async store', function () {
            test.beforeEach(function () {
                createNoRenderQueryGrid(asyncStore);
            });
 
            test.afterEach(destroyGrid);
 
            test.test('expand + callback', function () {
                var promise = grid.collection.dfd.then(function () {
                    // Start testing when the initial query is done is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows before expand resolves');
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
 
            test.test('expand + multiple callback', function () {
                var promise = grid.collection.dfd.then(function () {
                    // Start testing when the initial query is done is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows before expand resolves');
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows');
                    var promise = grid.expand(2);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 3 rows before expand resolves');
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 5 rows');
                    var promise = grid.expand(4);
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(5, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 5 rows after expanding item with no children');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
 
            test.test('duplicate expand + callback', function () {
                var promise = grid.collection.dfd.then(function () {
                    // Start testing when the initial query is done is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
                    delayedResolve();
                    return promise;
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 3 rows');
                    return grid.expand(1);
                }).then(function () {
                    assert.strictEqual(3, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 3 rows (no query)');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
 
            test.test('expand + callback, rejecting', function () {
                var promise = grid.collection.dfd.then(function () {
                    // Start testing when the initial query is done is ready.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should have 2 rows');
                    var promise = grid.expand(1);
 
                    // Verify that the result is the same before the query resolves.
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows before expand resolves');
                    setTimeout(function () {
                        grid.collection.reject('Rejected');
                    }, 10);
                    return promise;
                }).then(function () {
                    throw new Error('Promise should have been rejected');
                }, function () {
                    assert.strictEqual(2, query('.dgrid-row', grid.domNode).length,
                        'Grid should still have 2 rows after rejected promise');
                });
 
                assert.strictEqual(0, query('.dgrid-row', grid.domNode).length,
                    'Grid should have 0 rows before first async query resolves');
                // Resolve the grid's initial store query.
                delayedResolve();
                return promise;
            });
        });
    });
});