-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add tests for onboarding modal input form
- Loading branch information
1 parent
371bb3c
commit 3e4e0bf
Showing
3 changed files
with
139 additions
and
15 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,123 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { TokenInputForm } from "./TokenInputForm"; | ||
import { fireEvent, getByRole, render, screen, waitFor } from "@testing-library/react"; | ||
import React from "react"; | ||
|
||
describe("TokenInputForm", () => { | ||
it("should initially have the button be disabled", () => { | ||
render( | ||
<TokenInputForm | ||
onTokenSubmit={() => {}} | ||
testToken={async () => { | ||
return true; | ||
}} | ||
/>, | ||
); | ||
|
||
const button = screen.getByRole("button"); | ||
expect(button).toHaveAttribute("disabled"); | ||
}); | ||
|
||
it("should initially not show an error", () => { | ||
render( | ||
<TokenInputForm | ||
onTokenSubmit={() => {}} | ||
testToken={async () => { | ||
return true; | ||
}} | ||
/>, | ||
); | ||
|
||
const textBox = screen.getByRole("textbox"); | ||
expect(textBox).not.toHaveAttribute("data-invalid"); | ||
}); | ||
|
||
it("should show an error if input is empty after focus and unfocus", () => { | ||
render( | ||
<TokenInputForm | ||
onTokenSubmit={() => {}} | ||
testToken={async () => { | ||
return true; | ||
}} | ||
/>, | ||
); | ||
|
||
const textBox = screen.getByRole("textbox"); | ||
fireEvent.focus(textBox); | ||
fireEvent.blur(textBox); | ||
|
||
expect(textBox).toHaveAttribute("data-invalid", "true"); | ||
}); | ||
|
||
it("should shown an error if API test rejects token", async () => { | ||
const [getArgs, testToken] = makeFakeTokenTest(false); | ||
render(<TokenInputForm onTokenSubmit={() => {}} testToken={testToken} />); | ||
|
||
const textBox = screen.getByRole("textbox"); | ||
fireEvent.focus(textBox); | ||
fireEvent.change(textBox, { target: { value: "abcdef" } }); | ||
fireEvent.blur(textBox); | ||
|
||
await waitFor(() => { | ||
expect(textBox).toHaveAttribute("data-invalid", "true"); | ||
}); | ||
expect(getArgs()).toBe("abcdef"); | ||
}); | ||
|
||
it("should enable button if API test accepts token", async () => { | ||
const [getArgs, testToken] = makeFakeTokenTest(true); | ||
render(<TokenInputForm onTokenSubmit={() => {}} testToken={testToken} />); | ||
|
||
const textBox = screen.getByRole("textbox"); | ||
fireEvent.focus(textBox); | ||
fireEvent.change(textBox, { target: { value: "abcdef" } }); | ||
fireEvent.blur(textBox); | ||
|
||
await waitFor(() => { | ||
const button = screen.getByRole("button"); | ||
expect(button).not.toHaveAttribute("disabled"); | ||
}); | ||
expect(getArgs()).toBe("abcdef"); | ||
}); | ||
|
||
it("should call callback when submit button is pressed", async () => { | ||
const [_, testToken] = makeFakeTokenTest(true); | ||
let submittedToken = ""; | ||
render( | ||
<TokenInputForm | ||
onTokenSubmit={(token: string) => { | ||
submittedToken = token; | ||
}} | ||
testToken={testToken} | ||
/>, | ||
); | ||
|
||
const textBox = screen.getByRole("textbox"); | ||
const button = screen.getByRole("button"); | ||
|
||
fireEvent.focus(textBox); | ||
fireEvent.change(textBox, { target: { value: "abcdef" } }); | ||
fireEvent.blur(textBox); | ||
|
||
await waitFor(() => { | ||
expect(button).not.toHaveAttribute("disabled"); | ||
}); | ||
|
||
fireEvent.click(button); | ||
expect(submittedToken).toBe("abcdef"); | ||
}); | ||
}); | ||
|
||
const makeFakeTokenTest: ( | ||
result: boolean, | ||
) => [() => string | undefined, (token: string) => Promise<boolean>] = (result) => { | ||
let called: string | undefined = undefined; | ||
|
||
return [ | ||
() => called, | ||
async (token: string) => { | ||
called = token; | ||
return result; | ||
}, | ||
]; | ||
}; |
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