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
// FormPrompt.js | |
import { useEffect } from "react"; | |
export const FormPrompt = ({ hasUnsavedChanges }) => { | |
useEffect(() => { | |
const onBeforeUnload = (e) => { | |
if (hasUnsavedChanges) { | |
e.preventDefault(); | |
e.returnValue = ""; | |
} |
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
//Steps/Contact.js | |
export const Contact = forwardRef((props, ref) => { | |
//.. | |
return ( | |
<Form onSubmit={handleSubmit(saveData)}> | |
//.. | |
<Button ref={ref}>Next {">"}</Button> | |
</Form> | |
); | |
}); |
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
// state.js | |
import React, { createContext, useContext, useState } from "react"; | |
export const AppStateContext = createContext(); | |
export function AppProvider({ children }) { | |
const value = useState({}); | |
return ( | |
<AppStateContext.Provider value={value}> |
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
it("renders without breaking", async () => { | |
render(<ListPage />); | |
await waitForElementToBeRemoved(() => screen.queryByText("Loading...")); | |
}); |
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
describe("ListPage", () => { | |
it("renders without breaking", async () => { | |
render(<ListPage />); | |
expect( | |
await screen.findByRole("heading", { name: "List of items" }) | |
).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
import { waitFor } from "@testing-library/react"; | |
//... | |
describe("ListPage", () => { | |
it("renders without breaking", async () => { | |
render(<ListPage />); | |
await waitFor(() => { | |
expect( | |
screen.getByRole("heading", { name: "List of items" }) |
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
export const ListPage = () => { | |
const [items, setItems] = useState([]); | |
useEffect(() => { | |
const loadItems = async () => { | |
setTimeout(() => setItems(["Item 1", "Item 2"]), 100); | |
}; | |
loadItems(); | |
}, []); |
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
describe("Form", () => { | |
it("should save correct data on submit", async () => { | |
const mockSave = jest.fn(); | |
render(<Form saveData={mockSave} />); | |
await userEvent.type(screen.getByRole("textbox", { name: "Name" }), "Test"); | |
await userEvent.click(screen.getByRole("button", { name: "Sign up" })); | |
expect(mockSave).toHaveBeenLastCalledWith({ ...defaultData, name: "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
describe("Form", () => { | |
it("should save correct data on submit", () => { | |
const mockSave = jest.fn(); | |
render(<Form saveData={mockSave} />); | |
fireEvent.change(screen.getByRole("textbox", { name: "Name" }), { | |
target: { value: "Test" }, | |
}); | |
fireEvent.submit(screen.getByRole("button", { name: "Sign up" })); | |
expect(mockSave).toHaveBeenLastCalledWith({ ...defaultData, name: "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
<label htmlFor="name">Name</label> | |
<input | |
id="name" | |
onChange={onFieldChange} | |
placeholder="Enter your name" | |
/> |
NewerOlder