-
Notifications
You must be signed in to change notification settings - Fork 0
test(components): add tests for Input, TextField, TextArea, Search, EDSProvider #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91cd913
test(components): add tests for Input, TextField, TextArea, Search, E…
Chibuzor-Nwemambu 3fdd73c
fix(components): merge caller accessibilityState instead of overwriti…
Chibuzor-Nwemambu 3634e3d
test(components): add regression test for Input's accessibilityState …
Chibuzor-Nwemambu fd38b01
Revert "test(components): add regression test for Input's accessibili…
Chibuzor-Nwemambu 355b147
Revert "fix(components): merge caller accessibilityState instead of o…
Chibuzor-Nwemambu fdbd197
Merge branch 'main' into 214-batch-2-tests
Chibuzor-Nwemambu a70a3a5
test(components): add accessibilityState merge regression tests for I…
Chibuzor-Nwemambu 31ef2a3
Merge branch 'main' into 214-batch-2-tests
Chibuzor-Nwemambu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
packages/components/src/components/EDSProvider/EDSProvider.test.tsx
This file contains hidden or 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,87 @@ | ||
| import React from "react"; | ||
| import { render, screen } from "@testing-library/react-native"; | ||
| import { Text } from "react-native"; | ||
| import { useToken } from "../../hooks/useToken"; | ||
| import { | ||
| comfortableSpacingToken, | ||
| darkColorToken, | ||
| lightColorToken, | ||
| spaciousSpacingToken, | ||
| } from "../../styling/tokens"; | ||
| import { EDSProvider } from "./index"; | ||
|
|
||
| const TokenSpy = ({ onToken }: { onToken: (token: unknown) => void }) => { | ||
| const token = useToken(); | ||
| onToken(token); | ||
| return null; | ||
| }; | ||
|
|
||
| describe("EDSProvider", () => { | ||
| it("renders its children", () => { | ||
| render( | ||
| <EDSProvider colorScheme="light" density="comfortable"> | ||
| <Text>Hello world</Text> | ||
| </EDSProvider> | ||
| ); | ||
| expect(screen.getByText("Hello world")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("resolves the light color token for colorScheme=light", () => { | ||
| const onToken = jest.fn(); | ||
| render( | ||
| <EDSProvider colorScheme="light" density="comfortable"> | ||
| <TokenSpy onToken={onToken} /> | ||
| </EDSProvider> | ||
| ); | ||
| expect(onToken).toHaveBeenCalledWith( | ||
| expect.objectContaining({ colors: lightColorToken }) | ||
| ); | ||
| }); | ||
|
|
||
| it("resolves the dark color token for colorScheme=dark", () => { | ||
| const onToken = jest.fn(); | ||
| render( | ||
| <EDSProvider colorScheme="dark" density="comfortable"> | ||
| <TokenSpy onToken={onToken} /> | ||
| </EDSProvider> | ||
| ); | ||
| expect(onToken).toHaveBeenCalledWith( | ||
| expect.objectContaining({ colors: darkColorToken }) | ||
| ); | ||
| }); | ||
|
|
||
| it("resolves the comfortable spacing token for density=comfortable", () => { | ||
| const onToken = jest.fn(); | ||
| render( | ||
| <EDSProvider colorScheme="light" density="comfortable"> | ||
| <TokenSpy onToken={onToken} /> | ||
| </EDSProvider> | ||
| ); | ||
| expect(onToken).toHaveBeenCalledWith( | ||
| expect.objectContaining({ spacing: comfortableSpacingToken }) | ||
| ); | ||
| }); | ||
|
|
||
| it("resolves the spacious spacing token for density=spacious", () => { | ||
| const onToken = jest.fn(); | ||
| render( | ||
| <EDSProvider colorScheme="light" density="spacious"> | ||
| <TokenSpy onToken={onToken} /> | ||
| </EDSProvider> | ||
| ); | ||
| expect(onToken).toHaveBeenCalledWith( | ||
| expect.objectContaining({ spacing: spaciousSpacingToken }) | ||
| ); | ||
| }); | ||
|
|
||
| it("throws from useToken when used outside of an EDSProvider", () => { | ||
| // Expected: React logs the thrown error to the console during render. | ||
| const consoleError = jest | ||
| .spyOn(console, "error") | ||
| .mockImplementation(() => undefined); | ||
| expect(() => render(<TokenSpy onToken={jest.fn()} />)).toThrow( | ||
| "useToken must be called within a EDSProvider" | ||
| ); | ||
| consoleError.mockRestore(); | ||
| }); | ||
| }); |
This file contains hidden or 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 { Text } from "react-native"; | ||
| import { fireEvent, render, screen } from "test-utils"; | ||
| import { Input } from "./index"; | ||
|
|
||
| describe("Input", () => { | ||
| it("calls onChange with the new text", () => { | ||
| const onChange = jest.fn(); | ||
| render(<Input onChange={onChange} placeholder="Type here" />); | ||
| fireEvent.changeText(screen.getByPlaceholderText("Type here"), "hi"); | ||
| expect(onChange).toHaveBeenCalledWith("hi"); | ||
| }); | ||
|
|
||
| it("renders start and end text", () => { | ||
| render(<Input startText="https://" endText=".com" />); | ||
| expect(screen.getByText("https://")).toBeTruthy(); | ||
| expect(screen.getByText(".com")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("renders start and end adornments", () => { | ||
| render( | ||
| <Input | ||
| startAdornment={<Text>start-adornment</Text>} | ||
| endAdornment={<Text>end-adornment</Text>} | ||
| /> | ||
| ); | ||
| expect(screen.getByText("start-adornment")).toBeTruthy(); | ||
| expect(screen.getByText("end-adornment")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("shows an error icon when invalid", () => { | ||
| render(<Input invalid placeholder="Type here" />); | ||
| expect( | ||
| screen.UNSAFE_getByProps({ name: "alert-circle" }) | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("hides the error icon when hideErrorIcon is true", () => { | ||
| render(<Input invalid hideErrorIcon placeholder="Type here" />); | ||
| expect( | ||
| screen.UNSAFE_queryByProps({ name: "alert-circle" }) | ||
| ).toBeFalsy(); | ||
| }); | ||
|
|
||
| it("hides the error icon when disabled", () => { | ||
| render(<Input invalid disabled placeholder="Type here" />); | ||
| expect( | ||
| screen.UNSAFE_queryByProps({ name: "alert-circle" }) | ||
| ).toBeFalsy(); | ||
| }); | ||
|
|
||
| it("disables editing and exposes disabled to assistive technology", () => { | ||
| render(<Input disabled placeholder="Type here" />); | ||
| const input = screen.getByPlaceholderText("Type here"); | ||
| expect(input).toHaveProp("editable", false); | ||
| expect(input).toHaveProp("accessibilityState", { disabled: true }); | ||
| }); | ||
|
|
||
| it("disables editing but does not expose disabled when readOnly", () => { | ||
| // RNTL's toBeDisabled() treats any non-editable TextInput as disabled | ||
| // regardless of accessibilityState, so it can't be used to assert this | ||
| // distinction — check the actual prop the component controls instead. | ||
| render(<Input readOnly placeholder="Type here" />); | ||
| const input = screen.getByPlaceholderText("Type here"); | ||
| expect(input).toHaveProp("editable", false); | ||
| expect(input).toHaveProp("accessibilityState", { disabled: false }); | ||
| }); | ||
|
|
||
| it("merges a caller-supplied accessibilityState, with the component's own disabled value winning on conflict", () => { | ||
| render( | ||
| <Input | ||
| accessibilityState={{ selected: true, disabled: true }} | ||
| placeholder="Type here" | ||
| /> | ||
| ); | ||
| expect(screen.getByPlaceholderText("Type here")).toHaveProp( | ||
| "accessibilityState", | ||
| { selected: true, disabled: false } | ||
| ); | ||
| }); | ||
|
|
||
| it("calls the user-provided onFocus and onBlur handlers", () => { | ||
| const onFocus = jest.fn(); | ||
| const onBlur = jest.fn(); | ||
| render( | ||
| <Input | ||
| onFocus={onFocus} | ||
| onBlur={onBlur} | ||
| placeholder="Type here" | ||
| /> | ||
| ); | ||
| const input = screen.getByPlaceholderText("Type here"); | ||
| fireEvent(input, "focus"); | ||
| expect(onFocus).toHaveBeenCalledTimes(1); | ||
| fireEvent(input, "blur"); | ||
| expect(onBlur).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
117 changes: 117 additions & 0 deletions
117
packages/components/src/components/Search/Search.test.tsx
This file contains hidden or 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,117 @@ | ||
| import React from "react"; | ||
| import { fireEvent, render, screen } from "test-utils"; | ||
| import { Search } from "./index"; | ||
|
|
||
| describe("Search", () => { | ||
| it("renders label, description, and helperMessage", () => { | ||
| render( | ||
| <Search | ||
| label="Search products" | ||
| description="Search by name or SKU" | ||
| helperMessage="Press enter to search" | ||
| /> | ||
| ); | ||
| expect(screen.getByText("Search products")).toBeTruthy(); | ||
| expect(screen.getByText("Search by name or SKU")).toBeTruthy(); | ||
| expect(screen.getByText("Press enter to search")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("calls onChange when text changes", () => { | ||
| const onChange = jest.fn(); | ||
| render(<Search onChange={onChange} placeholder="Search" />); | ||
| fireEvent.changeText(screen.getByPlaceholderText("Search"), "shoes"); | ||
| expect(onChange).toHaveBeenCalledWith("shoes"); | ||
| }); | ||
|
|
||
| it("syncs the displayed text when the value prop changes (controlled usage)", () => { | ||
| const { rerender } = render( | ||
| <Search value="shoes" placeholder="Search" /> | ||
| ); | ||
| expect(screen.getByPlaceholderText("Search")).toHaveProp( | ||
| "value", | ||
| "shoes" | ||
| ); | ||
| rerender(<Search value="boots" placeholder="Search" />); | ||
| expect(screen.getByPlaceholderText("Search")).toHaveProp( | ||
| "value", | ||
| "boots" | ||
| ); | ||
| }); | ||
|
|
||
| it("does not show a clear button when there is no text", () => { | ||
| render(<Search placeholder="Search" />); | ||
| expect(screen.queryByLabelText("Clear search")).toBeFalsy(); | ||
| }); | ||
|
|
||
| it("shows a clear button once there is text, and clears it on press", () => { | ||
| const onChange = jest.fn(); | ||
| render( | ||
| <Search | ||
| defaultValue="shoes" | ||
| onChange={onChange} | ||
| placeholder="Search" | ||
| /> | ||
| ); | ||
| fireEvent.press(screen.getByLabelText("Clear search")); | ||
| expect(onChange).toHaveBeenCalledWith(""); | ||
| expect(screen.getByPlaceholderText("Search")).toHaveProp( | ||
| "value", | ||
| "" | ||
| ); | ||
| }); | ||
|
|
||
| it("does not show a clear button when disabled", () => { | ||
| render(<Search defaultValue="shoes" disabled placeholder="Search" />); | ||
| expect(screen.queryByLabelText("Clear search")).toBeFalsy(); | ||
| }); | ||
|
|
||
| it("does not show a clear button when readOnly", () => { | ||
| render(<Search defaultValue="shoes" readOnly placeholder="Search" />); | ||
| expect(screen.queryByLabelText("Clear search")).toBeFalsy(); | ||
| }); | ||
|
|
||
| it("does not render a Cancel button by default", () => { | ||
| render(<Search placeholder="Search" />); | ||
| expect(screen.queryByText("Cancel")).toBeFalsy(); | ||
| }); | ||
|
|
||
| it("renders a Cancel button when cancellable, clearing text and calling onCancelPress", () => { | ||
| // The Cancel button's wrapper sets pointerEvents to "none" until the | ||
| // input is focused (it's an absolutely-positioned overlay that slides | ||
| // in on focus), so it isn't press-able until the input is focused first. | ||
| const onChange = jest.fn(); | ||
| const onCancelPress = jest.fn(); | ||
| render( | ||
| <Search | ||
| cancellable | ||
| defaultValue="shoes" | ||
| onChange={onChange} | ||
| onCancelPress={onCancelPress} | ||
| placeholder="Search" | ||
| /> | ||
| ); | ||
| fireEvent(screen.getByPlaceholderText("Search"), "focus"); | ||
| fireEvent.press(screen.getByText("Cancel")); | ||
| expect(onChange).toHaveBeenCalledWith(""); | ||
| expect(onCancelPress).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("shows the error icon when invalid", () => { | ||
| render(<Search invalid placeholder="Search" />); | ||
| expect( | ||
| screen.UNSAFE_getByProps({ name: "alert-circle" }) | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("hides the error icon when disabled even if invalid", () => { | ||
| render(<Search invalid disabled placeholder="Search" />); | ||
| expect( | ||
| screen.UNSAFE_queryByProps({ name: "alert-circle" }) | ||
| ).toBeFalsy(); | ||
| }); | ||
|
|
||
| it("always shows the search icon", () => { | ||
| render(<Search placeholder="Search" />); | ||
| expect(screen.UNSAFE_getByProps({ name: "magnify" })).toBeTruthy(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.