-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6881 from Sage/FE-6607_loader-RTL
chore(loader): convert enzyme to RTL - FE-6607
- Loading branch information
Showing
6 changed files
with
168 additions
and
206 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,98 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import Loader from "."; | ||
|
||
jest.mock("../../hooks/useMediaQuery", () => { | ||
return jest.fn(() => true); | ||
}); | ||
|
||
it.each([0, 1, 2])("each loader square renders as expected", (index) => { | ||
render(<Loader />); | ||
|
||
const loaderSquares = screen.getAllByTestId("loader-square"); | ||
|
||
expect(loaderSquares[index]).toBeInTheDocument(); | ||
}); | ||
|
||
// These styling tests required for coverage | ||
test("when `size` prop is set to large, the expected width, height, and margin styles are applied", () => { | ||
render(<Loader size="large" />); | ||
|
||
const loaderSquares = screen.getAllByTestId("loader-square"); | ||
|
||
expect(loaderSquares[0]).toHaveStyle({ | ||
height: "20px", | ||
width: "20px", | ||
marginRight: "8px", | ||
}); | ||
|
||
expect(loaderSquares[1]).toHaveStyle({ | ||
height: "20px", | ||
width: "20px", | ||
marginRight: "8px", | ||
}); | ||
|
||
expect(loaderSquares[2]).toHaveStyle({ | ||
height: "20px", | ||
width: "20px", | ||
}); | ||
}); | ||
|
||
// These styling tests required for coverage | ||
test("when `size` prop is set to small, the expected width, height, and margin styles are applied", () => { | ||
render(<Loader size="small" />); | ||
|
||
const loaderSquares = screen.getAllByTestId("loader-square"); | ||
|
||
expect(loaderSquares[0]).toHaveStyle({ | ||
width: "12px", | ||
marginRight: "6px", | ||
}); | ||
|
||
expect(loaderSquares[1]).toHaveStyle({ | ||
width: "12px", | ||
marginRight: "6px", | ||
}); | ||
|
||
expect(loaderSquares[2]).toHaveStyle({ | ||
width: "12px", | ||
}); | ||
}); | ||
|
||
// These styling tests required for coverage | ||
test("when inside button, the expected white background colour is applied", () => { | ||
render(<Loader isInsideButton />); | ||
|
||
const loaderSquares = screen.getAllByTestId("loader-square"); | ||
|
||
expect(loaderSquares[0]).toHaveStyleRule( | ||
"backgroundColor: var(--colorsUtilityYang100)" | ||
); | ||
|
||
expect(loaderSquares[1]).toHaveStyleRule( | ||
"backgroundColor: var(--colorsUtilityYang100)" | ||
); | ||
|
||
expect(loaderSquares[2]).toHaveStyleRule( | ||
"backgroundColor: var(--colorsUtilityYang100)" | ||
); | ||
}); | ||
|
||
// These styling tests required for coverage | ||
test("when inside button and `isActive` prop is false, the expected background colour is applied", () => { | ||
render(<Loader isInsideButton isActive={false} />); | ||
|
||
const loaderSquares = screen.getAllByTestId("loader-square"); | ||
|
||
expect(loaderSquares[0]).toHaveStyleRule( | ||
"backgroundColor: var(--colorsSemanticNeutral500)" | ||
); | ||
|
||
expect(loaderSquares[1]).toHaveStyleRule( | ||
"backgroundColor: var(--colorsSemanticNeutral500)" | ||
); | ||
|
||
expect(loaderSquares[2]).toHaveStyleRule( | ||
"backgroundColor: var(--colorsSemanticNeutral500)" | ||
); | ||
}); |
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 was deleted.
Oops, something went wrong.
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,49 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { testStyledSystemMargin } from "../../__spec_helper__/__internal__/test-utils"; | ||
import Loader from "."; | ||
import useMediaQuery from "../../hooks/useMediaQuery"; | ||
|
||
jest.mock("../../hooks/useMediaQuery", () => ({ | ||
__esModule: true, | ||
default: jest.fn().mockReturnValue(false), | ||
})); | ||
|
||
testStyledSystemMargin((props) => <Loader {...props} />); | ||
|
||
test("when the user disallows animations or their preference cannot be determined, alternative loading text is rendered", () => { | ||
render(<Loader />); | ||
|
||
expect(screen.getByText("Loading")).toBeInTheDocument(); | ||
}); | ||
|
||
describe("when the user allows animations", () => { | ||
beforeEach(() => { | ||
const mockUseMediaQuery = useMediaQuery as jest.MockedFunction< | ||
typeof useMediaQuery | ||
>; | ||
mockUseMediaQuery.mockReturnValueOnce(true); | ||
}); | ||
|
||
test("renders three square animation", () => { | ||
render(<Loader />); | ||
|
||
const squares = screen.getAllByTestId("loader-square"); | ||
|
||
expect(squares).toHaveLength(3); | ||
}); | ||
|
||
test("root element has accessible name", () => { | ||
render(<Loader />); | ||
|
||
expect(screen.getByRole("progressbar")).toHaveAccessibleName("Loading"); | ||
}); | ||
|
||
test("when custom `aria-label` is passed, set accessible name to its value", () => { | ||
render(<Loader aria-label="Still loading" />); | ||
|
||
expect(screen.getByRole("progressbar")).toHaveAccessibleName( | ||
"Still loading" | ||
); | ||
}); | ||
}); |