|
| 1 | +import "utils/dayjs"; // ensure dayjs timezone plugin is registered |
| 2 | + |
| 3 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | +import { service } from "service"; |
| 6 | +import users from "test/fixtures/users.json"; |
| 7 | +import wrapper from "test/hookWrapper"; |
| 8 | +import i18n from "test/i18n"; |
| 9 | +import { addDefaultUser, mockConsoleError, MockedService } from "test/utils"; |
| 10 | + |
| 11 | +import { ProfileUserProvider } from "../hooks/useProfileUser"; |
| 12 | +import NewHostRequest from "./NewHostRequest"; |
| 13 | + |
| 14 | +const { t } = i18n; |
| 15 | + |
| 16 | +jest.mock("@mui/x-date-pickers", () => ({ |
| 17 | + ...jest.requireActual("@mui/x-date-pickers"), |
| 18 | + DatePicker: jest.requireActual("@mui/x-date-pickers").DesktopDatePicker, |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock("features/analytics/hooks", () => ({ |
| 22 | + useLogEvent: () => jest.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock("features/analytics/foregroundTracker", () => ({ |
| 26 | + createForegroundTracker: () => ({ |
| 27 | + onVisibilityChange: jest.fn(), |
| 28 | + finalize: () => ({ foregroundMs: 0, totalMs: 0 }), |
| 29 | + }), |
| 30 | +})); |
| 31 | + |
| 32 | +jest.mock("features/userQueries/useLiteUsers", () => ({ |
| 33 | + useLiteUser: () => ({ isLoading: false, error: null }), |
| 34 | +})); |
| 35 | + |
| 36 | +const createHostRequestMock = service.requests |
| 37 | + .createHostRequest as MockedService< |
| 38 | + typeof service.requests.createHostRequest |
| 39 | +>; |
| 40 | + |
| 41 | +const [, hostUser] = users; // funnydog, userId=2 |
| 42 | + |
| 43 | +const LONG_TEXT = "a".repeat(250); |
| 44 | + |
| 45 | +function renderNewHostRequest() { |
| 46 | + render( |
| 47 | + <ProfileUserProvider user={hostUser}> |
| 48 | + <NewHostRequest |
| 49 | + setIsRequestSuccess={jest.fn()} |
| 50 | + setIsRequesting={jest.fn()} |
| 51 | + /> |
| 52 | + </ProfileUserProvider>, |
| 53 | + { wrapper }, |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +describe("NewHostRequest", () => { |
| 58 | + beforeEach(() => { |
| 59 | + addDefaultUser(); |
| 60 | + jest.useFakeTimers(); |
| 61 | + jest.setSystemTime(new Date("2026-05-24")); |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + jest.useRealTimers(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("preserves form data when the API call fails", async () => { |
| 69 | + mockConsoleError(); |
| 70 | + createHostRequestMock.mockRejectedValue(new Error("Network error")); |
| 71 | + renderNewHostRequest(); |
| 72 | + |
| 73 | + const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime }); |
| 74 | + |
| 75 | + const arrivalGroup = await screen.findByRole("group", { |
| 76 | + name: t("profile:request_form.arrival_date"), |
| 77 | + }); |
| 78 | + await user.click(arrivalGroup); |
| 79 | + await user.keyboard("{Control>}a{/Control}"); |
| 80 | + await user.keyboard("06012026"); |
| 81 | + |
| 82 | + const departureGroup = screen.getByRole("group", { |
| 83 | + name: t("profile:request_form.departure_date"), |
| 84 | + }); |
| 85 | + await user.click(departureGroup); |
| 86 | + await user.keyboard("{Control>}a{/Control}"); |
| 87 | + await user.keyboard("06052026"); |
| 88 | + |
| 89 | + const textArea = screen.getByLabelText(t("profile:request_form.request")); |
| 90 | + fireEvent.change(textArea, { target: { value: LONG_TEXT } }); |
| 91 | + |
| 92 | + await user.click(screen.getByRole("button", { name: t("global:send") })); |
| 93 | + |
| 94 | + expect(await screen.findByRole("alert")).toHaveTextContent("Network error"); |
| 95 | + expect(textArea).toHaveValue(LONG_TEXT); |
| 96 | + }); |
| 97 | + |
| 98 | + it("rejects an arrival date that is already in the past in the host's timezone", async () => { |
| 99 | + // At 2026-05-24T22:00:00Z it is still May 24 for the requester (UTC), but |
| 100 | + // the host (Europe/Helsinki, UTC+3) is already on May 25. The requester |
| 101 | + // choosing "today" (May 24) should be treated as a past date and the form |
| 102 | + // should NOT submit to the API. |
| 103 | + jest.setSystemTime(new Date("2026-05-24T22:00:00Z")); |
| 104 | + |
| 105 | + createHostRequestMock.mockResolvedValue(1); |
| 106 | + render( |
| 107 | + <ProfileUserProvider user={hostUser}> |
| 108 | + <NewHostRequest |
| 109 | + setIsRequestSuccess={jest.fn()} |
| 110 | + setIsRequesting={jest.fn()} |
| 111 | + /> |
| 112 | + </ProfileUserProvider>, |
| 113 | + { wrapper }, |
| 114 | + ); |
| 115 | + |
| 116 | + const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime }); |
| 117 | + |
| 118 | + // Type May 24 — today from the requester's perspective, but yesterday in |
| 119 | + // Helsinki where the host lives. |
| 120 | + const arrivalGroup = await screen.findByRole("group", { |
| 121 | + name: t("profile:request_form.arrival_date"), |
| 122 | + }); |
| 123 | + await user.click(arrivalGroup); |
| 124 | + await user.keyboard("{Control>}a{/Control}"); |
| 125 | + await user.keyboard("05242026"); |
| 126 | + |
| 127 | + const departureGroup = screen.getByRole("group", { |
| 128 | + name: t("profile:request_form.departure_date"), |
| 129 | + }); |
| 130 | + await user.click(departureGroup); |
| 131 | + await user.keyboard("{Control>}a{/Control}"); |
| 132 | + await user.keyboard("05282026"); |
| 133 | + |
| 134 | + const textArea = screen.getByLabelText(t("profile:request_form.request")); |
| 135 | + fireEvent.change(textArea, { target: { value: LONG_TEXT } }); |
| 136 | + |
| 137 | + await user.click(screen.getByRole("button", { name: t("global:send") })); |
| 138 | + |
| 139 | + // The form should catch the invalid date before hitting the API. |
| 140 | + expect(createHostRequestMock).not.toHaveBeenCalled(); |
| 141 | + // The request text must still be intact so the user doesn't lose their work. |
| 142 | + expect(textArea).toHaveValue(LONG_TEXT); |
| 143 | + }); |
| 144 | + |
| 145 | + it("allows an arrival date that is today in the host's timezone when the requester is ahead", async () => { |
| 146 | + // At 2026-05-25T05:00:00Z it is May 25 in UTC but still May 24 in |
| 147 | + // America/Los_Angeles (UTC-7). Picking May 25 is the host's "tomorrow" — |
| 148 | + // a valid future date that must go through. |
| 149 | + jest.setSystemTime(new Date("2026-05-25T05:00:00Z")); |
| 150 | + |
| 151 | + createHostRequestMock.mockResolvedValue(1); |
| 152 | + const hostBehindTimezone = { ...users[1], timezone: "America/Los_Angeles" }; |
| 153 | + render( |
| 154 | + <ProfileUserProvider user={hostBehindTimezone}> |
| 155 | + <NewHostRequest |
| 156 | + setIsRequestSuccess={jest.fn()} |
| 157 | + setIsRequesting={jest.fn()} |
| 158 | + /> |
| 159 | + </ProfileUserProvider>, |
| 160 | + { wrapper }, |
| 161 | + ); |
| 162 | + |
| 163 | + const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime }); |
| 164 | + |
| 165 | + const arrivalGroup = await screen.findByRole("group", { |
| 166 | + name: t("profile:request_form.arrival_date"), |
| 167 | + }); |
| 168 | + await user.click(arrivalGroup); |
| 169 | + await user.keyboard("{Control>}a{/Control}"); |
| 170 | + await user.keyboard("05252026"); |
| 171 | + |
| 172 | + const departureGroup = screen.getByRole("group", { |
| 173 | + name: t("profile:request_form.departure_date"), |
| 174 | + }); |
| 175 | + await user.click(departureGroup); |
| 176 | + await user.keyboard("{Control>}a{/Control}"); |
| 177 | + await user.keyboard("05302026"); |
| 178 | + |
| 179 | + fireEvent.change(screen.getByLabelText(t("profile:request_form.request")), { |
| 180 | + target: { value: LONG_TEXT }, |
| 181 | + }); |
| 182 | + |
| 183 | + await user.click(screen.getByRole("button", { name: t("global:send") })); |
| 184 | + |
| 185 | + await waitFor(() => expect(createHostRequestMock).toHaveBeenCalled()); |
| 186 | + expect(screen.queryByRole("alert")).not.toBeInTheDocument(); |
| 187 | + }); |
| 188 | +}); |
0 commit comments