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
define([
    'intern!tdd',
    'intern/chai!assert',
    'dojo/_base/declare',
    'dojo/dom-style',
    'dojo/dom-construct',
    'dojo/query',
    'dgrid/Grid',
    'dgrid/ColumnSet',
    'dgrid/extensions/ColumnHider',
    'dgrid/extensions/ColumnResizer',
    'dgrid/util/misc',
    '../addCss!'
], function (test, assert, declare, domStyle, domConstruct, query,
        Grid, ColumnSet, ColumnHider, ColumnResizer, miscUtil) {
 
    var grid;
    function gridDestroy() {
        if (grid) {
            grid.destroy();
            grid = undefined;
        }
    }
 
    test.suite('addCssRule', function () {
        var testDiv;
 
        // Setup / teardown
        test.before(function () {
            testDiv = domConstruct.create('div', null, document.body);
        });
 
        test.after(function () {
            domConstruct.destroy(testDiv);
        });
 
        test.afterEach(gridDestroy);
 
        // Tests
        test.test('addCssRule + remove', function () {
            testDiv.className = 'foo';
            // cache original style and update .foo
            var origStyle = domStyle.getComputedStyle(testDiv).fontSize,
                rule = miscUtil.addCssRule('.foo', 'font-size: 8px;');
            // check updated font size was applied
            assert.strictEqual('8px', domStyle.getComputedStyle(testDiv).fontSize,
                'Node with matching class has expected font-size');
            // remove the rule & make sure the computed style is good again
            rule.remove();
            assert.strictEqual(origStyle, domStyle.getComputedStyle(testDiv).fontSize,
                'Node with matching class no longer has font-size from removed rule');
            testDiv.className = '';
        });
 
        test.test('addCssRule get/set/remove', function () {
            testDiv.className = 'bar';
            // cache original style and update .foo
            var origStyle = domStyle.getComputedStyle(testDiv).fontSize,
                rule = miscUtil.addCssRule('.bar', 'font-size: 8px;');
            // check updated font size was applied
            assert.strictEqual('8px', rule.get('fontSize'),
                'rule.get(\'fontSize\') reports expected value');
            // update the font size
            rule.set('fontSize', '9px');
            // verify that the size updated
            assert.strictEqual('9px', domStyle.getComputedStyle(testDiv).fontSize,
                'Node with matching class has expected font-size after set');
            // make sure rule.get works the same
            assert.strictEqual('9px', rule.get('fontSize'),
                'rule.get(\'fontSize\') reports expected value after set');
            // remove the rule & make sure it updates
            rule.remove();
            assert.strictEqual(origStyle, domStyle.getComputedStyle(testDiv).fontSize,
                'Node with matching class no longer has font-size from removed rule');
            testDiv.className = '';
        });
 
        test.test('add/remove multiple rules in mixed order', function () {
            var origStyle = domStyle.getComputedStyle(testDiv).fontSize,
                rules = [],
                expected = { // hash containing test classes / values
                    foo: '7px',
                    bar: '8px',
                    baz: '9px'
                },
                cls;
 
            function check() {
                // Test that all expected styles hold
                var cls;
                for (cls in expected) {
                    testDiv.className = cls;
                    assert.strictEqual(expected[cls], domStyle.getComputedStyle(testDiv).fontSize,
                        'Node with class ' + cls + ' has expected font-size');
                }
                testDiv.className = '';
            }
 
            // Create rules and maintain references to returned objects
            for (cls in expected) {
                rules.push(miscUtil.addCssRule('.' + cls, 'font-size: ' + expected[cls] + ';'));
            }
 
            // Do initial check, then remove rules one by one, out of order,
            // updating the hash and checking each time along the way
            check();
 
            rules[2].remove();
            expected.baz = origStyle;
            check();
 
            rules[0].remove();
            expected.foo = origStyle;
            check();
 
            rules[1].remove();
            expected.bar = origStyle;
            check();
        });
 
        test.test('addCssRule via dgrid APIs', function () {
            var values = ['7px', '8px'],
                origValues;
 
            function createGrid(cleanAddedRules) {
                grid = new Grid({
                    id: 'my.grid', // test escaping of CSS identifiers from methods
                    columns: {
                        name: 'Name',
                        value: 'Value',
                        comment: 'Comment' // unstyled buffer
                    },
                    cleanAddedRules: cleanAddedRules
                });
                document.body.appendChild(grid.domNode);
                grid.startup();
            }
 
            function addRules() {
                var rules = [];
 
                rules.push(grid.addCssRule('.field-value', 'font-size: ' + values[0] + ';'));
                rules.push(grid.styleColumn('name', 'font-size: ' + values[1] + ';'));
 
                return rules;
            }
 
            function getStyles() {
                return [
                    domStyle.getComputedStyle(query('.field-value', grid.domNode)[0]).fontSize,
                    domStyle.getComputedStyle(query('.dgrid-column-name', grid.domNode)[0]).fontSize
                ];
            }
 
            function checkRules(expected) {
                var actual = getStyles(grid);
                assert.strictEqual(expected[0], actual[0],
                    'Style modified via addCssRule has expected value');
                assert.strictEqual(expected[1], actual[1],
                    'Style modified via styleColumn has expected value');
            }
 
            // Create grid for the first time
            createGrid(true);
            // Collect original style values for later cleanup check
            origValues = getStyles();
            // Add rules and make sure they applied as expected
            addRules();
            checkRules(values);
            // Destroy the grid, which should remove the style rules
            gridDestroy();
 
            // Create grid for the second time, with cleanAddedRules: false
            createGrid(false);
            // Before adding styles, make sure the ones from last time were removed
            checkRules(origValues);
            // Add rules and check again;
            // store the rules for tearDown since they won't be cleaned up
            this.rules = addRules();
            checkRules(values);
            // Destroy the grid, which should *not* remove the style rules
            gridDestroy();
 
            // Create grid for the third time
            createGrid(true);
            // Check that styles from last time still exist
            checkRules(values);
            // Destroy the grid (which won't remove the styles from last time,
            // since no handles were added to this exact instance)
            gridDestroy();
 
            // Clean up rule litter from cleanAddedRules: false test
            var i;
            for (i in this.rules) {
                this.rules[i].remove();
            }
        });
 
        test.test('Grid columns as array (numeric IDs)', function () {
            grid = new Grid({
                columns: [
                    { field: 'name' },
                    { field: 'value' }
                ]
            });
            document.body.appendChild(grid.domNode);
            grid.startup();
 
            assert.doesNotThrow(function () {
                grid.styleColumn(0, 'font-size: 8px;');
            }, null, 'styleColumn with numeric column ID should not throw error');
            assert.strictEqual('8px', domStyle.getComputedStyle(query('.dgrid-cell')[0]).fontSize,
                'Column cell should have expected style');
        });
    });
 
    test.suite('CSS escaping of column IDs via dgrid APIs', function () {
 
        test.suite('Grid columns and columnsets', function () {
            function makeTest(func, id) {
                return function () {
                    assert.doesNotThrow(function () {
                        grid[func](id, 'font-size: 8px;');
                    }, null, func + ' should escape special characters and not throw error');
                    assert.strictEqual('8px', domStyle.getComputedStyle(query('.dgrid-cell')[0]).fontSize,
                        'Column cell should have expected style');
                };
            }
 
            test.beforeEach(function () {
                grid = new (declare([Grid, ColumnSet]))({
                    id: 'i:d',
                    columnSets: [[[
                        { field: 'foo', id: 'col:umn' }
                    ]]]
                });
                document.body.appendChild(grid.domNode);
                grid.startup();
            });
 
            test.afterEach(gridDestroy);
 
            test.test('styleColumn', makeTest('styleColumn', 'col:umn'));
 
            // Currently ColumnSet IDs can't be customized so that isn't really an issue,
            // but this still tests escaping the grid ID
            test.test('styleColumnSet', makeTest('styleColumnSet', '0'));
        });
 
        test.suite('ColumnHider', function () {
            var ColumnHiderGrid = declare([Grid, ColumnHider]);
 
            test.afterEach(gridDestroy);
 
            test.test('Hiding column after construction', function () {
                assert.doesNotThrow(function () {
                    grid = new ColumnHiderGrid({
                        id: 'i:d',
                        columns: [
                            { field: 'foo', id: 'col:umn' }
                        ]
                    });
                }, null, 'ColumnHider should not throw error during construction');
                document.body.appendChild(grid.domNode);
                grid.startup();
 
                assert.doesNotThrow(function () {
                    grid._hideColumn('col:umn');
                }, null, '_hideColumn should escape special characters and not throw error');
                assert.strictEqual(query('.dgrid-cell')[0].offsetHeight, 0,
                    'Column should be hidden');
            });
 
            test.test('Hiding column during construction', function () {
                assert.doesNotThrow(function () {
                    grid = new ColumnHiderGrid({
                        id: 'i:d',
                        columns: [
                            { field: 'foo', id: 'col:umn', hidden: true }
                        ]
                    });
                }, null, 'ColumnHider should not throw error during construction');
                document.body.appendChild(grid.domNode);
                grid.startup();
                assert.strictEqual(query('.dgrid-cell')[0].offsetHeight, 0,
                    'Column should be hidden');
            });
        });
 
        test.suite('ColumnResizer', function () {
            var ColumnResizerGrid = declare([Grid, ColumnResizer]);
 
            test.afterEach(gridDestroy);
 
            test.test('Resizing column after construction', function () {
                assert.doesNotThrow(function () {
                    grid = new ColumnResizerGrid({
                        id: 'i:d',
                        columns: [
                            { field: 'foo', id: 'col:umn' },
                            { field: 'bar' }
                        ]
                    });
                }, null, 'ColumnResizer should not throw error during construction');
                document.body.appendChild(grid.domNode);
                grid.startup();
 
                assert.doesNotThrow(function () {
                    grid.resizeColumnWidth('col:umn', 100);
                }, null, 'resizeColumnWidth should escape special characters and not throw error');
                assert.strictEqual(query('.dgrid-cell')[0].offsetWidth, 100,
                    'Column should be the expected width');
            });
 
            test.test('Resizing column during construction', function () {
                assert.doesNotThrow(function () {
                    grid = new ColumnResizerGrid({
                        id: 'i:d',
                        columns: [
                            { field: 'foo', id: 'col:umn', width: 100 },
                            { field: 'bar' }
                        ]
                    });
                }, null, 'ColumnResizer should not throw error during construction');
                document.body.appendChild(grid.domNode);
                grid.startup();
                assert.strictEqual(query('.dgrid-cell')[0].offsetWidth, 100,
                    'Column should be the expected width');
            });
        });
    });
});