Skip to content

Commit 637b80d

Browse files
authored
Merge pull request #761 from lalithkarikelli/surf2076
test(hx-switch-control): implement component tests
2 parents 6ac257b + fa3e67e commit 637b80d

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { fixture, oneEvent, expect } from '@open-wc/testing';
2+
3+
/**
4+
* <hx-switch-control> component tests
5+
*
6+
* @type HXSwitchControlElement
7+
*
8+
*/
9+
describe('<hx-switch-control> component tests', () => {
10+
const template = '<hx-switch-control>';
11+
const mockup = `<hx-switch-control>
12+
<input type="checkbox" id="switchOnOffTest" />
13+
<label for="switchOnOffTest">
14+
<hx-switch
15+
onlabel="on"
16+
offlabel="off"
17+
aria-labelledby="switchOnOffTest">
18+
</hx-switch>
19+
</label>
20+
</hx-switch-control>`;
21+
22+
describe('test instantiate element', () => {
23+
it('should be instantiated with hx-defined attribute', async () => {
24+
const fragment = /** @type {HXSwitchControlElement} */ await fixture(mockup);
25+
const attr = fragment.hasAttribute('hx-defined');
26+
27+
expect(attr).to.be.true;
28+
});
29+
30+
it('should not be hidden', async () => {
31+
const fragment = /** @type {HXSwitchControlElement} */ await fixture(mockup);
32+
const prop = fragment.hidden;
33+
34+
expect(prop).to.be.false;
35+
});
36+
37+
it(`the rendered Light DOM should NOT equal simple template ${template}`, async () => {
38+
const fragment = /** @type {HXSwitchControlElement} */ await fixture(mockup);
39+
40+
expect(fragment).lightDom.to.not.equal(mockup);
41+
});
42+
43+
it(`should NOT have a Shadow DOM`, async () => {
44+
const fragment = /** @type {HXSwitchControlElement} */ await fixture(mockup);
45+
const shadow = fragment.shadowRoot;
46+
47+
expect(shadow).to.be.null;
48+
});
49+
50+
});
51+
describe('test for click event listener', () => {
52+
it('should fire a click event', async () => {
53+
const fragment = /** @type { HXSwitchControlElement } */ await fixture(mockup);
54+
const detail = { evt: 'clicked!'};
55+
const customEvent = new CustomEvent('click', { detail });
56+
57+
setTimeout(() => fragment.dispatchEvent(customEvent));
58+
const evt = await oneEvent(fragment, 'click');
59+
60+
expect(evt).to.equal(customEvent);
61+
expect(evt.detail).to.equal(detail);
62+
});
63+
});
64+
65+
describe('test <hx-switch-control> getters', () => {
66+
it('should get the control element"]', async () => {
67+
const fragment = /** @type { HXSwitchControlElement } */ await fixture(mockup);
68+
const id = 'switchOnOffTest';
69+
const ctrlElement = fragment.controlElement;
70+
const ctrlElementID = ctrlElement.id;
71+
72+
expect(ctrlElementID).to.equal(id);
73+
});
74+
75+
it('should get the <hx-switch> element', async () => {
76+
const template = '<hx-switch>';
77+
const fragment = /** @type { HXSwitchControlElement } */ await fixture(mockup);
78+
const switchFra = /** @type { HXSwitchElement } */ await fixture(template);
79+
const switchEl = fragment.switchElement;
80+
81+
expect(switchEl.tagName).to.equal(switchFra.tagName);
82+
});
83+
84+
it('should get the <hx-switch> toggled state', async () => {
85+
const fragment = /** @type { HXSwitchControlElement } */ await fixture(mockup);
86+
const switchElementState = fragment.switchElement.toggled;
87+
88+
expect(switchElementState).to.be.false;
89+
});
90+
});
91+
});

src/elements/hx-switch/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ describe('<hx-switch> component tests', () => {
4646

4747
expect(attr).to.be.true;
4848
});
49+
50+
it('should be able to add toggled attribute', async () => {
51+
const fragment = /** @type {HXToggleElement} */ await fixture(mockup);
52+
const attr = fragment.hasAttribute('toggled');
53+
fragment.toggled = true;
54+
const toggledAttr = fragment.hasAttribute('toggled');
55+
56+
expect(attr).to.be.false;
57+
expect(toggledAttr).to.be.true;
58+
});
4959
});
5060

5161
describe('test for Sync Labels', () => {

0 commit comments

Comments
 (0)