-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathinput-mixin.test.js
187 lines (157 loc) · 5.43 KB
/
input-mixin.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { expect } from '@vaadin/chai-plugins';
import { defineLit, definePolymer, fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
import { InputMixin } from '../src/input-mixin.js';
const runTests = (defineHelper, baseMixin) => {
const tag = defineHelper(
'input-mixin',
'<slot name="input"></slot>',
(Base) => class extends InputMixin(baseMixin(Base)) {},
);
let element, input;
describe('type', () => {
beforeEach(() => {
element = fixtureSync(`<${tag}></${tag}>`);
});
it('should have a read-only type property', async () => {
expect(element.type).to.be.undefined;
element.type = 'number';
await nextUpdate(element);
expect(element.type).to.be.undefined;
});
});
describe('value', () => {
beforeEach(async () => {
element = fixtureSync(`<${tag}></${tag}>`);
await nextRender();
input = document.createElement('input');
input.setAttribute('slot', 'input');
element.appendChild(input);
element._setInputElement(input);
await nextRender();
});
it('should set property to empty string by default', () => {
expect(element.value).to.be.equal('');
});
it('should not set has-value attribute by default', () => {
expect(element.hasAttribute('has-value')).to.be.false;
});
it('should set has-value attribute when value attribute is set', async () => {
element.setAttribute('value', 'test');
await nextUpdate(element);
expect(element.hasAttribute('has-value')).to.be.true;
});
it('should set has-value attribute when value property is set', async () => {
element.value = 'test';
await nextUpdate(element);
expect(element.hasAttribute('has-value')).to.be.true;
});
it('should remove has-value attribute when value is removed', async () => {
element.value = 'foo';
await nextUpdate(element);
element.value = '';
await nextUpdate(element);
expect(element.hasAttribute('has-value')).to.be.false;
});
it('should propagate value to the input element', async () => {
element.value = 'foo';
await nextUpdate(element);
expect(input.value).to.equal('foo');
});
it('should clear input value when value is set to null', async () => {
element.value = 'foo';
await nextUpdate(element);
element.value = null;
await nextUpdate(element);
expect(input.value).to.equal('');
});
it('should clear input value when value is set to undefined', async () => {
element.value = 'foo';
await nextUpdate(element);
element.value = undefined;
await nextUpdate(element);
expect(input.value).to.equal('');
});
it('should update field value on the input event', () => {
input.value = 'foo';
input.dispatchEvent(new Event('input'));
expect(element.value).to.equal('foo');
});
it('should clear the field value on clear method call', async () => {
element.value = 'foo';
await nextUpdate(element);
element.clear();
await nextUpdate(element);
expect(element.value).to.equal('');
});
it('should clear the input value on clear method call', async () => {
element.value = 'foo';
await nextUpdate(element);
element.clear();
await nextUpdate(element);
expect(input.value).to.equal('');
});
});
describe('events', () => {
let eventsTag, inputSpy, changeSpy;
before(() => {
inputSpy = sinon.spy();
changeSpy = sinon.spy();
eventsTag = defineHelper(
'input-mixin-events',
'<slot name="input"></slot>',
(Base) =>
class extends InputMixin(baseMixin(Base)) {
_onInput() {
inputSpy();
}
_onChange() {
changeSpy();
}
},
);
});
beforeEach(async () => {
element = fixtureSync(`<${eventsTag}></${eventsTag}>`);
await nextRender();
input = document.createElement('input');
element.appendChild(input);
element._setInputElement(input);
await nextRender();
});
afterEach(() => {
inputSpy.resetHistory();
changeSpy.resetHistory();
});
it('should call an input event listener', () => {
input.dispatchEvent(new CustomEvent('input'));
expect(inputSpy.calledOnce).to.be.true;
});
it('should call a change event listener', () => {
input.dispatchEvent(new CustomEvent('change'));
expect(changeSpy.calledOnce).to.be.true;
});
it('should not call an input event listener when input is unset', async () => {
element.removeChild(input);
element._setInputElement(undefined);
await nextRender();
input.dispatchEvent(new CustomEvent('input'));
expect(inputSpy.called).to.be.false;
});
it('should not call a change event listener when input is unset', async () => {
element.removeChild(input);
element._setInputElement(undefined);
await nextRender();
input.dispatchEvent(new CustomEvent('change'));
expect(changeSpy.called).to.be.false;
});
});
};
describe('InputMixin + Polymer', () => {
runTests(definePolymer, ControllerMixin);
});
describe('InputMixin + Lit', () => {
runTests(defineLit, PolylitMixin);
});