Skip to content

Commit 1ab3a80

Browse files
authored
feat: throw when using both a template and a renderer (#2098)
* feat: throw when using both a template and a renderer * test: expect exception when using both template and renderer in grids
1 parent 70e820c commit 1ab3a80

9 files changed

Lines changed: 254 additions & 121 deletions

File tree

‎packages/vaadin-template-renderer/src/vaadin-template-renderer.js‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,52 @@ function createRenderer(component, template, TemplatizerClass = Templatizer) {
1414
};
1515

1616
template.__templatizer = templatizer;
17+
renderer.__templatized = true;
1718

1819
return renderer;
1920
}
2021

22+
function assignRenderer(component, rendererName, renderer) {
23+
const oldRenderer = component[rendererName];
24+
25+
if (oldRenderer && !oldRenderer.__templatized) {
26+
const tag = component.localName;
27+
28+
throw new Error(`Cannot use both a template and a renderer for <${tag} />.`);
29+
}
30+
31+
component[rendererName] = renderer;
32+
}
33+
2134
function processGridTemplate(grid, template) {
2235
if (template.matches('.row-details')) {
23-
grid.rowDetailsRenderer = createRenderer(grid, template, GridTemplatizer);
36+
const renderer = createRenderer(grid, template, GridTemplatizer);
37+
assignRenderer(grid, 'rowDetailsRenderer', renderer);
2438
return;
2539
}
2640
}
2741

2842
function processGridColumnTemplate(column, template) {
2943
if (template.matches('.header')) {
30-
column.headerRenderer = createRenderer(column, template);
44+
const renderer = createRenderer(column, template);
45+
assignRenderer(column, 'headerRenderer', renderer);
3146
return;
3247
}
3348

3449
if (template.matches('.footer')) {
35-
column.footerRenderer = createRenderer(column, template);
50+
const renderer = createRenderer(column, template);
51+
assignRenderer(column, 'footerRenderer', renderer);
3652
return;
3753
}
3854

3955
if (template.matches('.editor')) {
40-
column.editModeRenderer = createRenderer(column, template, GridTemplatizer);
56+
const renderer = createRenderer(column, template, GridTemplatizer);
57+
assignRenderer(column, 'editModeRenderer', renderer);
4158
return;
4259
}
4360

44-
column.renderer = createRenderer(column, template, GridTemplatizer);
61+
const renderer = createRenderer(column, template, GridTemplatizer);
62+
assignRenderer(column, 'renderer', renderer);
4563
}
4664

4765
function processTemplate(component, template) {
@@ -55,7 +73,8 @@ function processTemplate(component, template) {
5573
return;
5674
}
5775

58-
component.renderer = createRenderer(component, template);
76+
const renderer = createRenderer(component, template);
77+
assignRenderer(component, 'renderer', renderer);
5978
}
6079

6180
function processTemplates(component) {

‎packages/vaadin-template-renderer/test/fixtures/mock-component.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class MockComponent extends HTMLElement {
66
}
77

88
connectedCallback() {
9-
window.Vaadin.templateRendererCallback?.(this);
9+
window.Vaadin.templateRendererCallback(this);
1010
}
1111

1212
get $() {
@@ -20,6 +20,10 @@ export class MockComponent extends HTMLElement {
2020
this.render();
2121
}
2222

23+
get renderer() {
24+
return this.__renderer;
25+
}
26+
2327
render() {
2428
this.__renderer?.(this.$.content);
2529
}

‎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(() => {

0 commit comments

Comments
 (0)