-
Notifications
You must be signed in to change notification settings - Fork 1
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 #9 from keyurparalkar/test/test-unit-tests
Test/test unit tests
- Loading branch information
Showing
6 changed files
with
3,124 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: check-unit-tests | ||
on: push | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install modules | ||
run: yarn | ||
- name: Run tests | ||
run: yarn test |
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,5 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "jsdom", | ||
}; |
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 "@testing-library/jest-dom"; | ||
import { render, renderHook, screen, waitFor } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import usePasscode from "./usePasscode"; | ||
|
||
const TestComponent = (props: { isAlphaNumeric: boolean }) => { | ||
const { array, getEventHandlers, refs } = usePasscode({ | ||
count: 4, | ||
isAlphaNumeric: props.isAlphaNumeric, | ||
}); | ||
|
||
return ( | ||
<> | ||
{array.map((value: string | number, index: number) => ( | ||
<input | ||
ref={(el) => el && (refs.current[index] = el)} | ||
type="text" | ||
inputMode="numeric" | ||
autoComplete="one-time-code" | ||
maxLength={1} | ||
pattern="\d{1}" | ||
value={String(value)} | ||
key={`index-${index}`} | ||
data-testid={`index-${index}`} | ||
{...getEventHandlers(index)} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
describe("test basic workflow", () => { | ||
it("1. test whether passing no. of inputs creates an array of equal number ", () => { | ||
const { result } = renderHook(() => usePasscode({ count: 4 })); | ||
expect(result.current.array).toHaveLength(4); | ||
}); | ||
|
||
it("2. test if the focus changes to next element when typed", async () => { | ||
render(<TestComponent isAlphaNumeric={false} />); | ||
// focus on the first input | ||
const firstInput: HTMLInputElement = screen.getByTestId("index-0"); | ||
firstInput.focus(); | ||
expect(firstInput).toHaveFocus(); | ||
|
||
//Type in first input and check the focus of next input | ||
userEvent.type(firstInput, "1"); | ||
const secondtInput: HTMLInputElement = screen.getByTestId("index-1"); | ||
await waitFor(() => { | ||
expect(secondtInput).toHaveFocus(); | ||
}); | ||
}); | ||
|
||
it("3. test if the focus changes to previous element when backspaced", async () => { | ||
render(<TestComponent isAlphaNumeric={false} />); | ||
// focus on the first input | ||
const firstInput: HTMLInputElement = screen.getByTestId("index-0"); | ||
firstInput.focus(); | ||
|
||
//Type in first input and check the focus of next input | ||
userEvent.type(firstInput, "1"); | ||
const secondtInput: HTMLInputElement = screen.getByTestId("index-1"); | ||
await waitFor(() => { | ||
expect(secondtInput).toHaveFocus(); | ||
}); | ||
|
||
//Backspace and observe focus shift | ||
userEvent.keyboard("{Backspace}"); | ||
await waitFor(() => { | ||
expect(firstInput).toHaveFocus(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.