|
| 1 | +import { expect, fixture, html } from '@open-wc/testing'; |
| 2 | + |
| 3 | +import { LitElement } from '../index.js'; |
| 4 | +import { DisabledWithTabIndexMixin } from '../src/DisabledWithTabIndexMixin.js'; |
| 5 | + |
| 6 | +describe('DisabledWithTabIndexMixin', () => { |
| 7 | + before(() => { |
| 8 | + class WithTabIndex extends DisabledWithTabIndexMixin(LitElement) {} |
| 9 | + customElements.define('can-be-disabled-with-tab-index', WithTabIndex); |
| 10 | + }); |
| 11 | + |
| 12 | + it('has an initial tabIndex of 0', async () => { |
| 13 | + const el = await fixture(html` |
| 14 | + <can-be-disabled-with-tab-index></can-be-disabled-with-tab-index> |
| 15 | + `); |
| 16 | + expect(el.tabIndex).to.equal(0); |
| 17 | + expect(el.getAttribute('tabindex')).to.equal('0'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('sets tabIndex to -1 if disabled', async () => { |
| 21 | + const el = await fixture(html` |
| 22 | + <can-be-disabled-with-tab-index></can-be-disabled-with-tab-index> |
| 23 | + `); |
| 24 | + el.disabled = true; |
| 25 | + expect(el.tabIndex).to.equal(-1); |
| 26 | + await el.updateComplete; |
| 27 | + expect(el.getAttribute('tabindex')).to.equal('-1'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('disabled does not override user provided tabindex', async () => { |
| 31 | + const el = await fixture(html` |
| 32 | + <can-be-disabled-with-tab-index tabindex="5" disabled></can-be-disabled-with-tab-index> |
| 33 | + `); |
| 34 | + expect(el.getAttribute('tabindex')).to.equal('-1'); |
| 35 | + el.disabled = false; |
| 36 | + await el.updateComplete; |
| 37 | + expect(el.getAttribute('tabindex')).to.equal('5'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('can be disabled imperatively', async () => { |
| 41 | + const el = await fixture(html` |
| 42 | + <can-be-disabled-with-tab-index disabled></can-be-disabled-with-tab-index> |
| 43 | + `); |
| 44 | + expect(el.getAttribute('tabindex')).to.equal('-1'); |
| 45 | + |
| 46 | + el.disabled = false; |
| 47 | + await el.updateComplete; |
| 48 | + expect(el.getAttribute('tabindex')).to.equal('0'); |
| 49 | + expect(el.hasAttribute('disabled')).to.equal(false); |
| 50 | + |
| 51 | + el.disabled = true; |
| 52 | + await el.updateComplete; |
| 53 | + expect(el.getAttribute('tabindex')).to.equal('-1'); |
| 54 | + expect(el.hasAttribute('disabled')).to.equal(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('will not allow to change tabIndex after makeRequestToBeDisabled()', async () => { |
| 58 | + const el = await fixture(html` |
| 59 | + <can-be-disabled-with-tab-index></can-be-disabled-with-tab-index> |
| 60 | + `); |
| 61 | + el.makeRequestToBeDisabled(); |
| 62 | + |
| 63 | + el.tabIndex = 5; |
| 64 | + expect(el.tabIndex).to.equal(-1); |
| 65 | + await el.updateComplete; |
| 66 | + expect(el.getAttribute('tabindex')).to.equal('-1'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('will restore last tabIndex after retractRequestToBeDisabled()', async () => { |
| 70 | + const el = await fixture(html` |
| 71 | + <can-be-disabled-with-tab-index tabindex="5"></can-be-disabled-with-tab-index> |
| 72 | + `); |
| 73 | + el.makeRequestToBeDisabled(); |
| 74 | + expect(el.tabIndex).to.equal(-1); |
| 75 | + await el.updateComplete; |
| 76 | + expect(el.getAttribute('tabindex')).to.equal('-1'); |
| 77 | + el.retractRequestToBeDisabled(); |
| 78 | + expect(el.tabIndex).to.equal(5); |
| 79 | + await el.updateComplete; |
| 80 | + expect(el.getAttribute('tabindex')).to.equal('5'); |
| 81 | + |
| 82 | + el.makeRequestToBeDisabled(); |
| 83 | + el.tabIndex = 12; |
| 84 | + el.retractRequestToBeDisabled(); |
| 85 | + expect(el.tabIndex).to.equal(12); |
| 86 | + await el.updateComplete; |
| 87 | + expect(el.getAttribute('tabindex')).to.equal('12'); |
| 88 | + |
| 89 | + el.makeRequestToBeDisabled(); |
| 90 | + el.tabIndex = 13; |
| 91 | + el.tabIndex = 14; |
| 92 | + el.retractRequestToBeDisabled(); |
| 93 | + expect(el.tabIndex).to.equal(14); |
| 94 | + await el.updateComplete; |
| 95 | + expect(el.getAttribute('tabindex')).to.equal('14'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('may allow multiple calls to retractRequestToBeDisabled', async () => { |
| 99 | + const el = await fixture(html` |
| 100 | + <can-be-disabled-with-tab-index disabled></can-be-disabled-with-tab-index> |
| 101 | + `); |
| 102 | + el.retractRequestToBeDisabled(); |
| 103 | + el.retractRequestToBeDisabled(); |
| 104 | + expect(el.disabled).to.be.true; |
| 105 | + expect(el.tabIndex).to.be.equal(-1); |
| 106 | + }); |
| 107 | +}); |
0 commit comments