|
| 1 | +import { expect, fixture, html, waitUntil } from '@open-wc/testing'; |
| 2 | +import FloatingPanelElement from './index'; |
| 3 | +import Sinon from 'sinon'; |
| 4 | + |
| 5 | +describe('Floating panel', () => { |
| 6 | + it('starts and stops the positioning of the panel', async () => { |
| 7 | + const el = await fixture<FloatingPanelElement>(html` |
| 8 | + <twc-floating-panel> |
| 9 | + <button type="button" data-target="twc-floating-panel.trigger">Trigger</button> |
| 10 | + <div data-target="twc-floating-panel.panel"></div> |
| 11 | + </twc-floating-panel> |
| 12 | + `); |
| 13 | + |
| 14 | + const panel = el.querySelector('div')!; |
| 15 | + |
| 16 | + expect(el).not.to.have.attribute('active'); |
| 17 | + expect(el).not.to.have.attribute('data-current-placement'); |
| 18 | + |
| 19 | + el.active = true; |
| 20 | + expect(el).to.have.attribute('active'); |
| 21 | + await waitUntil(() => el.hasAttribute('data-current-placement')); |
| 22 | + expect(el).to.have.attribute('data-current-placement', 'bottom-start'); |
| 23 | + expect(panel).to.have.style('position', 'absolute'); |
| 24 | + |
| 25 | + el.active = false; |
| 26 | + expect(el).not.to.have.attribute('active'); |
| 27 | + expect(el).not.to.have.attribute('data-current-placement'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('setting the initial placement', async () => { |
| 31 | + const el = await fixture<FloatingPanelElement>(html` |
| 32 | + <twc-floating-panel placement="bottom-end"> |
| 33 | + <button type="button" data-target="twc-floating-panel.trigger">Trigger</button> |
| 34 | + <div data-target="twc-floating-panel.panel"></div> |
| 35 | + </twc-floating-panel> |
| 36 | + `); |
| 37 | + |
| 38 | + el.active = true; |
| 39 | + await waitUntil(() => el.hasAttribute('data-current-placement')); |
| 40 | + expect(el).to.have.attribute('data-current-placement', 'bottom-end'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('setting the positioning strategy', async () => { |
| 44 | + const el = await fixture<FloatingPanelElement>(html` |
| 45 | + <twc-floating-panel strategy="fixed"> |
| 46 | + <button type="button" data-target="twc-floating-panel.trigger">Trigger</button> |
| 47 | + <div data-target="twc-floating-panel.panel"></div> |
| 48 | + </twc-floating-panel> |
| 49 | + `); |
| 50 | + |
| 51 | + const panel = el.querySelector('div')!; |
| 52 | + |
| 53 | + el.active = true; |
| 54 | + await waitUntil(() => el.hasAttribute('data-current-placement')); |
| 55 | + expect(panel).to.have.style('position', 'fixed'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('emits the changed event after positioning the panel', async () => { |
| 59 | + const el = await fixture<FloatingPanelElement>(html` |
| 60 | + <twc-floating-panel> |
| 61 | + <button type="button" data-target="twc-floating-panel.trigger">Trigger</button> |
| 62 | + <div data-target="twc-floating-panel.panel"></div> |
| 63 | + </twc-floating-panel> |
| 64 | + `); |
| 65 | + |
| 66 | + const handler = Sinon.spy(); |
| 67 | + document.addEventListener(`${el.identifier}:changed`, handler); |
| 68 | + |
| 69 | + el.active = true; |
| 70 | + await waitUntil(() => el.hasAttribute('data-current-placement')); |
| 71 | + expect(handler.called).to.be.true; |
| 72 | + }); |
| 73 | +}); |
0 commit comments