2022-03-17 14:59:04 +00:00
|
|
|
import useForm, { SubmitPromise } from "@saleor/hooks/useForm";
|
|
|
|
import { act, renderHook } from "@testing-library/react-hooks";
|
|
|
|
import React from "react";
|
|
|
|
import { useHistory } from "react-router";
|
|
|
|
import { MemoryRouter } from "react-router-dom";
|
|
|
|
|
|
|
|
import {
|
|
|
|
ExitFormDialogContext,
|
2022-06-21 09:36:55 +00:00
|
|
|
useExitFormDialogProvider,
|
2022-03-17 14:59:04 +00:00
|
|
|
} from "./ExitFormDialogProvider";
|
|
|
|
import { useExitFormDialog } from "./useExitFormDialog";
|
|
|
|
|
|
|
|
jest.mock("../../hooks/useNotifier", () => undefined);
|
|
|
|
|
|
|
|
const MockExitFormDialogProvider = ({ children }) => {
|
|
|
|
const { providerData } = useExitFormDialogProvider();
|
|
|
|
return (
|
|
|
|
<ExitFormDialogContext.Provider value={providerData}>
|
|
|
|
{children}
|
|
|
|
</ExitFormDialogContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const initialPath = "/";
|
|
|
|
const targetPath = "/path";
|
|
|
|
|
|
|
|
const setup = (submitFn: () => SubmitPromise, confirmLeave = true) =>
|
|
|
|
renderHook(
|
|
|
|
() => {
|
|
|
|
const form = useForm({ field: "" }, submitFn, { confirmLeave });
|
|
|
|
const exit = useExitFormDialog();
|
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
return {
|
|
|
|
form,
|
|
|
|
exit,
|
2022-06-21 09:36:55 +00:00
|
|
|
history,
|
2022-03-17 14:59:04 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
{
|
|
|
|
wrapper: ({ children }) => (
|
|
|
|
<MemoryRouter initialEntries={[{ pathname: "/" }]}>
|
|
|
|
<MockExitFormDialogProvider>{children}</MockExitFormDialogProvider>
|
|
|
|
</MemoryRouter>
|
2022-06-21 09:36:55 +00:00
|
|
|
),
|
|
|
|
},
|
2022-03-17 14:59:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
describe("useExitFormDialog", () => {
|
|
|
|
it("blocks navigation after leaving dirty form", async () => {
|
|
|
|
// Given
|
|
|
|
const submitFn = jest.fn(() => Promise.resolve([]));
|
|
|
|
const { result } = setup(submitFn);
|
|
|
|
|
|
|
|
// When
|
|
|
|
act(() => {
|
|
|
|
result.current.form.change({
|
2022-06-21 09:36:55 +00:00
|
|
|
target: { name: "field", value: "something" },
|
2022-03-17 14:59:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
result.current.history.push(targetPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(result.current.exit.shouldBlockNavigation()).toBe(true);
|
|
|
|
expect(result.current.history.location.pathname).toBe(initialPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("allows navigation after leaving dirty form if no confirmation is needed", async () => {
|
|
|
|
// Given
|
|
|
|
const submitFn = jest.fn(() => Promise.resolve([]));
|
|
|
|
const { result } = setup(submitFn, false);
|
|
|
|
|
|
|
|
// When
|
|
|
|
act(() => {
|
|
|
|
result.current.form.change({
|
2022-06-21 09:36:55 +00:00
|
|
|
target: { name: "field", value: "something" },
|
2022-03-17 14:59:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
result.current.history.push(targetPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(result.current.exit.shouldBlockNavigation()).toBe(false);
|
|
|
|
expect(result.current.history.location.pathname).toBe(targetPath);
|
|
|
|
});
|
2022-04-27 12:57:10 +00:00
|
|
|
it("navigates to full url with querystring", async () => {
|
|
|
|
// Given
|
|
|
|
const submitFn = jest.fn(() => Promise.resolve([]));
|
|
|
|
const { result } = setup(submitFn);
|
|
|
|
const qs = "?param=value";
|
|
|
|
const targetPathWithQs = targetPath + qs;
|
|
|
|
|
|
|
|
// When
|
|
|
|
act(() => {
|
|
|
|
result.current.form.change({
|
2022-06-21 09:36:55 +00:00
|
|
|
target: { name: "field", value: "something" },
|
2022-04-27 12:57:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
act(() => {
|
|
|
|
result.current.history.push(targetPathWithQs);
|
|
|
|
result.current.exit.leave();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(result.current.history.location.pathname).toBe(targetPath);
|
|
|
|
expect(result.current.history.location.search).toBe(qs);
|
|
|
|
});
|
2022-03-17 14:59:04 +00:00
|
|
|
});
|