generated from timelessco/react-components-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: added a11y test util * chore: test naming change * test: improved & extracted out mockImage
- Loading branch information
1 parent
811490f
commit fb2ad25
Showing
4 changed files
with
112 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from "react"; | ||
import { configureAxe } from "jest-axe"; | ||
import { RunOptions } from "axe-core"; | ||
import { render, RenderOptions, waitFor } from "@testing-library/react"; | ||
|
||
export const axe = configureAxe({ | ||
rules: { | ||
// disabled landmark rules when testing isolated components. | ||
region: { enabled: false }, | ||
}, | ||
}); | ||
|
||
type UI = Parameters<typeof render>[0] | Element; | ||
type TestA11YOptions = RenderOptions & { | ||
axeOptions?: RunOptions; | ||
}; | ||
|
||
export const testA11y = async ( | ||
ui: UI, | ||
{ axeOptions, ...options }: TestA11YOptions = {}, | ||
) => { | ||
jest.useRealTimers(); | ||
|
||
const container = React.isValidElement(ui) | ||
? render(ui, options).container | ||
: ui; | ||
|
||
const results = await axe(container, axeOptions); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}; | ||
|
||
const DELAY = 0; | ||
const LOAD_IMAGE = "load.png"; | ||
const ERROR_IMAGE = "error.png"; | ||
const orignalImage = window.Image; | ||
|
||
export const mockImage = (loadState: string) => { | ||
jest.useFakeTimers(); | ||
|
||
(window.Image as unknown) = class MockImage { | ||
onload: () => void = () => {}; | ||
onerror: () => void = () => {}; | ||
src: string = ""; | ||
constructor() { | ||
if (loadState === LOAD_IMAGE) { | ||
setTimeout(() => { | ||
this.onload(); | ||
}, DELAY); | ||
} | ||
if (loadState === ERROR_IMAGE) { | ||
setTimeout(() => { | ||
this.onerror(); | ||
}, DELAY); | ||
} | ||
return this; | ||
} | ||
}; | ||
}; | ||
|
||
mockImage.DELAY = DELAY; | ||
mockImage.LOAD_IMAGE = LOAD_IMAGE; | ||
mockImage.ERROR_IMAGE = ERROR_IMAGE; | ||
mockImage.ERROR_IMAGE = ERROR_IMAGE; | ||
mockImage.load = () => mockImage(LOAD_IMAGE); | ||
mockImage.error = () => mockImage(ERROR_IMAGE); | ||
mockImage.restoreMock = () => (window.Image = orignalImage); | ||
mockImage.advanceTimer = async () => { | ||
return await waitFor(() => { | ||
jest.advanceTimersByTime(mockImage.DELAY); | ||
}); | ||
}; |
fb2ad25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: