Skip to content

Commit

Permalink
Add unit tests for cleaning up cell editor listeners
Browse files Browse the repository at this point in the history
Fixes dojo#1237
  • Loading branch information
maier49 committed Jan 7, 2016
1 parent 9b40f94 commit ce55feb
Showing 1 changed file with 121 additions and 4 deletions.
125 changes: 121 additions & 4 deletions test/intern/mixins/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ define([
'intern!tdd',
'intern/chai!assert',
'dojo/_base/declare',
'dojo/aspect',
'dojo/_base/lang',
'dojo/Deferred',
'dojo/on',
'dojo/promise/all',
Expand All @@ -13,10 +13,12 @@ define([
'dgrid/Grid',
'dgrid/OnDemandGrid',
'dgrid/Editor',
'dgrid/extensions/Pagination',
'dgrid/test/data/createSyncStore',
'dgrid/test/data/orderedData'
], function (test, assert, declare, aspect, Deferred, on, all, query, when, registry, TextBox,
Grid, OnDemandGrid, Editor, createSyncStore, orderedData) {
'dgrid/test/data/orderedData',
'../addCss!'
], function (test, assert, declare, lang, Deferred, on, all, query, when, registry, TextBox,
Grid, OnDemandGrid, Editor, Pagination, createSyncStore, orderedData) {

var testOrderedData = orderedData.items,
EditorGrid = declare([ Grid, Editor ]),
Expand Down Expand Up @@ -489,5 +491,120 @@ define([
assert.strictEqual(cell.element.innerHTML, cellContent, 'Widget should be identical to before refresh');
});
});

test.suite('Cleanup editor listeners', function () {
function testCellListeners(rowCount, editorColumnCount) {
var rowEntryCount = 0;
for (var rowKey in grid._editorCellListeners) {
rowEntryCount ++;
var listenerCount = 0;
var rowCellListeners = grid._editorCellListeners[rowKey];

for (var cellKey in rowCellListeners) {
assert.ok(rowCellListeners[cellKey].remove, 'Object in hash was not a listener');
listenerCount++;
}

assert.strictEqual(listenerCount, editorColumnCount, 'Should have two listeners per row');
}
assert.strictEqual(rowEntryCount, rowCount, 'Should only have one entry for each row');
}

function buildPaginatedGrid (GridConstructor, rowsPerPage) {
return new GridConstructor({
rowsPerPage: rowsPerPage || 10,
columns: {
name: {
editor: 'text',
editOn: 'click'
},
description: {
editor: 'text',
editOn: 'click'
}
},
collection: createSyncStore({
data: testOrderedData,
idProperty: 'order'
})
});
}
test.test('clean up after refresh cell', function () {
// Test with Pagination mixed in first
grid = buildPaginatedGrid(declare([Grid, Pagination, Editor]));
document.body.appendChild(grid.domNode);
grid.startup();
for (var i = 1; i < 10;i ++) {
grid.refreshCell(grid.cell(i, 'name'));
grid.refreshCell(grid.cell(i, 'description'));
}
testCellListeners(9, 2);
grid.destroy();

// Test with Editor mixed in first
grid = buildPaginatedGrid(declare([Grid, Editor, Pagination]));
document.body.appendChild(grid.domNode);
grid.startup();
for (i = 1; i < 10;i ++) {
grid.refreshCell(grid.cell(i, 'name'));
grid.refreshCell(grid.cell(i, 'description'));
}
testCellListeners(9, 2);
});

test.test('clean up after row is rerendered', function () {
grid = buildPaginatedGrid(declare([Grid, Pagination, Editor]));
document.body.appendChild(grid.domNode);
grid.startup();

for (var i = 0; i < 9; i++) {
grid.collection.put(
lang.mixin(lang.clone(orderedData.items[i]), {description: 'modified'})
);
}
testCellListeners(9, 2);
});

test.test('clean up when switching pages', function () {
grid = buildPaginatedGrid(declare([Grid, Pagination, Editor]), 5);
document.body.appendChild(grid.domNode);
grid.startup();

testCellListeners(5, 2);
grid.gotoPage(2);
testCellListeners(4, 2);
});

test.test('clean up when scrolling', function () {
var data = [];
for (var i = 0; i < 1000; i++) {
data[i] = {
id: i,
name: 'Name ' + i
};
}
grid = new (declare([OnDemandGrid, Editor]))({
columns: {
name: {
editor: 'text',
editOn: 'click'
}
},
minRowsPerPage: 250,
collection: createSyncStore({
data: data
})
});

document.body.appendChild(grid.domNode);
grid.startup();

testCellListeners(250, 1);
grid.bodyNode.scrollTop = '10000';
return when(grid._processScroll(), function () {
testCellListeners(250, 1);
});
});
});
});
});

0 comments on commit ce55feb

Please sign in to comment.