Skip to content

Commit 1994750

Browse files
authored
test: add LitElement tests for input mixins (#3795)
1 parent 3cbea7c commit 1994750

6 files changed

Lines changed: 383 additions & 236 deletions

File tree

Lines changed: 80 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,156 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync } from '@vaadin/testing-helpers';
3-
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
2+
import { fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpers';
43
import { DelegateStateMixin } from '../src/delegate-state-mixin.js';
4+
import { define } from './helpers.js';
5+
6+
const runTests = (baseClass) => {
7+
const tag = define[baseClass](
8+
'delegate-state-mixin',
9+
'<input id="input">',
10+
(Base) =>
11+
class extends DelegateStateMixin(Base) {
12+
static get properties() {
13+
return {
14+
title: {
15+
type: String,
16+
reflectToAttribute: true,
17+
},
18+
19+
invalid: {
20+
type: Boolean,
21+
reflectToAttribute: true,
22+
},
23+
24+
indeterminate: {
25+
type: Boolean,
26+
value: true,
27+
},
28+
};
29+
}
30+
31+
static get delegateAttrs() {
32+
return ['title', 'invalid'];
33+
}
34+
35+
static get delegateProps() {
36+
return ['indeterminate'];
37+
}
38+
39+
ready() {
40+
super.ready();
41+
42+
this.stateTarget = this.$.input;
43+
}
44+
},
45+
);
546

6-
customElements.define(
7-
'delegate-state-mixin-element',
8-
class extends DelegateStateMixin(PolymerElement) {
9-
static get properties() {
10-
return {
11-
title: {
12-
type: String,
13-
reflectToAttribute: true,
14-
},
15-
16-
invalid: {
17-
type: Boolean,
18-
reflectToAttribute: true,
19-
},
20-
21-
indeterminate: {
22-
type: Boolean,
23-
value: true,
24-
},
25-
};
26-
}
27-
28-
static get delegateAttrs() {
29-
return ['title', 'invalid'];
30-
}
31-
32-
static get delegateProps() {
33-
return ['indeterminate'];
34-
}
35-
36-
static get template() {
37-
return html`<input id="input" />`;
38-
}
39-
40-
ready() {
41-
super.ready();
42-
43-
this.stateTarget = this.$.input;
44-
}
45-
},
46-
);
47-
48-
describe('delegate-state-mixin', () => {
4947
let element, target;
5048

5149
describe('title attribute', () => {
5250
describe('default', () => {
53-
beforeEach(() => {
54-
element = fixtureSync('<delegate-state-mixin-element></delegate-state-mixin-element>');
51+
beforeEach(async () => {
52+
element = fixtureSync(`<${tag}></${tag}>`);
53+
await nextRender();
5554
target = element.stateTarget;
5655
});
5756

58-
it('should delegate title attribute to the target', () => {
57+
it('should delegate title attribute to the target', async () => {
5958
expect(target.hasAttribute('title')).to.be.false;
6059

6160
element.setAttribute('title', 'foo');
61+
await nextFrame();
6262
expect(target.getAttribute('title')).to.equal('foo');
6363
});
6464
});
6565

6666
describe('initially set', () => {
67-
beforeEach(() => {
68-
element = fixtureSync('<delegate-state-mixin-element title="foo"></delegate-state-mixin-element>');
67+
beforeEach(async () => {
68+
element = fixtureSync(`<${tag} title="foo"></${tag}>`);
69+
await nextRender();
6970
target = element.stateTarget;
7071
});
7172

72-
it('should delegate title attribute to the target', () => {
73+
it('should delegate title attribute to the target', async () => {
7374
expect(target.getAttribute('title')).to.equal('foo');
7475

7576
element.removeAttribute('title');
77+
await nextFrame();
7678
expect(target.hasAttribute('title')).to.be.false;
7779
});
7880
});
7981
});
8082

8183
describe('invalid attribute', () => {
8284
describe('default', () => {
83-
beforeEach(() => {
84-
element = fixtureSync('<delegate-state-mixin-element></delegate-state-mixin-element>');
85+
beforeEach(async () => {
86+
element = fixtureSync(`<${tag}></${tag}>`);
87+
await nextRender();
8588
target = element.stateTarget;
8689
});
8790

88-
it('should delegate invalid attribute to the target', () => {
91+
it('should delegate invalid attribute to the target', async () => {
8992
expect(target.hasAttribute('invalid')).to.be.false;
9093

9194
element.toggleAttribute('invalid', true);
95+
await nextFrame();
9296
expect(target.hasAttribute('invalid')).to.be.true;
9397
});
9498

95-
it('should delegate aria-invalid attribute to the target', () => {
99+
it('should delegate aria-invalid attribute to the target', async () => {
96100
expect(target.hasAttribute('aria-invalid')).to.be.false;
97101

98102
element.toggleAttribute('invalid', true);
103+
await nextFrame();
99104
expect(target.hasAttribute('aria-invalid')).to.be.true;
100105
});
101106
});
102107

103108
describe('initially set', () => {
104-
beforeEach(() => {
105-
element = fixtureSync('<delegate-state-mixin-element invalid></delegate-state-mixin-element>');
109+
beforeEach(async () => {
110+
element = fixtureSync(`<${tag} invalid></${tag}>`);
111+
await nextRender();
106112
target = element.stateTarget;
107113
});
108114

109-
it('should delegate invalid attribute to the target', () => {
115+
it('should delegate invalid attribute to the target', async () => {
110116
expect(target.hasAttribute('invalid')).to.be.true;
111117

112118
element.removeAttribute('invalid');
119+
await nextFrame();
113120
expect(target.hasAttribute('invalid')).to.be.false;
114121
});
115122

116-
it('should delegate aria-invalid attribute to the target', () => {
123+
it('should delegate aria-invalid attribute to the target', async () => {
117124
expect(target.hasAttribute('aria-invalid')).to.be.true;
118125

119126
element.removeAttribute('invalid');
127+
await nextFrame();
120128
expect(target.hasAttribute('aria-invalid')).to.be.false;
121129
});
122130
});
123131
});
124132

125133
describe('indeterminate property', () => {
126-
beforeEach(() => {
127-
element = fixtureSync('<delegate-state-mixin-element></delegate-state-mixin-element>');
134+
beforeEach(async () => {
135+
element = fixtureSync(`<${tag}></${tag}>`);
136+
await nextRender();
128137
target = element.stateTarget;
129138
});
130139

131-
it('should delegate indeterminate property to the target', () => {
140+
it('should delegate indeterminate property to the target', async () => {
132141
expect(target.indeterminate).to.be.true;
133142

134143
target.indeterminate = false;
144+
await nextFrame();
135145
expect(target.indeterminate).to.be.false;
136146
});
137147
});
148+
};
149+
150+
describe('DelegateStateMixin + Polymer', () => {
151+
runTests('polymer');
152+
});
153+
154+
describe('DelegateStateMixin + Lit', () => {
155+
runTests('lit');
138156
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
2+
import { html, LitElement } from 'lit';
3+
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
4+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
5+
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
6+
7+
export const define = {
8+
polymer: (prefix, htmlString, classFactory) => {
9+
const tag = `${prefix}-polymer-element`;
10+
11+
customElements.define(
12+
tag,
13+
class extends classFactory(ControllerMixin(PolymerElement)) {
14+
static get template() {
15+
const tpl = document.createElement('template');
16+
tpl.innerHTML = htmlString;
17+
return tpl;
18+
}
19+
},
20+
);
21+
22+
return tag;
23+
},
24+
lit: (prefix, htmlString, classFactory) => {
25+
const tag = `${prefix}-lit-element`;
26+
27+
customElements.define(
28+
tag,
29+
class extends classFactory(PolylitMixin(LitElement)) {
30+
render() {
31+
return html`${unsafeHTML(htmlString)}`;
32+
}
33+
},
34+
);
35+
36+
return tag;
37+
},
38+
};

0 commit comments

Comments
 (0)