jxdnsong
2020-10-23 a7929e6b3ec9ac17233f39e55a2b8ac63ea75f42
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
define([
    'intern!tdd',
    'intern/chai!assert',
    'dojo/_base/declare',
    'dojo/aspect',
    'dojo/dom-class',
    'dojo/query',
    '../../data/createSyncStore',
    'dgrid/OnDemandList'
], function (test, assert, declare, aspect, domClass, query, createSyncStore, OnDemandList) {
 
    var widget,
        storeCounter = 0;
 
    function destroyWidget() {
        if (widget) {
            widget.destroy();
            widget = null;
        }
    }
 
    function indexToId(index) {
        return (index + 1) * 10;
    }
 
    function createItem(index) {
        var id = indexToId(index);
        return { id: id, value: 'Value ' + id + ' / Store ' + storeCounter };
    }
 
    function createData(numStoreItems) {
        var data = [];
        for (var i = 0; i < numStoreItems; i++) {
            data.push(createItem(i));
        }
        return data;
    }
 
    function createStore(numStoreItems) {
        storeCounter++;
        return createSyncStore({
            data: createData(numStoreItems)
        });
    }
 
    function renderRow(object) {
        var div = document.createElement('div');
        div.appendChild(document.createTextNode(object.value));
        return div;
    }
 
    function createList(numStoreItems, itemsPerQuery, overlap, shouldTrackCollection) {
        widget = new OnDemandList({
            collection: createStore(numStoreItems),
            minRowsPerPage: itemsPerQuery,
            maxRowsPerPage: itemsPerQuery,
            queryRowsOverlap: overlap,
            renderRow: renderRow,
            shouldTrackCollection: shouldTrackCollection !== false,
            sort: 'id'
        });
        document.body.appendChild(widget.domNode);
        widget.startup();
    }
 
    function itemTest(itemAction, index, numToModify, backwards) {
        // Creates a single test case for performing an action on numToModify rows/items.
        var description = itemAction.actionName + ' ' + numToModify + ' item' + (numToModify > 1 ? 's' : '') +
            ' starting at index ' + index + ', in ' + (backwards ? 'decreasing' : 'increasing') + ' order';
 
        numToModify = numToModify || 1;
 
        test.test(description, function () {
            var i,
                cnt,
                step = function () {
                    cnt++;
                    backwards ? i-- : i++;
                },
                tmp,
                expectedValues = [],
                msgPrefix;
 
            function testRow(element, i) {
                var expectedValue = expectedValues[i];
                if (expectedValue == null || expectedValue.deleted) {
                    assert.isTrue(element == null, msgPrefix + 'row at index ' + i + ' should not be found');
                }
                else {
                    expectedValue = expectedValue.value;
                    assert.isTrue(element != null,
                        msgPrefix + 'row at index ' + i + ' with an expected value of "' +
                        expectedValue + '" is missing');
                    assert.strictEqual(expectedValue, element.innerHTML,
                        msgPrefix + element.innerHTML + ' should be ' + expectedValue);
                }
            }
 
            // Perform the actions and update the array of expected values.
            expectedValues = createData(widget.collection.data.length);
            for (i = index, cnt = 0; cnt < numToModify; step()) {
                itemAction(indexToId(i), expectedValues);
            }
 
            // Use the dgrid widget API to test if the action was performed properly.
            msgPrefix = 'dgrid API: ';
            tmp = [];
            for (i = 0; i < expectedValues.length; i++) {
                var expectedValue = expectedValues[i],
                    expectedId = expectedValue.id;
                testRow(widget.row(expectedId).element, i);
                if (!expectedValue.deleted) {
                    tmp.push(expectedValue);
                }
            }
            expectedValues = tmp;
 
            // Query the DOM to verify the structure matches the expected results.
            msgPrefix = 'DOM query: ';
            query('.dgrid-row', widget.domNode).forEach(testRow);
        });
    }
 
    function itemTestSuite(widgetClassName, storeSize, itemsPerQuery, overlap, config) {
        // Create a test suite that performs one action type (itemAction) on 1 to config.itemsModifiedMax with
        // a given amount of overlap.
        var index, numToModify;
 
        test.suite(widgetClassName + ' with ' + overlap + ' overlap', function () {
 
            test.beforeEach(function () {
                createList(storeSize, itemsPerQuery, overlap);
            });
 
            test.afterEach(destroyWidget);
 
            // Modify items counting up.
            for (numToModify = 1; numToModify <= config.itemsModifiedMax; numToModify++) {
                for (index = 0; index <= (storeSize - numToModify); index++) {
                    itemTest(config.itemAction, index, numToModify);
                }
            }
            // Modify items counting down.  Starting at a count of 2 because
            // single item modification were tested above.
            for (numToModify = 2; numToModify <= config.itemsModifiedMax; numToModify++) {
                for (index = numToModify - 1; index < storeSize; index++) {
                    itemTest(config.itemAction, index, numToModify, true);
                }
            }
        });
    }
 
    function itemActionTestSuite(description, itemAction, config) {
        // Creates multiple item test suites for a given action (itemAction):
        // - a list that executes a single query
        // - lists with overlap from 0 to config.itemOverlapMax
 
        // Note: for debugging, comment out the contents of destroyWidget so the dgrid widgets are not destroyed.
        // Each widget uses a different store id and those ids are used in the row contents allowing you to
        // easily match up an error message like
        //  "Error: dgrid API: row at index 2 with an expected value of "Value 30 / Store 10 / Changed!" is missing"
        // with the correct widget on the page.
        config.itemAction = itemAction;
 
        test.suite(description, function () {
            // Test widgets with only one query: total item count equals item count per query.
            itemTestSuite('OnDemandList one query', config.itemsPerQuery, config.itemsPerQuery, 0, config);
 
            // Test widgets that make multiple query requests: twice as many items as items per query so multiple
            // queries will create multiple observers.
            var storeSize = config.itemsPerQuery * 2;
            // Test with OnDemandList with varying overlap values
            for (var overlap = 0; overlap <= config.itemOverlapMax; overlap++) {
                itemTestSuite('OnDemandList multiple queries', storeSize, config.itemsPerQuery, overlap, config);
            }
        });
    }
 
    function itemAddEmptyStoreTest(itemsToAddCount, itemsPerQuery, overlap) {
        var i;
 
        function rowHasClass(rowNode, cssClass) {
            assert.isTrue(domClass.contains(rowNode, cssClass), rowNode.outerHTML + ' should have ' + cssClass);
        }
 
        test.test('Add ' + itemsToAddCount + ' items with ' + overlap + ' overlap', function () {
            createList(0, itemsPerQuery, overlap);
            var store = widget.collection;
            for (i = 0; i < itemsToAddCount; i++) {
                store.put(createItem(i));
            }
 
            var rows = query('.dgrid-content > div', widget.domNode);
            rowHasClass(rows[0], 'dgrid-preload');
            for (i = 1; i <= itemsToAddCount; i++) {
                rowHasClass(rows[i], (i % 2) ? 'dgrid-row-even' : 'dgrid-row-odd');
            }
            rowHasClass(rows[i], 'dgrid-preload');
 
            for (i = 0; i < itemsToAddCount; i++) {
                store.put(createItem(i));
            }
 
            rows = query('.dgrid-content > div', widget.domNode);
            rowHasClass(rows[0], 'dgrid-preload');
            for (i = 1; i <= itemsToAddCount; i++) {
                rowHasClass(rows[i], (i % 2) ? 'dgrid-row-even' : 'dgrid-row-odd');
            }
            rowHasClass(rows[i], 'dgrid-preload');
        });
    }
 
    function itemAddEmptyStoreTestSuite(config) {
        test.suite('Add items to empty store', function () {
 
            test.afterEach(destroyWidget);
 
            itemAddEmptyStoreTest(1, config.itemsPerQuery, 0);
 
            // Test with OnDemandList with varying overlap values
            for (var overlap = 0; overlap <= config.itemOverlapMax; overlap++) {
                itemAddEmptyStoreTest(config.itemsPerQuery + overlap + 1, config.itemsPerQuery, overlap);
            }
        });
    }
 
    test.suite('Trackable lists', function () {
        // Creates test suites that execute the following actions on OnDemandLists with varying amount of
        // overlap and modifying varying number of items:
        // - modify existing items
        // - remove existing items
        // - add new items before existing items
        // - add new items after existing items
 
        function findIndex(id, objs) {
            for (var i = 0; i < objs.length; i++) {
                var obj = objs[i];
                if (obj && obj.id === id) {
                    return i;
                }
            }
            return -1;
        }
 
        var modifyAction = function (id, expectedValues) {
            var index = findIndex(id, expectedValues);
            var value = expectedValues[index].value + ' / Changed!';
            var dataObj = {id: id, value: value};
            widget.collection.put(dataObj);
            expectedValues[index] = dataObj;
        };
        modifyAction.actionName = 'Modify';
 
        var removeAction = function (id, expectedValues) {
            widget.collection.remove(id);
            var index = findIndex(id, expectedValues);
            expectedValues[index].deleted = true;
        };
        removeAction.actionName = 'Remove';
 
        var addBeforeAction = function (id, expectedValues) {
            var index = findIndex(id, expectedValues);
            var obj = { id: id - 5, value: expectedValues[index].value + ' / Added before!' };
            widget.collection.add(obj);
            expectedValues.splice(index, 0, obj);
        };
        addBeforeAction.actionName = 'Add before';
 
        var addAfterAction = function (id, expectedValues) {
            var index = findIndex(id, expectedValues);
            var obj = {id: id + 5, value: expectedValues[index].value + ' / Added after!'};
            widget.collection.add(obj);
            expectedValues.splice(index + 1, 0, obj);
        };
        addAfterAction.actionName = 'Add after';
 
        // Run a test case with each action (modify, remove, add before, add after) and vary the amount of
        // queryRowsOverlap and vary the number of items modified during each test case.  A configuration
        // object controls the amount of variation.  The properties are:
        // itemsPerQuery - The OnDemandList is configured to request this number of items per query.
        //        This property also determines the size of the store.  Test cases run with a store size
        //        equal to this number and test cases run with a store size twice this number.
        // itemOverlapMax - Each test case is executed with a queryRowsOverap value of 0 up to this number.
        // itemsModifiedMax - Each test is executed where the number of items modified, deleted or added is
        //        1 up to this number; all of the test cases are run where 1 item is modified and then again
        //        with 2 items being modified and so on.
        var config = {
            itemsPerQuery: 3,
            itemOverlapMax: 2,
            itemsModifiedMax: 2
        };
        itemActionTestSuite('Modify store items', modifyAction, config);
        itemActionTestSuite('Remove store items', removeAction, config);
        itemActionTestSuite('Insert store items before', addBeforeAction, config);
        itemActionTestSuite('Insert store items after', addAfterAction, config);
 
        itemAddEmptyStoreTestSuite(config);
    });
 
    test.suite('Multiple updates to trackable list', function () {
        test.afterEach(destroyWidget);
 
        test.test('Multiple puts', function () {
            var i;
            var data = [];
            for (i = 0; i < 10; i++) {
                data.push({ id: i, value: 'Value ' + i, enabled: true });
            }
            var store = createSyncStore({ data: data });
            widget = new OnDemandList({
                collection: store.filter({ enabled: true }),
                renderRow: renderRow
            });
            document.body.appendChild(widget.domNode);
            widget.startup();
 
            // Test putting several items with a filter applied;
            // the put objects should all be seen as removed
            for (i = 0; i < 5; i++){
                var item = data[i];
                item.enabled = false;
                store.put(item);
            }
 
            assert.strictEqual(widget._rows.length, 5,
                '5 items should remain in the _rows array (5 should be removed)');
        });
    });
 
    test.suite('shouldTrackCollection = false + store modifications', function () {
        var numItems = 3;
        var store;
        var handles = [];
 
        test.before(function () {
            createList(numItems, 25, 0, false);
        });
 
        test.beforeEach(function () {
            store = createStore(numItems);
            widget.set('collection', store);
        });
 
        test.afterEach(function () {
            for (var i = handles.length; i--;) {
                handles[i].remove();
            }
            handles = [];
        });
 
        test.after(destroyWidget);
 
        function countRows() {
            var count = query('.dgrid-row', widget.contentNode).length;
            return count;
        }
 
        test.test('shouldTrackCollection = false + add', function () {
            var numRows = countRows();
            store.addSync(createItem(3));
            assert.strictEqual(countRows(), numRows);
        });
 
        test.test('shouldTrackCollection = false + put', function () {
            var calls = 0;
 
            handles.push(aspect.before(widget, 'removeRow', function () {
                calls++;
            }));
            handles.push(aspect.before(widget, 'insertRow', function () {
                calls++;
            }));
 
            for (var i = 0; i < numItems; i++) {
                store.putSync(store.getSync(indexToId(i)));
            }
            assert.strictEqual(calls, 0, 'insertRow and removeRow should never be called');
        });
 
        test.test('shouldTrackCollection = false + remove', function () {
            var numRows = countRows();
            for (var i = 0; i < numItems; i++) {
                store.removeSync(indexToId(i));
            }
            assert.strictEqual(countRows(), numRows);
        });
    });
});