Last active
September 7, 2023 14:07
-
-
Save reyronald/dd40c20f5a38233ad77f10c6e825f562 to your computer and use it in GitHub Desktop.
toBeAriaDisabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { MatcherFunction } from "expect"; | |
function assertIsElement(el: unknown): asserts el is HTMLElement { | |
const isElement = el instanceof HTMLElement; | |
if (!isElement) { | |
throw new TypeError("Not an HTMLElement"); | |
} | |
} | |
export const toBeAriaDisabled: MatcherFunction<[element: unknown]> = function (element) { | |
assertIsElement(element); | |
const ariaDisabled = element.getAttribute("aria-disabled"); | |
const isDisabled = ariaDisabled === "true"; | |
return { | |
pass: isDisabled, | |
message: () => { | |
const elementPrint = this.utils.printReceived(element.cloneNode(false)); | |
if (this.isNot) { | |
return ( | |
`${this.utils.matcherHint(".not.toBeAriaDisabled", "element", "")}\n\n` + | |
`Received element is aria-disabled\n` + | |
`Expecting no aria-disabled attribute or aria-disabled="false"\n\n` + | |
`${elementPrint}` | |
); | |
} else { | |
return ( | |
`${this.utils.matcherHint(".toBeAriaDisabled", "element", "")}\n\n` + | |
`Received element is not aria-disabled\n` + | |
`Expecting attribute: aria-disabled="true"\n\n` + | |
`${elementPrint}` | |
); | |
} | |
}, | |
}; | |
}; | |
export const toBeAriaEnabled: MatcherFunction<[element: unknown]> = function (element) { | |
assertIsElement(element); | |
const ariaDisabled = element.getAttribute("aria-disabled"); | |
const isEnabled = ariaDisabled == null || ariaDisabled === "false"; | |
return { | |
pass: isEnabled, | |
message: () => { | |
const elementPrint = this.utils.printReceived(element.cloneNode(false)); | |
if (this.isNot) { | |
return ( | |
`${this.utils.matcherHint(".not.toBeAriaEnabled", "element", "")}\n\n` + | |
`Received element is not aria-disabled\n` + | |
`Expecting attribute: aria-disabled="true"\n\n` + | |
`${elementPrint}` | |
); | |
} else { | |
return ( | |
`${this.utils.matcherHint(".toBeAriaEnabled", "element", "")}\n\n` + | |
`Received element is aria-disabled\n` + | |
`Expecting no aria-disabled attribute or aria-disabled="false"\n\n` + | |
`${elementPrint}` | |
); | |
} | |
}, | |
}; | |
}; | |
declare global { | |
// Can't do it without namespace in our current version of Jest | |
// eslint-disable-next-line @typescript-eslint/no-namespace -- 👆 | |
namespace jest { | |
interface Matchers<R> { | |
toBeAriaDisabled(): R; | |
toBeAriaEnabled(): R; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as matchers from "./tests/toBeAriaDisabled"; | |
expect.extend(matchers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment