|
| 1 | +import { fixture, oneEvent, expect } from '@open-wc/testing'; |
| 2 | + |
| 3 | +/** |
| 4 | + * <hx-modal> component tests |
| 5 | + * |
| 6 | + * @type HXModalElement |
| 7 | + * |
| 8 | + */ |
| 9 | +describe('<hx-modal> component tests', () => { |
| 10 | + const template = '<hx-modal>'; |
| 11 | + |
| 12 | + describe('test instantiate element', () => { |
| 13 | + it('should be instantiated with hx-defined attribute', async () => { |
| 14 | + const component = /** @type {HXModalElement} */ 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 {HXModalElement} */ 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 {HXModalElement} */ await fixture(template); |
| 29 | + |
| 30 | + expect(component).lightDom.to.not.equal(template); |
| 31 | + }); |
| 32 | + |
| 33 | + }); |
| 34 | + |
| 35 | + describe('test Shadow DOM', () => { |
| 36 | + describe('verify render', () => { |
| 37 | + it('should have a static Shadow DOM', async function () { |
| 38 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 39 | + const shadow = component.shadowRoot.innerHTML; |
| 40 | + |
| 41 | + expect(component).shadowDom.to.equal(shadow); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should render the Shadow Root mode open', async () => { |
| 45 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 46 | + const mode = component.shadowRoot.mode; |
| 47 | + |
| 48 | + expect(mode).to.equal('open'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should have a single <slot>', async () => { |
| 52 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 53 | + const shadow = component.shadowRoot; |
| 54 | + const query = shadow.querySelectorAll('slot'); |
| 55 | + const len = query.length; |
| 56 | + |
| 57 | + expect(len).to.be.equal(1); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should have an unnamed <slot>', async () => { |
| 61 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 62 | + const name = component.slot; |
| 63 | + |
| 64 | + if ( name !== null ) { |
| 65 | + expect(name).to.be.equal(''); |
| 66 | + } else { |
| 67 | + expect(name).to.be.null; |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('verify Shadow DOM markup', () => { |
| 73 | + |
| 74 | + it('markup should contain a #hxBackdrop <div>', async () => { |
| 75 | + const elSelector = 'div#hxBackdrop'; |
| 76 | + const id = 'hxBackdrop'; |
| 77 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 78 | + const shadow = component.shadowRoot; |
| 79 | + const query = shadow.querySelector(elSelector); |
| 80 | + const queryId = query.id; |
| 81 | + |
| 82 | + expect(queryId).to.equal(id); |
| 83 | + }); |
| 84 | + |
| 85 | + it('markup should contain a #hxModal <div>', async () => { |
| 86 | + const elSelector = 'div#hxModal'; |
| 87 | + const id = 'hxModal'; |
| 88 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 89 | + const shadow = component.shadowRoot; |
| 90 | + const query = shadow.querySelector(elSelector); |
| 91 | + const queryId = query.id; |
| 92 | + |
| 93 | + expect(queryId).to.equal(id); |
| 94 | + }); |
| 95 | + |
| 96 | + it('markup should contain a #hxClose button', async () => { |
| 97 | + const elSelector = 'button#hxClose'; |
| 98 | + const id = 'hxClose'; |
| 99 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 100 | + const shadow = component.shadowRoot; |
| 101 | + const query = shadow.querySelector(elSelector); |
| 102 | + const queryId = query.id; |
| 103 | + |
| 104 | + expect(queryId).to.equal(id); |
| 105 | + }); |
| 106 | + |
| 107 | + it('markup should contain a #hxClose button with a times type <hx-icon>', async () => { |
| 108 | + const elSelector = 'button#hxClose > hx-icon'; |
| 109 | + const iconType = 'times'; |
| 110 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 111 | + const shadow = component.shadowRoot; |
| 112 | + const query = shadow.querySelector(elSelector); |
| 113 | + const queryType = query.type; |
| 114 | + |
| 115 | + expect(queryType).to.equal(iconType); |
| 116 | + }); |
| 117 | + |
| 118 | + it('markup should contain a #hxContent <div>', async () => { |
| 119 | + const elSelector = 'div#hxContent'; |
| 120 | + const id = 'hxContent'; |
| 121 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 122 | + const shadow = component.shadowRoot; |
| 123 | + const query = shadow.querySelector(elSelector); |
| 124 | + const queryId = query.id; |
| 125 | + |
| 126 | + expect(queryId).to.equal(id); |
| 127 | + }); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('test getters and setters', () => { |
| 132 | + it('should be able to open', async () => { |
| 133 | + const component = /** @type {HXModalElement} */ await fixture(template); |
| 134 | + component.open = true; |
| 135 | + const attr = component.hasAttribute('open'); |
| 136 | + |
| 137 | + expect(attr).to.be.true; |
| 138 | + }); |
| 139 | + it('should have aria-hidden attribute and set to true', async () => { |
| 140 | + const component = /** @type {HXModalElement} */ await fixture(template); |
| 141 | + const hasAttr = component.hasAttribute('aria-hidden'); |
| 142 | + const attr = component.getAttribute('aria-hidden'); |
| 143 | + |
| 144 | + expect(hasAttr).to.be.true; |
| 145 | + expect(attr).to.be.equal(String(true)); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('test for click event listener', () => { |
| 150 | + it('should fire a click event', async () => { |
| 151 | + const component = /** @type { HXModalElement } */ await fixture(template); |
| 152 | + const detail = { evt: 'clicked!'}; |
| 153 | + const customEvent = new CustomEvent('click', { detail }); |
| 154 | + |
| 155 | + setTimeout(() => component.dispatchEvent(customEvent)); |
| 156 | + const evt = await oneEvent(component, 'click'); |
| 157 | + |
| 158 | + expect(evt).to.equal(customEvent); |
| 159 | + expect(evt.detail).to.equal(detail); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments