-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also added OTP demo page to doc site.
- Loading branch information
Showing
15 changed files
with
30,518 additions
and
20,161 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,56 @@ | ||
import React from "react"; | ||
import { render, screen, waitFor, fireEvent } from "../utils/test"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
import Form from "./Form"; | ||
import OtpInput from "./OtpInput"; | ||
|
||
function FormWithOtpInput(props) { | ||
const initialValues = { | ||
otp: "", | ||
}; | ||
|
||
return ( | ||
<Form initialValues={initialValues}> | ||
<OtpInput name="otp" {...props} /> | ||
</Form> | ||
); | ||
} | ||
|
||
describe("OtpInput", () => { | ||
it("renders correctly", () => { | ||
render(<FormWithOtpInput label="Enter OTP" testId="otp" />); | ||
expect(screen.getByTestId("otp")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders default number of inputs", () => { | ||
render(<FormWithOtpInput label="Enter OTP" />); | ||
const inputs = screen.getAllByRole("textbox"); | ||
expect(inputs.length).toBe(6); | ||
}); | ||
|
||
it("becomes disabled when the disabled prop is true", () => { | ||
render(<FormWithOtpInput label="Enter OTP" disabled />); | ||
const inputs = screen.getAllByRole("textbox"); | ||
inputs.forEach((input) => { | ||
expect(input).toBeDisabled(); | ||
}); | ||
}); | ||
|
||
it("calls onChange when value changes", () => { | ||
const onChange = jest.fn(); | ||
render(<FormWithOtpInput label="Enter OTP" onChange={onChange} />); | ||
const firstInput = screen.getAllByRole("textbox")[0]; | ||
fireEvent.change(firstInput, { target: { value: "1" } }); | ||
expect(onChange).toHaveBeenCalledWith("1"); | ||
}); | ||
|
||
it("displays an error message for invalid input", async () => { | ||
render(<FormWithOtpInput label="Enter OTP" />); | ||
const firstInput = screen.getAllByRole("textbox")[0]; | ||
fireEvent.change(firstInput, { target: { value: "1" } }); | ||
fireEvent.blur(firstInput); | ||
await waitFor(() => { | ||
expect(screen.getByText("Must be 6 digits")).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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,48 @@ | ||
export default (theme, { getColor }) => { | ||
return { | ||
getCSS: ({ targetElement, color }) => { | ||
switch (targetElement) { | ||
case "inputContainer": { | ||
return { | ||
position: "relative", | ||
fontSize: theme.fontSizes[1], | ||
fontWeight: theme.fontWeights.light, | ||
lineHeight: theme.lineHeights[2], | ||
fontFamily: theme.fonts.body, | ||
color: theme.colors.black, | ||
}; | ||
} | ||
|
||
case "input": { | ||
const focusStyle = { | ||
outline: 0, | ||
borderRadius: theme.radii[0], | ||
boxShadow: theme.shadows.focus, | ||
}; | ||
|
||
return { | ||
textAlign: "center", | ||
boxSizing: "border-box", | ||
width: "100%", | ||
height: "48px", | ||
border: 0, | ||
margin: 0, | ||
paddingTop: 0, | ||
paddingBottom: 0, | ||
fontSize: "inherit", | ||
fontWeight: "inherit", | ||
lineHeight: "inherit", | ||
fontFamily: "inherit", | ||
color: "inherit", | ||
backgroundColor: getColor(color), | ||
":focus": focusStyle, | ||
}; | ||
} | ||
|
||
default: { | ||
return null; | ||
} | ||
} | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.