Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { StrictMode, useEffect, useState } from "react";
import RecommendationsClient from "./RecommendationsClient";
import { MovieProvider, useMovieContext } from "@/contexts/MovieContext";
import {
Expand Down Expand Up @@ -53,15 +53,17 @@ function MovieSessionSnapshot() {
);
}

function renderRecommendations() {
return render(
function renderRecommendations({ strictMode = false }: { strictMode?: boolean } = {}) {
const content = (
<MovieProvider>
<RecommendationsSessionInitialiser>
<RecommendationsClient />
<MovieSessionSnapshot />
</RecommendationsSessionInitialiser>
</MovieProvider>
);

return render(strictMode ? <StrictMode>{content}</StrictMode> : content);
}

function getRequestUrl(input: RequestInfo | URL): string {
Expand Down Expand Up @@ -121,6 +123,38 @@ describe("RecommendationsClient integration", () => {
expect(screen.getByAltText("Mad Max: Fury Road")).toBeInTheDocument();
});

it("still submits recommendations in strict mode after effect replay", async () => {
let recommendationRequestCount = 0;

global.fetch = jest.fn((input: RequestInfo | URL) => {
const url = getRequestUrl(input);

if (url === "/api/recommendations") {
recommendationRequestCount += 1;
return Promise.resolve(createRecommendationStreamResponse());
}

if (url.startsWith("https://api.themoviedb.org/")) {
return Promise.resolve(
createJsonResponse(tmdbFixtures.postersByQuery["Mad Max: Fury Road"])
);
}

return Promise.reject(new Error(`Unhandled fetch: ${url}`));
}) as typeof fetch;

renderRecommendations({ strictMode: true });

await waitFor(() => {
expect(recommendationRequestCount).toBeGreaterThan(0);
expect(
screen.getByRole("heading", { name: "Mad Max: Fury Road (2015)" })
).toBeInTheDocument();
});

expect(screen.getByText("Movie 1 of 2")).toBeInTheDocument();
});

it("renders a no-results state when the streamed response is empty", async () => {
global.fetch = jest.fn((input: RequestInfo | URL) => {
const url = getRequestUrl(input);
Expand Down Expand Up @@ -166,6 +200,24 @@ describe("RecommendationsClient integration", () => {
expect(screen.getByText(/Pipeline failed/)).toBeInTheDocument();
});

it("renders an error message when the recommendations request cannot be reached at the network level", async () => {
global.fetch = jest.fn((input: RequestInfo | URL) => {
const url = getRequestUrl(input);

if (url === "/api/recommendations") {
return Promise.reject(new Error("Failed to fetch"));
}

return Promise.reject(new Error(`Unhandled fetch: ${url}`));
}) as typeof fetch;

renderRecommendations();

await waitFor(() => {
expect(screen.getByText("Oops! Something went wrong")).toBeInTheDocument();
});
});

it("clears the movie session and routes home when Start Over is pressed", async () => {
global.fetch = jest.fn((input: RequestInfo | URL) => {
const url = getRequestUrl(input);
Expand Down
12 changes: 8 additions & 4 deletions app/(routes)/recommendations/RecommendationsClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function RecommendationsClient() {
const [state, dispatch] = useReducer(recommendationsViewReducer, initialRecommendationsState);
const { currentIndex, posterUrls, isLoadingPoster } = state;
const hasSubmittedRef = useRef(false);
const wasStoppedRef = useRef(false);

useEffect(() => {
if (participantsData.length === 0) {
Expand All @@ -75,14 +76,17 @@ export default function RecommendationsClient() {
});

useEffect(() => {
if (participantsData.length > 0 && !hasSubmittedRef.current) {
hasSubmittedRef.current = true;
submit({ participantsData, timeAvailable });
}
if (participantsData.length === 0) return;
if (hasSubmittedRef.current && !wasStoppedRef.current) return;

hasSubmittedRef.current = true;
wasStoppedRef.current = false;
void submit({ participantsData, timeAvailable });
}, [participantsData, timeAvailable, submit]);

useEffect(() => {
return () => {
wasStoppedRef.current = true;
stop();
};
}, [stop]);
Expand Down
Loading