Skip to content

Commit 22a16e9

Browse files
authored
Merge pull request #806 from mixonic/mixonic/rowcount-fix
Update test rowcount when collapse state changes
2 parents 020ad0b + 6503b3e commit 22a16e9

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

addon/components/ember-tbody/component.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import CollapseTree, { SELECT_MODE } from '../../-private/collapse-tree';
1111
import defaultTo from '../../-private/utils/default-to';
1212

1313
import layout from './template';
14-
import { assert } from '@ember/debug';
14+
import { assert, runInDebug } from '@ember/debug';
1515

1616
/**
1717
The table body component. This component manages the main bulk of the rows of
@@ -258,12 +258,24 @@ export default Component.extend({
258258

259259
this._updateCollapseTree();
260260

261+
runInDebug(() => {
262+
let scheduleUpdate = (this._scheduleUpdate = () => {
263+
run.scheduleOnce('actions', this, this._updateDataTestRowCount);
264+
});
265+
this.collapseTree.addObserver('rows', scheduleUpdate);
266+
this.collapseTree.addObserver('[]', scheduleUpdate);
267+
});
268+
261269
assert(
262270
'You must create an {{ember-thead}} with columns before creating an {{ember-tbody}}',
263271
!!this.get('unwrappedApi.columnTree')
264272
);
265273
},
266274

275+
_updateDataTestRowCount() {
276+
this.set('dataTestRowCount', this.get('collapseTree.length'));
277+
},
278+
267279
// eslint-disable-next-line
268280
_updateCollapseTree: observer(
269281
'unwrappedApi.{sorts,sortFunction,compareFunction,sortEmptyLast}',
@@ -295,6 +307,10 @@ export default Component.extend({
295307
this.rowMetaCache.delete(row);
296308
}
297309

310+
runInDebug(() => {
311+
this.collapseTree.removeObserver('rows', this._scheduleUpdate);
312+
this.collapseTree.removeObserver('[]', this._scheduleUpdate);
313+
});
298314
this.collapseTree.destroy();
299315
},
300316

@@ -309,11 +325,6 @@ export default Component.extend({
309325
this.collapseTree.set('rowMetaCache', this.rowMetaCache);
310326
this.collapseTree.set('rows', rows);
311327

312-
run.schedule('actions', () => {
313-
// eslint-disable-next-line ember-best-practices/no-side-effect-cp
314-
this.set('dataTestRowCount', this.get('wrappedRows.length')); // eslint-disable-line ember/no-side-effects
315-
});
316-
317328
return this.collapseTree;
318329
}),
319330

tests/integration/components/tree-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@ module('Integration | Tree', () => {
4747
await generateTable(this, { rowCount: 2, rowDepth: 2 });
4848

4949
assert.equal(table.rows.length, 6, 'renders all rows');
50+
assert.equal(table.body.rowCount, 6, 'total number of rows');
5051
assert.equal(table.getCell(0, 0).text, '0A', 'correct cell rendered');
5152
assert.equal(table.getCell(1, 0).text, '00A', 'correct cell rendered');
5253

5354
// toggle a row
5455
await table.rows.objectAt(0).toggleCollapse();
5556
assert.equal(table.rows.length, 4, 'rows were removed');
57+
assert.equal(table.body.rowCount, 4, 'total number of rows removed');
5658
assert.equal(table.getCell(0, 0).text, '0A', 'correct cell rendered');
5759
assert.equal(table.getCell(1, 0).text, '1A', 'correct cell rendered');
5860

5961
// uncollapse
6062
await table.rows.objectAt(0).toggleCollapse();
6163
assert.equal(table.rows.length, 6, 'rows were removed');
64+
assert.equal(table.body.rowCount, 6, 'total number of rows removed');
6265
assert.equal(table.getCell(0, 0).text, '0A', 'correct cell rendered');
6366
assert.equal(table.getCell(1, 0).text, '00A', 'correct cell rendered');
6467
});

0 commit comments

Comments
 (0)