Skip to content

Commit 70f7742

Browse files
committed
test: expect exception when using both template and renderer in grids
1 parent 98dde7f commit 70f7742

File tree

6 files changed

+208
-114
lines changed

6 files changed

+208
-114
lines changed

packages/vaadin-template-renderer/test/fixtures/mock-grid-host.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ export class MockGridHost extends PolymerElement {
99
static get template() {
1010
return html`
1111
<vaadin-grid id="grid" items="[[items]]">
12-
<vaadin-grid-column>
13-
<template class="header">header</template>
14-
<template class="footer">footer</template>
15-
<template class="body">[[item.title]]</template>
16-
</vaadin-grid-column>
17-
1812
<vaadin-grid-column>
1913
<template>
2014
<input class="parent-property" value="{{parentProperty::input}}" />
@@ -39,8 +33,6 @@ export class MockGridHost extends PolymerElement {
3933
<vaadin-grid-tree-column></vaadin-grid-tree-column>
4034
4135
<template class="row-details">
42-
row-details
43-
4436
<div class="index">[[index]]</div>
4537
<input class="title" value="{{item.title::input}}" />
4638
<vaadin-checkbox class="selected" checked="{{selected}}" />
@@ -68,8 +60,6 @@ export class MockGridHost extends PolymerElement {
6860
}
6961
};
7062
}
71-
72-
onClick() {}
7363
}
7464

7565
customElements.define('mock-grid-host', MockGridHost);

packages/vaadin-template-renderer/test/grid-body.test.js

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
1+
import sinon from 'sinon';
12
import { expect } from '@esm-bundle/chai';
23
import { fire, fixtureSync, nextRender } from '@vaadin/testing-helpers';
4+
import '@vaadin/vaadin-grid';
35
import { GridTemplatizer } from '../src/vaadin-template-renderer-grid-templatizer.js';
46

57
import '../vaadin-template-renderer.js';
68

79
import './fixtures/mock-grid-host.js';
810

11+
describe('grid body template', () => {
12+
function fixtureGrid() {
13+
return fixtureSync(`
14+
<vaadin-grid>
15+
<vaadin-grid-column>
16+
<template></template>
17+
</vaadin-grid-column>
18+
</vaadin-grid>
19+
`);
20+
}
21+
22+
it('should process the template', () => {
23+
const grid = fixtureGrid();
24+
const template = grid.querySelector('template');
25+
26+
expect(template.__templatizer).to.be.an.instanceof(GridTemplatizer);
27+
});
28+
29+
it('should throw when using both a template and a renderer', () => {
30+
const stub = sinon.stub(window.Vaadin, 'templateRendererCallback');
31+
const grid = fixtureGrid();
32+
const column = grid.firstElementChild;
33+
stub.restore();
34+
35+
column.renderer = () => {};
36+
37+
expect(() => window.Vaadin.templateRendererCallback(column)).to.throw(
38+
/^Cannot use both a template and a renderer for <vaadin-grid-column \/>\.$/
39+
);
40+
});
41+
});
42+
943
describe('grid body template', () => {
1044
let host, grid;
1145

@@ -26,63 +60,57 @@ describe('grid body template', () => {
2660
await nextRender(grid);
2761
});
2862

29-
it('should process the template', () => {
30-
const template = grid.querySelector('template.body');
31-
32-
expect(template.__templatizer).to.be.an.instanceof(GridTemplatizer);
33-
});
34-
3563
it('should render parentProperty', () => {
36-
const parentProperty0 = queryCellContent({ row: 0, col: 2 }, 'div.parent-property');
37-
const parentProperty1 = queryCellContent({ row: 1, col: 2 }, 'div.parent-property');
64+
const parentProperty0 = queryCellContent({ row: 0, col: 1 }, 'div.parent-property');
65+
const parentProperty1 = queryCellContent({ row: 1, col: 1 }, 'div.parent-property');
3866

3967
expect(parentProperty0.textContent).to.equal('parentValue');
4068
expect(parentProperty1.textContent).to.equal('parentValue');
4169
});
4270

4371
it('should render model.index', () => {
44-
const index0 = queryCellContent({ row: 0, col: 2 }, 'div.index');
45-
const index1 = queryCellContent({ row: 1, col: 2 }, 'div.index');
72+
const index0 = queryCellContent({ row: 0, col: 1 }, 'div.index');
73+
const index1 = queryCellContent({ row: 1, col: 1 }, 'div.index');
4674

4775
expect(index0.textContent).to.equal('0');
4876
expect(index1.textContent).to.equal('1');
4977
});
5078

5179
it('should render model.selected', () => {
52-
const selected0 = queryCellContent({ row: 0, col: 2 }, 'div.selected');
53-
const selected1 = queryCellContent({ row: 1, col: 2 }, 'div.selected');
80+
const selected0 = queryCellContent({ row: 0, col: 1 }, 'div.selected');
81+
const selected1 = queryCellContent({ row: 1, col: 1 }, 'div.selected');
5482

5583
expect(selected0.textContent).to.equal('false');
5684
expect(selected1.textContent).to.equal('false');
5785
});
5886

5987
it('should render model.expanded', () => {
60-
const expanded0 = queryCellContent({ row: 0, col: 2 }, 'div.expanded');
61-
const expanded1 = queryCellContent({ row: 1, col: 2 }, 'div.expanded');
88+
const expanded0 = queryCellContent({ row: 0, col: 1 }, 'div.expanded');
89+
const expanded1 = queryCellContent({ row: 1, col: 1 }, 'div.expanded');
6290

6391
expect(expanded0.textContent).to.equal('false');
6492
expect(expanded1.textContent).to.equal('false');
6593
});
6694

6795
it('should render model.detailsOpened', () => {
68-
const detailsOpened0 = queryCellContent({ row: 0, col: 2 }, 'div.details-opened');
69-
const detailsOpened1 = queryCellContent({ row: 1, col: 2 }, 'div.details-opened');
96+
const detailsOpened0 = queryCellContent({ row: 0, col: 1 }, 'div.details-opened');
97+
const detailsOpened1 = queryCellContent({ row: 1, col: 1 }, 'div.details-opened');
7098

7199
expect(detailsOpened0.textContent).to.equal('false');
72100
expect(detailsOpened1.textContent).to.equal('false');
73101
});
74102

75103
it('should render model.item.title', () => {
76-
const title0 = queryCellContent({ row: 0, col: 2 }, 'div.title');
77-
const title1 = queryCellContent({ row: 1, col: 2 }, 'div.title');
104+
const title0 = queryCellContent({ row: 0, col: 1 }, 'div.title');
105+
const title1 = queryCellContent({ row: 1, col: 1 }, 'div.title');
78106

79107
expect(title0.textContent).to.equal('item0');
80108
expect(title1.textContent).to.equal('item1');
81109
});
82110

83111
describe('changing parentProperty', () => {
84112
beforeEach(() => {
85-
const input = queryCellContent({ row: 0, col: 1 }, 'input.parent-property');
113+
const input = queryCellContent({ row: 0, col: 0 }, 'input.parent-property');
86114

87115
input.value = 'new';
88116
fire(input, 'input');
@@ -93,8 +121,8 @@ describe('grid body template', () => {
93121
});
94122

95123
it('should re-render the property in other rows', () => {
96-
const parentProperty0 = queryCellContent({ row: 0, col: 2 }, 'div.parent-property');
97-
const parentProperty1 = queryCellContent({ row: 1, col: 2 }, 'div.parent-property');
124+
const parentProperty0 = queryCellContent({ row: 0, col: 1 }, 'div.parent-property');
125+
const parentProperty1 = queryCellContent({ row: 1, col: 1 }, 'div.parent-property');
98126

99127
expect(parentProperty0.textContent).to.equal('new');
100128
expect(parentProperty1.textContent).to.equal('new');
@@ -103,7 +131,7 @@ describe('grid body template', () => {
103131

104132
describe('changing model.item.title', () => {
105133
beforeEach(() => {
106-
const input = queryCellContent({ row: 0, col: 1 }, 'input.title');
134+
const input = queryCellContent({ row: 0, col: 0 }, 'input.title');
107135

108136
input.value = 'new0';
109137
fire(input, 'input');
@@ -115,8 +143,8 @@ describe('grid body template', () => {
115143
});
116144

117145
it('should re-render the row', () => {
118-
const title0 = queryCellContent({ row: 0, col: 2 }, 'div.title');
119-
const title1 = queryCellContent({ row: 1, col: 2 }, 'div.title');
146+
const title0 = queryCellContent({ row: 0, col: 1 }, 'div.title');
147+
const title1 = queryCellContent({ row: 1, col: 1 }, 'div.title');
120148

121149
expect(title0.textContent).to.equal('new0');
122150
expect(title1.textContent).to.equal('item1');
@@ -137,9 +165,9 @@ describe('grid body template', () => {
137165
cb(items, 1);
138166
};
139167

140-
checkbox = getCell({ row: 0, col: 1 })._content.querySelector('vaadin-checkbox.expanded');
141-
expanded0 = getCell({ row: 0, col: 2 })._content.querySelector('div.expanded');
142-
expanded1 = getCell({ row: 1, col: 2 })._content.querySelector('div.expanded');
168+
checkbox = getCell({ row: 0, col: 0 })._content.querySelector('vaadin-checkbox.expanded');
169+
expanded0 = getCell({ row: 0, col: 1 })._content.querySelector('div.expanded');
170+
expanded1 = getCell({ row: 1, col: 1 })._content.querySelector('div.expanded');
143171
});
144172

145173
beforeEach(() => {
@@ -175,9 +203,9 @@ describe('grid body template', () => {
175203
let checkbox, selected0, selected1;
176204

177205
beforeEach(() => {
178-
checkbox = queryCellContent({ row: 0, col: 1 }, 'vaadin-checkbox.selected');
179-
selected0 = queryCellContent({ row: 0, col: 2 }, 'div.selected');
180-
selected1 = queryCellContent({ row: 1, col: 2 }, 'div.selected');
206+
checkbox = queryCellContent({ row: 0, col: 0 }, 'vaadin-checkbox.selected');
207+
selected0 = queryCellContent({ row: 0, col: 1 }, 'div.selected');
208+
selected1 = queryCellContent({ row: 1, col: 1 }, 'div.selected');
181209
});
182210

183211
beforeEach(() => {
@@ -213,9 +241,9 @@ describe('grid body template', () => {
213241
let checkbox, detailsOpened0, detailsOpened1;
214242

215243
beforeEach(() => {
216-
checkbox = queryCellContent({ row: 0, col: 1 }, 'vaadin-checkbox.details-opened');
217-
detailsOpened0 = queryCellContent({ row: 0, col: 2 }, 'div.details-opened');
218-
detailsOpened1 = queryCellContent({ row: 1, col: 2 }, 'div.details-opened');
244+
checkbox = queryCellContent({ row: 0, col: 0 }, 'vaadin-checkbox.details-opened');
245+
detailsOpened0 = queryCellContent({ row: 0, col: 1 }, 'div.details-opened');
246+
detailsOpened1 = queryCellContent({ row: 1, col: 1 }, 'div.details-opened');
219247
});
220248

221249
beforeEach(() => {
Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
1+
import sinon from 'sinon';
12
import { expect } from '@esm-bundle/chai';
23
import { fixtureSync, nextRender, enter } from '@vaadin/testing-helpers';
4+
import '@vaadin/vaadin-grid-pro';
35
import { GridTemplatizer } from '../src/vaadin-template-renderer-grid-templatizer.js';
46

57
import '../vaadin-template-renderer.js';
68

79
import './fixtures/mock-grid-pro-host.js';
810

11+
describe('grid editor template', () => {
12+
function fixtureGrid() {
13+
return fixtureSync(`
14+
<vaadin-grid-pro>
15+
<vaadin-grid-pro-edit-column>
16+
<template class="editor"></template>
17+
</vaadin-grid-pro-edit-column>
18+
</vaadin-grid-pro>
19+
`);
20+
}
21+
22+
it('should process the template', () => {
23+
const grid = fixtureGrid();
24+
const template = grid.querySelector('template.editor');
25+
26+
expect(template.__templatizer).to.be.an.instanceof(GridTemplatizer);
27+
});
28+
29+
it('should throw when using both a template and a renderer', () => {
30+
const stub = sinon.stub(window.Vaadin, 'templateRendererCallback');
31+
const grid = fixtureGrid();
32+
const column = grid.firstElementChild;
33+
stub.restore();
34+
35+
column.editModeRenderer = () => {};
36+
37+
expect(() => window.Vaadin.templateRendererCallback(column)).to.throw(
38+
/^Cannot use both a template and a renderer for <vaadin-grid-pro-edit-column \/>\.$/
39+
);
40+
});
41+
});
42+
943
describe('grid editor template', () => {
1044
let host, grid;
45+
let cell0, cell1, input;
1146

1247
function getCell({ row, col }) {
1348
const items = grid.$.items.children;
@@ -20,48 +55,36 @@ describe('grid editor template', () => {
2055
grid = host.$.grid;
2156

2257
await nextRender(grid);
23-
});
24-
25-
it('should process the template', () => {
26-
const template = grid.querySelector('template.editor');
27-
28-
expect(template.__templatizer).to.be.an.instanceof(GridTemplatizer);
29-
});
30-
31-
describe('editing', () => {
32-
let cell0, cell1, input;
3358

34-
beforeEach(() => {
35-
cell0 = getCell({ row: 0, col: 0 });
36-
cell1 = getCell({ row: 1, col: 0 });
59+
cell0 = getCell({ row: 0, col: 0 });
60+
cell1 = getCell({ row: 1, col: 0 });
3761

38-
enter(cell0._content);
62+
enter(cell0._content);
3963

40-
input = cell0._content.querySelector('input');
41-
});
64+
input = cell0._content.querySelector('input');
65+
});
4266

43-
it('should render the editor template', () => {
44-
expect(cell0._content.children).to.have.lengthOf(1);
45-
expect(cell0._content.children[0]).to.equal(input);
46-
expect(cell1._content.textContent).to.equal('item1');
47-
});
67+
it('should render the editor template', () => {
68+
expect(cell0._content.children).to.have.lengthOf(1);
69+
expect(cell0._content.children[0]).to.equal(input);
70+
expect(cell1._content.textContent).to.equal('item1');
71+
});
4872

49-
it('should render the body template when finishing editing', () => {
50-
enter(cell0._content);
73+
it('should render the body template when finishing editing', () => {
74+
enter(cell0._content);
5175

52-
expect(cell0._content.children[0]).not.to.equal(input);
53-
expect(cell0._content.textContent).to.equal('item0');
54-
expect(cell1._content.textContent).to.equal('item1');
55-
});
76+
expect(cell0._content.children[0]).not.to.equal(input);
77+
expect(cell0._content.textContent).to.equal('item0');
78+
expect(cell1._content.textContent).to.equal('item1');
79+
});
5680

57-
it('should re-render the body template after changing the value', () => {
58-
input.value = 'new0';
81+
it('should re-render the body template after changing the value', () => {
82+
input.value = 'new0';
5983

60-
enter(cell0._content);
84+
enter(cell0._content);
6185

62-
expect(cell0._content.children[0]).not.to.equal(input);
63-
expect(cell0._content.textContent).to.equal('new0');
64-
expect(cell1._content.textContent).to.equal('item1');
65-
});
86+
expect(cell0._content.children[0]).not.to.equal(input);
87+
expect(cell0._content.textContent).to.equal('new0');
88+
expect(cell1._content.textContent).to.equal('item1');
6689
});
6790
});

0 commit comments

Comments
 (0)