Skip to content

Commit b982004

Browse files
authored
Merge pull request #759 from BY00565233/SURF-2034
test(hx-tooltip): implement component tests
2 parents 637b80d + 1ed8cd3 commit b982004

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
import { fixture, oneEvent, expect } from '@open-wc/testing';
2+
3+
/**
4+
* <hx-tooltip> component tests
5+
*
6+
* @type HXTooltipElement
7+
*
8+
*/
9+
describe('<hx-tooltip> component tests', () => {
10+
const template = '<hx-tooltip>';
11+
const mockup = `
12+
<hx-tooltip for="icon1"
13+
position="top-center"
14+
hx-defined=""
15+
aria-hidden="true"
16+
id="tip-1uucxkzw"
17+
role="tooltip"
18+
>
19+
Top Center Tooltip
20+
</hx-tooltip>`;
21+
const mockupId = `
22+
<hx-tooltip for="iconCtrlEl" position="bottom-center">
23+
Bottom Center Tooltip
24+
</hx-tooltip>
25+
<p>
26+
<hx-icon id="iconCtrlEl" type="help-circle"></hx-icon>
27+
</p>`;
28+
29+
describe('test instantiate element', () => {
30+
it('should be instantiated with hx-defined attribute', async () => {
31+
const component = /** @type {HXTooltipElement} */ await fixture(template);
32+
const attr = component.hasAttribute('hx-defined');
33+
34+
expect(attr).to.be.true;
35+
});
36+
37+
it('should not be hidden', async () => {
38+
const component = /** @type {HXTooltipElement} */ await fixture(template);
39+
const prop = component.hidden;
40+
41+
expect(prop).to.be.false;
42+
});
43+
44+
it(`the rendered Light DOM should NOT equal simple template ${template}`, async () => {
45+
const component = /** @type {HXTooltipElement} */ await fixture(template);
46+
47+
expect(component).lightDom.to.not.equal(template);
48+
});
49+
50+
});
51+
52+
describe('test Shadow DOM', () => {
53+
describe('verify render', () => {
54+
it('should have a static Shadow DOM', async function () {
55+
const component = /** @type {HXTooltipElement} */ await fixture(template);
56+
const shadow = component.shadowRoot.innerHTML;
57+
58+
expect(component).shadowDom.to.equal(shadow);
59+
});
60+
61+
it('should render the Shadow Root mode open', async () => {
62+
const component = /** @type {HXTooltipElement} */ await fixture(template);
63+
const mode = component.shadowRoot.mode;
64+
65+
expect(mode).to.equal('open');
66+
});
67+
68+
it('should have a single <slot>', async () => {
69+
const component = /** @type {HXTooltipElement} */ await fixture(template);
70+
const shadow = component.shadowRoot;
71+
const query = shadow.querySelectorAll('slot');
72+
const len = query.length;
73+
74+
expect(len).to.be.equal(1);
75+
});
76+
77+
it('should have an unnamed <slot>', async () => {
78+
const component = /** @type {HXTooltipElement} */ await fixture(template);
79+
const name = component.slot;
80+
81+
if (name !== null) {
82+
expect(name).to.be.equal('');
83+
} else {
84+
expect(name).to.be.null; // IE11, Legacy Edge, and older browsers
85+
}
86+
});
87+
});
88+
89+
describe('verify Shadow DOM markup', () => {
90+
it('markup should contain a #hxTooltip div', async () => {
91+
const elSelector = 'div#hxTooltip';
92+
const id = 'hxTooltip';
93+
const component = /** @type {HXTooltipElement} */ await fixture(template);
94+
const shadow = component.shadowRoot;
95+
const query = shadow.querySelector(elSelector);
96+
const queryId = query.id;
97+
98+
expect(queryId).to.equal(id);
99+
});
100+
});
101+
});
102+
103+
describe('verify onCreate method', () => {
104+
it('test for default position', async () => {
105+
const component = /** @type {HXTooltipElement} */ await fixture(template);
106+
const defaultPosition = 'top-center';
107+
const attr = component.hasAttribute('position');
108+
const tooltipPosition = component.getAttribute('position');
109+
110+
expect(attr).to.be.true;
111+
expect(tooltipPosition).to.equal(defaultPosition);
112+
});
113+
114+
it('test for position offset', async () => {
115+
const component = /** @type {HXTooltipElement} */ await fixture(template);
116+
const positionOffset = 20;
117+
const offsetValue = component.POSITION_OFFSET;
118+
119+
expect(offsetValue).to.equal(positionOffset);
120+
});
121+
122+
it('test for checking all positions', async () => {
123+
const component = /** @type {HXTooltipElement} */ await fixture(template);
124+
const position = component.getAttribute('position');
125+
const changePosition = 'right-middle';
126+
component.setShadowPosition(changePosition);
127+
const newPosition = component.shadowRoot.querySelector('div').getAttribute('position')
128+
129+
expect(newPosition).to.not.equal(position);
130+
expect(newPosition).to.equal(changePosition);
131+
});
132+
});
133+
134+
describe('test $onConnect method', () => {
135+
it('should have a randomly generated or assigned ID on render', async () => {
136+
const fragment = /** @type {HXTooltipElement} */ await fixture(mockup);
137+
const attr = fragment.hasAttribute('id');
138+
const tooltipId = fragment.getAttribute('id');
139+
140+
expect(attr).to.be.true;
141+
expect(tooltipId).to.not.be.null;
142+
});
143+
144+
it('should have role attribute default to tooltip', async () => {
145+
const roleAttr = 'tooltip';
146+
const component = /** @type {HXTooltipElement} */ await fixture(template);
147+
const attr = component.hasAttribute('role');
148+
const role = component.getAttribute('role');
149+
150+
expect(attr).to.be.true;
151+
expect(role).to.equal(roleAttr);
152+
});
153+
});
154+
155+
describe('test <hx-tooltip> getters and setters', () => {
156+
it('should get *for* value', async () => {
157+
const forValue = 'icon1';
158+
const component = /** @type {HXTooltipElement} */ await fixture(mockup);
159+
const attr = component.htmlFor;
160+
161+
expect(attr).to.equal(forValue);
162+
});
163+
164+
it('should set *for* attribute value', async () => {
165+
const component = /** @type {HXTooltipElement} */ await fixture(template);
166+
const value = 'newIcon';
167+
const attr = component.hasAttribute('for');
168+
169+
component.htmlFor = value;
170+
const forValue = component.getAttribute('for');
171+
172+
expect(attr).to.be.false;
173+
expect(forValue).to.equal(value);
174+
});
175+
176+
it('should set shadow root to new position', async () => {
177+
const fragment = /** @type {HXTooltipElement} */ await fixture(mockup);
178+
const newPosition = 'bottom-center';
179+
180+
fragment.setShadowPosition(newPosition);
181+
182+
const tooltipPosition = fragment.shadowRoot.querySelector('div').getAttribute('position');
183+
184+
expect(tooltipPosition).to.equal(newPosition);
185+
});
186+
187+
it('should get control element id of iconCtrlEl', async () => {
188+
const fragment = /** @type {HXTooltipElement} */ await fixture(mockupId);
189+
const id = 'iconCtrlEl';
190+
const ctrlElement = fragment.controlElement;
191+
const ctrlElementID = ctrlElement.id;
192+
193+
expect(ctrlElementID).to.equal(id);
194+
});
195+
});
196+
197+
describe('test for event listeners', () => {
198+
it('should fire a blur event', async () => {
199+
const component = /** @type {HXTooltipElement} */ await fixture(template);
200+
const detail = { evt: 'blur!'};
201+
const customEvent = new CustomEvent('blur', { detail });
202+
203+
setTimeout(() => component.dispatchEvent(customEvent));
204+
const evt = await oneEvent(component, 'blur');
205+
206+
expect(evt).to.equal(customEvent);
207+
expect(evt.detail).to.equal(detail);
208+
});
209+
210+
it('should fire a focus event', async () => {
211+
const component = /** @type {HXTooltipElement} */ await fixture(template);
212+
const detail = { evt: 'focus'};
213+
const customEvent = new CustomEvent('focus', { detail });
214+
215+
setTimeout(() => component.dispatchEvent(customEvent));
216+
const evt = await oneEvent(component, 'focus');
217+
218+
expect(evt).to.equal(customEvent);
219+
expect(evt.detail).to.equal(detail);
220+
});
221+
222+
it('should fire a mouseenter event', async () => {
223+
const component = /** @type {HXTooltipElement} */ await fixture(template);
224+
const detail = { evt: 'mouseenter'};
225+
const customEvent = new CustomEvent('mouseenter', { detail });
226+
227+
setTimeout(() => component.dispatchEvent(customEvent));
228+
const evt = await oneEvent(component, 'mouseenter');
229+
230+
expect(evt).to.equal(customEvent);
231+
expect(evt.detail).to.equal(detail);
232+
});
233+
234+
it('should fire a mouseleave event', async () => {
235+
const component = /** @type {HXTooltipElement} */ await fixture(template);
236+
const detail = { evt: 'mouseleave'};
237+
const customEvent = new CustomEvent('mouseleave', { detail });
238+
239+
setTimeout(() => component.dispatchEvent(customEvent));
240+
const evt = await oneEvent(component, 'mouseleave');
241+
242+
expect(evt).to.equal(customEvent);
243+
expect(evt.detail).to.equal(detail);
244+
});
245+
});
246+
});

0 commit comments

Comments
 (0)