|
1 | 1 | 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'; |
4 | 3 | 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 | + ); |
5 | 46 |
|
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', () => { |
49 | 47 | let element, target; |
50 | 48 |
|
51 | 49 | describe('title attribute', () => { |
52 | 50 | 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(); |
55 | 54 | target = element.stateTarget; |
56 | 55 | }); |
57 | 56 |
|
58 | | - it('should delegate title attribute to the target', () => { |
| 57 | + it('should delegate title attribute to the target', async () => { |
59 | 58 | expect(target.hasAttribute('title')).to.be.false; |
60 | 59 |
|
61 | 60 | element.setAttribute('title', 'foo'); |
| 61 | + await nextFrame(); |
62 | 62 | expect(target.getAttribute('title')).to.equal('foo'); |
63 | 63 | }); |
64 | 64 | }); |
65 | 65 |
|
66 | 66 | 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(); |
69 | 70 | target = element.stateTarget; |
70 | 71 | }); |
71 | 72 |
|
72 | | - it('should delegate title attribute to the target', () => { |
| 73 | + it('should delegate title attribute to the target', async () => { |
73 | 74 | expect(target.getAttribute('title')).to.equal('foo'); |
74 | 75 |
|
75 | 76 | element.removeAttribute('title'); |
| 77 | + await nextFrame(); |
76 | 78 | expect(target.hasAttribute('title')).to.be.false; |
77 | 79 | }); |
78 | 80 | }); |
79 | 81 | }); |
80 | 82 |
|
81 | 83 | describe('invalid attribute', () => { |
82 | 84 | 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(); |
85 | 88 | target = element.stateTarget; |
86 | 89 | }); |
87 | 90 |
|
88 | | - it('should delegate invalid attribute to the target', () => { |
| 91 | + it('should delegate invalid attribute to the target', async () => { |
89 | 92 | expect(target.hasAttribute('invalid')).to.be.false; |
90 | 93 |
|
91 | 94 | element.toggleAttribute('invalid', true); |
| 95 | + await nextFrame(); |
92 | 96 | expect(target.hasAttribute('invalid')).to.be.true; |
93 | 97 | }); |
94 | 98 |
|
95 | | - it('should delegate aria-invalid attribute to the target', () => { |
| 99 | + it('should delegate aria-invalid attribute to the target', async () => { |
96 | 100 | expect(target.hasAttribute('aria-invalid')).to.be.false; |
97 | 101 |
|
98 | 102 | element.toggleAttribute('invalid', true); |
| 103 | + await nextFrame(); |
99 | 104 | expect(target.hasAttribute('aria-invalid')).to.be.true; |
100 | 105 | }); |
101 | 106 | }); |
102 | 107 |
|
103 | 108 | 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(); |
106 | 112 | target = element.stateTarget; |
107 | 113 | }); |
108 | 114 |
|
109 | | - it('should delegate invalid attribute to the target', () => { |
| 115 | + it('should delegate invalid attribute to the target', async () => { |
110 | 116 | expect(target.hasAttribute('invalid')).to.be.true; |
111 | 117 |
|
112 | 118 | element.removeAttribute('invalid'); |
| 119 | + await nextFrame(); |
113 | 120 | expect(target.hasAttribute('invalid')).to.be.false; |
114 | 121 | }); |
115 | 122 |
|
116 | | - it('should delegate aria-invalid attribute to the target', () => { |
| 123 | + it('should delegate aria-invalid attribute to the target', async () => { |
117 | 124 | expect(target.hasAttribute('aria-invalid')).to.be.true; |
118 | 125 |
|
119 | 126 | element.removeAttribute('invalid'); |
| 127 | + await nextFrame(); |
120 | 128 | expect(target.hasAttribute('aria-invalid')).to.be.false; |
121 | 129 | }); |
122 | 130 | }); |
123 | 131 | }); |
124 | 132 |
|
125 | 133 | 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(); |
128 | 137 | target = element.stateTarget; |
129 | 138 | }); |
130 | 139 |
|
131 | | - it('should delegate indeterminate property to the target', () => { |
| 140 | + it('should delegate indeterminate property to the target', async () => { |
132 | 141 | expect(target.indeterminate).to.be.true; |
133 | 142 |
|
134 | 143 | target.indeterminate = false; |
| 144 | + await nextFrame(); |
135 | 145 | expect(target.indeterminate).to.be.false; |
136 | 146 | }); |
137 | 147 | }); |
| 148 | +}; |
| 149 | + |
| 150 | +describe('DelegateStateMixin + Polymer', () => { |
| 151 | + runTests('polymer'); |
| 152 | +}); |
| 153 | + |
| 154 | +describe('DelegateStateMixin + Lit', () => { |
| 155 | + runTests('lit'); |
138 | 156 | }); |
0 commit comments