|
| 1 | +import { fixture, oneEvent, expect } from '@open-wc/testing'; |
| 2 | + |
| 3 | +/** |
| 4 | + * <hx-disclosure> component tests |
| 5 | + * |
| 6 | + * @type HXDisclosureElement |
| 7 | + * |
| 8 | + */ |
| 9 | +describe('<hx-disclosure> component tests', () => { |
| 10 | + const template = '<hx-disclosure>'; |
| 11 | + |
| 12 | + describe('instantiate element', () => { |
| 13 | + it('should be instantiated with hx-defined attribute', async () => { |
| 14 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 15 | + const attr = component.hasAttribute('hx-defined'); |
| 16 | + |
| 17 | + expect(attr).to.be.true; |
| 18 | + }); |
| 19 | + |
| 20 | + it('should not be hidden', async () => { |
| 21 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 22 | + const prop = component.hidden; |
| 23 | + |
| 24 | + expect(prop).to.be.false; |
| 25 | + }); |
| 26 | + |
| 27 | + it(`the rendered light DOM should NOT equal simple template ${template}`, async () => { |
| 28 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 29 | + |
| 30 | + expect(component).lightDom.to.not.equal(template); |
| 31 | + }); |
| 32 | + |
| 33 | + it(`should NOT have a Shadow DOM`, async () => { |
| 34 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 35 | + const shadow = component.shadowRoot; |
| 36 | + |
| 37 | + expect(shadow).to.be.null; |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('test $onConnect method', () => { |
| 42 | + it('should have aria-expanded attribute', async () => { |
| 43 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 44 | + const attr = component.hasAttribute('aria-expanded'); |
| 45 | + |
| 46 | + expect(attr).to.be.true; |
| 47 | + }); |
| 48 | + |
| 49 | + it('should have aria-expanded attribute default to false', async () => { |
| 50 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 51 | + const attr = component.getAttribute('aria-expanded'); |
| 52 | + |
| 53 | + expect(attr).to.be.equal(String(false)); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should default to null target', async () => { |
| 57 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 58 | + const targetProp = component.target; |
| 59 | + |
| 60 | + expect(targetProp).to.be.null; |
| 61 | + }); |
| 62 | + |
| 63 | + it('should have role attribute default to button', async () => { |
| 64 | + const roleAttr = 'button'; |
| 65 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 66 | + const attr = component.hasAttribute('role'); |
| 67 | + const role = component.getAttribute('role'); |
| 68 | + |
| 69 | + expect(attr).to.be.true; |
| 70 | + expect(role).to.equal(roleAttr); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should have tabindex attribute', async () => { |
| 74 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 75 | + const attr = component.hasAttribute('tabindex'); |
| 76 | + |
| 77 | + expect(attr).to.be.true; |
| 78 | + }); |
| 79 | + |
| 80 | + it('should have tabindex attribute default to 0', async () => { |
| 81 | + const tabDefault = "0"; |
| 82 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 83 | + const attr = component.hasAttribute('tabindex'); |
| 84 | + const attrValue = component.getAttribute('tabindex'); |
| 85 | + |
| 86 | + expect(attr).to.be.true; |
| 87 | + expect(attrValue).to.be.equal(tabDefault); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('test <hx-disclosure> getter and setter methods', () => { |
| 92 | + it('should have a default null target', async () => { |
| 93 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 94 | + const target = component.target; |
| 95 | + |
| 96 | + expect(target).to.be.null; |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('test adding event listeners', () => { |
| 101 | + describe('test click event listeners', () => { |
| 102 | + it('should fire a click event', async () => { |
| 103 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 104 | + const detail = { evt: 'clicked!'}; |
| 105 | + const customEvent = new CustomEvent('click', { detail }); |
| 106 | + |
| 107 | + setTimeout(() => component.dispatchEvent(customEvent)); |
| 108 | + const evt = await oneEvent(component, 'click'); |
| 109 | + |
| 110 | + expect(evt).to.equal(customEvent); |
| 111 | + expect(evt.detail).to.equal(detail); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should add a keydown event listener', async () => { |
| 115 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 116 | + const detail = { evt: 'key down!'}; |
| 117 | + const customEvent = new CustomEvent('keydown', { detail }); |
| 118 | + |
| 119 | + setTimeout(() => component.dispatchEvent(customEvent)); |
| 120 | + const evt = await oneEvent(component, 'keydown'); |
| 121 | + |
| 122 | + expect(evt).to.equal(customEvent); |
| 123 | + expect(evt.detail).to.equal(detail); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should add a keyup event listener', async () => { |
| 127 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 128 | + const detail = { evt: 'key up!'}; |
| 129 | + const customEvent = new CustomEvent('keyup', { detail }); |
| 130 | + |
| 131 | + setTimeout(() => component.dispatchEvent(customEvent)); |
| 132 | + const evt = await oneEvent(component, 'keyup'); |
| 133 | + |
| 134 | + expect(evt).to.equal(customEvent); |
| 135 | + expect(evt.detail).to.equal(detail); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('test target event listeners', () => { |
| 140 | + it('should be able to open target', async () => { |
| 141 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 142 | + const detail = { evt: 'open target!'}; |
| 143 | + const customEvent = new CustomEvent('open', { detail }); |
| 144 | + |
| 145 | + setTimeout(() => component.dispatchEvent(customEvent)); |
| 146 | + const evt = await oneEvent(component, 'open'); |
| 147 | + |
| 148 | + expect(evt).to.equal(customEvent); |
| 149 | + expect(evt.detail).to.equal(detail); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should be able to close target', async () => { |
| 153 | + const component = /** @type {HXDisclosureElement} */ await fixture(template); |
| 154 | + const detail = { evt: 'close target!'}; |
| 155 | + const customEvent = new CustomEvent('close', { detail }); |
| 156 | + |
| 157 | + setTimeout(() => component.dispatchEvent(customEvent)); |
| 158 | + const evt = await oneEvent(component, 'close'); |
| 159 | + |
| 160 | + expect(evt).to.equal(customEvent); |
| 161 | + expect(evt.detail).to.equal(detail); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments