|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { fixtureSync, nextRender } from '@vaadin/testing-helpers'; |
| 3 | +import { sendKeys } from '@web/test-runner-commands'; |
| 4 | +import sinon from 'sinon'; |
| 5 | +import '../vaadin-accordion-panel.js'; |
| 6 | + |
| 7 | +describe('vaadin-accordion-panel', () => { |
| 8 | + let panel; |
| 9 | + |
| 10 | + describe('opened', () => { |
| 11 | + let contentPart, contentNode; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + panel = fixtureSync(` |
| 15 | + <vaadin-accordion-panel> |
| 16 | + <div>Content</div> |
| 17 | + </vaadin-accordion-panel> |
| 18 | + `); |
| 19 | + await nextRender(); |
| 20 | + contentPart = panel.shadowRoot.querySelector('[part="content"]'); |
| 21 | + contentNode = panel.querySelector('div'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should set opened to false by default', () => { |
| 25 | + expect(panel.opened).to.be.false; |
| 26 | + }); |
| 27 | + |
| 28 | + it('should reflect opened property to attribute', () => { |
| 29 | + panel.opened = true; |
| 30 | + expect(panel.hasAttribute('opened')).to.be.true; |
| 31 | + }); |
| 32 | + |
| 33 | + it('should hide the content when opened is false', () => { |
| 34 | + expect(getComputedStyle(contentPart).display).to.equal('none'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should show the content when opened is true', () => { |
| 38 | + panel.opened = true; |
| 39 | + expect(getComputedStyle(contentPart).display).to.equal('block'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should set aria-hidden on the slotted element to true by default', () => { |
| 43 | + expect(contentNode.getAttribute('aria-hidden')).to.equal('true'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should set aria-hidden on the slotted element to false when opened', () => { |
| 47 | + panel.opened = true; |
| 48 | + expect(contentNode.getAttribute('aria-hidden')).to.equal('false'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + ['default', 'custom'].forEach((type) => { |
| 53 | + const fixtures = { |
| 54 | + default: ` |
| 55 | + <vaadin-accordion-panel summary="Summary"> |
| 56 | + <div>Content</div> |
| 57 | + </vaadin-accordion-panel> |
| 58 | + `, |
| 59 | + custom: ` |
| 60 | + <vaadin-accordion-panel> |
| 61 | + <vaadin-accordion-heading slot="summary">Summary</vaadin-accordion-heading> |
| 62 | + <div>Content</div> |
| 63 | + </vaadin-accordion-panel> |
| 64 | + `, |
| 65 | + }; |
| 66 | + |
| 67 | + let heading; |
| 68 | + |
| 69 | + describe(`${type} summary`, () => { |
| 70 | + let toggle; |
| 71 | + |
| 72 | + beforeEach(async () => { |
| 73 | + panel = fixtureSync(fixtures[type]); |
| 74 | + await nextRender(); |
| 75 | + heading = panel.querySelector('[slot="summary"]'); |
| 76 | + toggle = heading.shadowRoot.querySelector('button'); |
| 77 | + }); |
| 78 | + |
| 79 | + it(`should toggle opened on ${type} heading button click`, () => { |
| 80 | + toggle.click(); |
| 81 | + expect(panel.opened).to.be.true; |
| 82 | + |
| 83 | + toggle.click(); |
| 84 | + expect(panel.opened).to.be.false; |
| 85 | + }); |
| 86 | + |
| 87 | + it(`should toggle opened on ${type} heading button Enter`, async () => { |
| 88 | + toggle.focus(); |
| 89 | + |
| 90 | + await sendKeys({ press: 'Enter' }); |
| 91 | + expect(panel.opened).to.be.true; |
| 92 | + |
| 93 | + await sendKeys({ press: 'Enter' }); |
| 94 | + expect(panel.opened).to.be.false; |
| 95 | + }); |
| 96 | + |
| 97 | + it(`should toggle opened on ${type} heading button Space`, async () => { |
| 98 | + toggle.focus(); |
| 99 | + |
| 100 | + await sendKeys({ press: 'Space' }); |
| 101 | + expect(panel.opened).to.be.true; |
| 102 | + |
| 103 | + await sendKeys({ press: 'Space' }); |
| 104 | + expect(panel.opened).to.be.false; |
| 105 | + }); |
| 106 | + |
| 107 | + it(`should not update opened on ${type} heading button Arrow Down`, async () => { |
| 108 | + toggle.focus(); |
| 109 | + await sendKeys({ press: 'ArrowDown' }); |
| 110 | + expect(panel.opened).to.be.false; |
| 111 | + }); |
| 112 | + |
| 113 | + it(`should fire opened-changed event on ${type} heading button click`, () => { |
| 114 | + const spy = sinon.spy(); |
| 115 | + panel.addEventListener('opened-changed', spy); |
| 116 | + toggle.click(); |
| 117 | + expect(spy.calledOnce).to.be.true; |
| 118 | + }); |
| 119 | + |
| 120 | + it(`should update aria-expanded on ${type} heading button click`, () => { |
| 121 | + toggle.click(); |
| 122 | + expect(toggle.getAttribute('aria-expanded')).to.equal('true'); |
| 123 | + |
| 124 | + toggle.click(); |
| 125 | + expect(toggle.getAttribute('aria-expanded')).to.equal('false'); |
| 126 | + }); |
| 127 | + |
| 128 | + it(`should propagate disabled attribute to ${type} heading`, () => { |
| 129 | + panel.disabled = true; |
| 130 | + expect(heading.hasAttribute('disabled')).to.be.true; |
| 131 | + |
| 132 | + panel.disabled = false; |
| 133 | + expect(heading.hasAttribute('disabled')).to.be.false; |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments