-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathtooltip-controller.test.js
More file actions
249 lines (200 loc) · 7.95 KB
/
tooltip-controller.test.js
File metadata and controls
249 lines (200 loc) · 7.95 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { expect } from '@vaadin/chai-plugins';
import { defineLit, fire, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import { PolylitMixin } from '../src/polylit-mixin.js';
import { TooltipController } from '../src/tooltip-controller.js';
describe('TooltipController', () => {
const tag = defineLit(
'tooltip-host',
`<slot></slot><slot name="tooltip"></slot>
`,
(Base) => class extends PolylitMixin(Base) {},
);
let host, tooltip, controller;
describe('slotted tooltip', () => {
beforeEach(() => {
host = fixtureSync(`
<${tag}>
<div>Target</div>
<vaadin-tooltip slot="tooltip"></vaadin-tooltip>
</${tag}>
`);
tooltip = host.querySelector('vaadin-tooltip');
controller = new TooltipController(host);
host.addController(controller);
});
it('should set has-tooltip attribute on the host', () => {
expect(host.hasAttribute('has-tooltip')).to.be.true;
});
it('should set tooltip target to the host itself by default', () => {
expect(tooltip.target).to.eql(host);
});
it('should update tooltip target using controller setTarget method', () => {
const target = host.querySelector('div');
controller.setTarget(target);
expect(tooltip.target).to.eql(target);
});
it('should update tooltip context using controller setContext method', () => {
const context = { foo: 'bar' };
controller.setContext(context);
expect(tooltip.context).to.eql(context);
});
it('should update tooltip manual using controller setManual method', () => {
controller.setManual(true);
expect(tooltip.manual).to.be.true;
controller.setManual(false);
expect(tooltip.manual).to.be.false;
});
it('should update tooltip shouldShow using controller shouldShow method', () => {
const shouldShow = () => true;
controller.setShouldShow(shouldShow);
expect(tooltip.shouldShow).to.be.eql(shouldShow);
});
it('should update tooltip _position using controller setPosition method', () => {
controller.setPosition('top-start');
expect(tooltip._position).to.eql('top-start');
});
it('should not set position property using controller setPosition method', () => {
controller.setPosition('top-start');
expect(tooltip.position).to.not.eql('top-start');
});
it('should update tooltip ariaTarget using controller setAriaTarget method', () => {
const input = document.createElement('input');
host.appendChild(input);
controller.setAriaTarget(input);
expect(tooltip.ariaTarget).to.equal(input);
});
it('should delegate open to the tooltip state controller', () => {
tooltip._stateController = { open: sinon.spy(), close: sinon.spy() };
controller.open({ hover: true });
expect(tooltip._stateController.open).to.be.calledOnceWith({ hover: true });
});
it('should not open the tooltip state controller when the tooltip is disconnected', () => {
tooltip._stateController = { open: sinon.spy(), close: sinon.spy() };
tooltip.remove();
controller.open({ hover: true });
expect(tooltip._stateController.open).to.be.not.called;
});
it('should delegate close to the tooltip state controller', () => {
tooltip._stateController = { open: sinon.spy(), close: sinon.spy() };
controller.close(true);
expect(tooltip._stateController.close).to.be.calledOnceWith(true);
});
});
describe('lazy tooltip', () => {
beforeEach(() => {
host = fixtureSync(`
<${tag}>
<div>Target</div>
</${tag}>
`);
controller = new TooltipController(host);
host.addController(controller);
tooltip = document.createElement('vaadin-tooltip');
tooltip.setAttribute('slot', 'tooltip');
});
it('should set tooltip target on the lazy tooltip to the host itself', async () => {
host.appendChild(tooltip);
await nextFrame();
expect(tooltip.target).to.eql(host);
});
it('should update lazy tooltip target using controller setTarget method', async () => {
const target = host.querySelector('div');
controller.setTarget(target);
host.appendChild(tooltip);
await nextFrame();
expect(tooltip.target).to.eql(target);
});
it('should update lazy tooltip context using controller setContext method', async () => {
const context = { foo: 'bar' };
controller.setContext(context);
host.appendChild(tooltip);
await nextFrame();
expect(tooltip.context).to.eql(context);
});
it('should update lazy tooltip manual using controller setManual method', async () => {
controller.setManual(true);
host.appendChild(tooltip);
await nextFrame();
expect(tooltip.manual).to.be.true;
controller.setManual(false);
expect(tooltip.manual).to.be.false;
});
it('should update lazy tooltip shouldShow using controller shouldShow method', async () => {
const shouldShow = () => true;
controller.setShouldShow(shouldShow);
host.appendChild(tooltip);
await nextFrame();
expect(tooltip.shouldShow).to.be.eql(shouldShow);
});
it('should update lazy tooltip _position using controller setPosition method', async () => {
controller.setPosition('top-start');
host.appendChild(tooltip);
await nextFrame();
expect(tooltip._position).to.eql('top-start');
});
it('should fire tooltip-changed event when the tooltip is added', async () => {
const spy = sinon.spy();
controller.addEventListener('tooltip-changed', spy);
host.appendChild(tooltip);
await nextFrame();
expect(spy).to.be.calledOnce;
});
it('should fire tooltip-changed event when the tooltip is removed', async () => {
const spy = sinon.spy();
controller.addEventListener('tooltip-changed', spy);
host.appendChild(tooltip);
await nextFrame();
host.removeChild(tooltip);
await nextFrame();
expect(spy).to.be.calledTwice;
});
it('should fire tooltip-changed event on tooltip content-changed', async () => {
const spy = sinon.spy();
controller.addEventListener('tooltip-changed', spy);
host.appendChild(tooltip);
await nextFrame();
spy.resetHistory();
fire(tooltip, 'content-changed');
await nextFrame();
expect(spy).to.be.calledOnce;
});
it('should not fire tooltip-changed event on tooltip content-changed after removing', async () => {
const spy = sinon.spy();
controller.addEventListener('tooltip-changed', spy);
host.appendChild(tooltip);
await nextFrame();
host.removeChild(tooltip);
await nextFrame();
spy.resetHistory();
fire(tooltip, 'content-changed');
await nextFrame();
expect(spy).to.be.not.called;
});
it('should set has-tooltip attribute on the host when tooltip is added', async () => {
expect(host.hasAttribute('has-tooltip')).to.be.false;
host.appendChild(tooltip);
await nextFrame();
expect(host.hasAttribute('has-tooltip')).to.be.true;
});
it('should remove has-tooltip attribute from the host when tooltip is removed', async () => {
host.appendChild(tooltip);
await nextFrame();
host.removeChild(tooltip);
await nextFrame();
expect(host.hasAttribute('has-tooltip')).to.be.false;
});
it('should not set has-tooltip attribute on the host when tooltip is added if manual', async () => {
controller.setManual(true);
host.appendChild(tooltip);
await nextFrame();
expect(host.hasAttribute('has-tooltip')).to.be.false;
});
it('should not throw when calling open before a tooltip is slotted', () => {
expect(() => controller.open({ hover: true })).to.not.throw();
});
it('should not throw when calling close before a tooltip is slotted', () => {
expect(() => controller.close(true)).to.not.throw();
});
});
});