|
| 1 | +import cases from 'jest-in-case' |
| 2 | +import {isDisabled} from '#src/utils' |
| 3 | +import {render} from '#testHelpers' |
| 4 | + |
| 5 | +customElements.define( |
| 6 | + 'form-associated', |
| 7 | + class FormAssociated extends HTMLElement { |
| 8 | + static formAssociated = true |
| 9 | + get disabled() { |
| 10 | + return this.hasAttribute('disabled') |
| 11 | + } |
| 12 | + }, |
| 13 | +) |
| 14 | + |
| 15 | +customElements.define( |
| 16 | + 'custom-el', |
| 17 | + class CustomEl extends HTMLElement { |
| 18 | + get disabled() { |
| 19 | + return this.hasAttribute('disabled') |
| 20 | + } |
| 21 | + }, |
| 22 | +) |
| 23 | + |
| 24 | +// https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements |
| 25 | +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#enabling-and-disabling-form-controls:-the-disabled-attribute |
| 26 | +cases( |
| 27 | + 'check if element is disabled', |
| 28 | + ({html, node = '//input', expected = true}) => { |
| 29 | + const {xpathNode} = render(html) |
| 30 | + expect(isDisabled(xpathNode<Element>(node))).toBe(expected) |
| 31 | + }, |
| 32 | + { |
| 33 | + control: { |
| 34 | + html: `<input/>`, |
| 35 | + expected: false, |
| 36 | + }, |
| 37 | + 'disabled control': { |
| 38 | + html: `<input disabled/>`, |
| 39 | + }, |
| 40 | + 'control in disabled fieldset': { |
| 41 | + html: `<fieldset disabled><input/></fieldset>`, |
| 42 | + }, |
| 43 | + 'control in first legend of disabled fieldset': { |
| 44 | + html: `<fieldset disabled><legend><input/></legend></fieldset>`, |
| 45 | + expected: false, |
| 46 | + }, |
| 47 | + 'control in other legend of disabled fieldset': { |
| 48 | + html: `<fieldset disabled><legend></legend><legend><input/></legend></fieldset>`, |
| 49 | + }, |
| 50 | + 'control in nested legend of disabled fieldset': { |
| 51 | + html: `<fieldset disabled><div>><legend><input/></legend></div></fieldset>`, |
| 52 | + }, |
| 53 | + 'element without support for disabled': { |
| 54 | + html: `<div disabled></div>`, |
| 55 | + node: '*', |
| 56 | + expected: false, |
| 57 | + }, |
| 58 | + 'form-associated disabled custom element': { |
| 59 | + html: `<form-associated disabled></form-associated>`, |
| 60 | + node: '*', |
| 61 | + }, |
| 62 | + 'form-associated enabled custom element': { |
| 63 | + html: `<form-associated></form-associated>`, |
| 64 | + node: '*', |
| 65 | + expected: false, |
| 66 | + }, |
| 67 | + 'other custom element': { |
| 68 | + html: `<custom-el disabled></custom-el>`, |
| 69 | + node: '*', |
| 70 | + expected: false, |
| 71 | + }, |
| 72 | + }, |
| 73 | +) |
0 commit comments