Skip to content

Commit

Permalink
feat: improved types
Browse files Browse the repository at this point in the history
  • Loading branch information
jlp-craigmorten committed Jun 24, 2024
1 parent 1ed6a58 commit 5f3dbda
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ npm install -D @guidepup/jest @guidepup/virtual-screen-reader
yarn add -D @guidepup/jest @guidepup/virtual-screen-reader
```

If you are using TypeScript, make sure to setup Jest correctly by following the [Using TypeScript](https://jestjs.io/docs/getting-started#using-typescript) guide.

Add a [Jest setup file](https://jestjs.io/docs/configuration#setupfilesafterenv-array) (e.g. `setup-jest.js`) and add the following code to register the screen reader snapshot matchers:

```ts
Expand Down
38 changes: 38 additions & 0 deletions examples/typescript/src/mix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,44 @@ describe("matchers", () => {
test("toMatchScreenReaderSnapshot on the whole body", async () => {
await expect(document.body).toMatchScreenReaderSnapshot();
});

test("toMatchScreenReaderInlineSnapshot on the whole body", async () => {
await expect(document.body).toMatchScreenReaderInlineSnapshot(`
[
"document",
"navigation",
"Nav Text",
"end of navigation",
"region",
"heading, First Section Heading, level 1",
"paragraph",
"First Section Text",
"end of paragraph",
"article",
"banner",
"heading, Article Header Heading, level 1",
"paragraph",
"Article Header Text",
"end of paragraph",
"end of banner",
"paragraph",
"Article Text",
"end of paragraph",
"end of article",
"end of region",
"region",
"heading, Second Section Heading, level 1",
"paragraph",
"Second Section Text",
"end of paragraph",
"end of region",
"contentinfo",
"Footer",
"end of contentinfo",
"end of document",
]
`);
});
});

describe("virtual screen reader tests", () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guidepup/jest",
"version": "0.4.0",
"version": "0.4.1",
"description": "Virtual Screen Reader Jest Matchers",
"author": "Craig Morten <[email protected]>",
"license": "MIT",
Expand Down
59 changes: 55 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,48 @@ declare global {
namespace jest {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<R> {
/**
* Takes a snapshot of the Virtual Screen Reader spoken output from navigating
* completely through the provided container.
*/
toMatchScreenReaderSnapshot(): Promise<void>;

/**
* Takes an inline snapshot of the Virtual Screen Reader spoken output from navigating
* completely through the provided container.
*
* @param {string} [inlineSnapshot] The inline snapshot to compare against.
*/
toMatchScreenReaderInlineSnapshot(inlineSnapshot?: string): Promise<void>;
}
}
}

declare module "expect" {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<R> {
/**
* Takes a snapshot of the Virtual Screen Reader spoken output from navigating
* completely through the provided container.
*/
toMatchScreenReaderSnapshot(): Promise<void>;

/**
* Takes an inline snapshot of the Virtual Screen Reader spoken output from navigating
* completely through the provided container.
*
* @param {string} [inlineSnapshot] The inline snapshot to compare against.
*/
toMatchScreenReaderInlineSnapshot(inlineSnapshot?: string): Promise<void>;
}
}

if (expect?.extend !== undefined) {
expect.extend({ toMatchScreenReaderInlineSnapshot });
expect.extend({ toMatchScreenReaderSnapshot });
}

async function getScreenReaderOutput(container) {
async function getScreenReaderOutput(container: Node): Promise<string[]> {
const virtual = new Virtual();

let spokenPhraseLog: string[] = [];
Expand Down Expand Up @@ -48,15 +78,36 @@ async function getScreenReaderOutput(container) {
return spokenPhraseLog;
}

export async function toMatchScreenReaderInlineSnapshot(container, ...rest) {
/**
* Takes an inline snapshot of the Virtual Screen Reader spoken output from navigating
* completely through the provided container.
*
* @param {Node} container The bounding HTML element to take the Virtual Screen Reader snapshot in. To use the entire page pass `document.body`.
* @param {string} inlineSnapshot The inline snapshot to compare against.
*/
export async function toMatchScreenReaderInlineSnapshot(
container: Node,
...inlineSnapshot: [
propertiesOrSnapshot?: object | string,
inlineSnapshot?: string
]
): Promise<jest.CustomMatcherResult> {
this.error = new Error();

const snapshot = await getScreenReaderOutput(container);

return toMatchInlineSnapshot.call(this, snapshot, ...rest);
return toMatchInlineSnapshot.call(this, snapshot, ...inlineSnapshot);
}

export async function toMatchScreenReaderSnapshot(container) {
/**
* Takes a snapshot of the Virtual Screen Reader spoken output from navigating
* completely through the provided container.
*
* @param {Node} container The bounding HTML element to take the Virtual Screen Reader snapshot in. To use the entire page pass `document.body`.
*/
export async function toMatchScreenReaderSnapshot(
container: Node
): Promise<jest.CustomMatcherResult> {
this.error = new Error();

const snapshot = await getScreenReaderOutput(container);
Expand Down

0 comments on commit 5f3dbda

Please sign in to comment.