Skip to content

Commit

Permalink
fix: clear details-opened attribute when item is removed (#225) (CP: 6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker committed Apr 28, 2021
1 parent 5b579d7 commit c51af02
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/vaadin-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ class GridElement extends ElementMixin(
this._a11yUpdateRowSelected(row, model.selected);
this._a11yUpdateRowLevel(row, model.level);
this._toggleAttribute('expanded', model.expanded, row);
this._toggleAttribute('details-opened', this._isDetailsOpened(item), row);
if (this._rowDetailsTemplate || this.rowDetailsRenderer) {
this._toggleDetailsCell(row, item);
}
Expand Down
93 changes: 76 additions & 17 deletions test/row-details.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { fixtureSync, nextFrame } from '@open-wc/testing-helpers';
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import '@polymer/polymer/lib/elements/dom-repeat.js';
import {
buildDataSet,
click,
flushGrid,
getBodyCellContent,
getCellContent,
getRows,
getRowCells,
getRows,
infiniteDataProvider,
scrollToEnd
} from './helpers.js';
Expand Down Expand Up @@ -49,6 +50,14 @@ describe('row details', () => {
let grid;
let bodyRows;

function openRowDetails(index) {
grid.openItemDetails(grid._cache.items[index]);
}

function closeRowDetails(index) {
grid.closeItemDetails(grid._cache.items[index]);
}

it('should not increase row update count', () => {
grid = fixtureSync(`
<vaadin-grid style="width: 50px; height: 400px" size="100">
Expand All @@ -66,10 +75,6 @@ describe('row details', () => {
});

describe('simple', () => {
function openRowDetails(index) {
grid.openItemDetails(grid._cache.items[index]);
}

beforeEach(async () => {
grid = fixtureSync(`
<vaadin-grid style="width: 50px; height: 400px" size="100">
Expand All @@ -85,10 +90,6 @@ describe('row details', () => {
await nextFrame();
});

function closeRowDetails(index) {
grid.closeItemDetails(grid._cache.items[index]);
}

it('should not activate on click', () => {
openRowDetails(0);
const detailsCellContent = getBodyCellContent(grid, 0, 1);
Expand Down Expand Up @@ -157,13 +158,6 @@ describe('row details', () => {
assertDetailsBounds();
});

it('should have state attribute', () => {
openRowDetails(1);
expect(bodyRows[1].hasAttribute('details-opened')).to.be.true;
closeRowDetails(1);
expect(bodyRows[1].hasAttribute('details-opened')).to.be.false;
});

it('should have correct bounds when modified after opening', async () => {
openRowDetails(1);
const cells = getRowCells(bodyRows[1]);
Expand Down Expand Up @@ -297,4 +291,69 @@ describe('row details', () => {
expect(row.offsetHeight).to.be.above(70);
});
});

describe('details opened attribute', () => {
let dataset = [];
const dataProvider = (params, callback) => callback(dataset);

const countRowsMarkedAsDetailsOpened = (grid) => {
return grid.$.items.querySelectorAll('tr[details-opened]').length;
};

beforeEach(async () => {
dataset = buildDataSet(10);
grid = fixtureSync(`
<vaadin-grid style="width: 50px; height: 400px" size="100">
<template class="row-details"><span>[[index]]</span>-details</template>
<vaadin-grid-column>
<template>[[index]]</template>
</vaadin-grid-column>
</vaadin-grid>
`);
grid.dataProvider = dataProvider;
flushGrid(grid);
bodyRows = getRows(grid.$.items);
await nextFrame();
});

it('should update when opening/closing imperatively', () => {
openRowDetails(1);
expect(bodyRows[1].hasAttribute('details-opened')).to.be.true;
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(1);
closeRowDetails(1);
expect(bodyRows[1].hasAttribute('details-opened')).to.be.false;
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
});

it('should be removed when item is removed', () => {
openRowDetails(0);
dataset.shift(); // remove opened item
grid.clearCache();

expect(bodyRows[0].hasAttribute('details-opened')).to.be.false;
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
});

it('should be removed when items are replaced', () => {
openRowDetails(0);
dataset = buildDataSet(10); // replace data
grid.clearCache();

expect(bodyRows[0].hasAttribute('details-opened')).to.be.false;
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
});

it('should be removed on all rows when items are replaced', () => {
// Open all rows
dataset.forEach((_, i) => {
openRowDetails(i);
});
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(dataset.length);

dataset = buildDataSet(10); // replace data
grid.clearCache();

expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
});
});
});

0 comments on commit c51af02

Please sign in to comment.