Skip to content

Commit 9af5474

Browse files
committed
refactor: remove dead code and update error handling in movie search
1 parent 613fb90 commit 9af5474

6 files changed

Lines changed: 65 additions & 18 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { hideNotice, setNoticeImpl } from "../noticeFunctions";
3+
4+
describe("noticeFunctions", () => {
5+
afterEach(() => {
6+
setNoticeImpl(null);
7+
});
8+
9+
it("hideNotice clears React notice when setter is registered", () => {
10+
const setter = vi.fn();
11+
setNoticeImpl(setter);
12+
13+
hideNotice();
14+
15+
expect(setter).toHaveBeenCalledWith(null);
16+
});
17+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @vitest-environment jsdom
2+
import { describe, it, expect, vi, beforeEach } from "vitest";
3+
import { act, renderHook, waitFor } from "@testing-library/react";
4+
import { useMovieSearch } from "../useMovieSearch";
5+
import { showError } from "../showError";
6+
7+
vi.mock("../showMessage", () => ({
8+
showMessage: vi.fn(),
9+
}));
10+
11+
vi.mock("../showError", () => ({
12+
showError: vi.fn(),
13+
}));
14+
15+
describe("useMovieSearch", () => {
16+
beforeEach(() => {
17+
vi.clearAllMocks();
18+
});
19+
20+
it("shows an error toast when network request fails", async () => {
21+
globalThis.fetch = vi.fn().mockRejectedValue(new TypeError("Failed to fetch"));
22+
23+
const { result } = renderHook(() => useMovieSearch());
24+
25+
await act(async () => {
26+
result.current({ title: "The Matrix", year: "1999", country: "US" });
27+
});
28+
29+
await waitFor(() => {
30+
expect(showError).toHaveBeenCalledWith(
31+
"Movie search failed. Check your connection and try again.",
32+
);
33+
});
34+
});
35+
});

src/client/src/movieTiles.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ export function classifyListReportSymptom(
6464
}
6565

6666
export function normalizeId(title: string, year: string | number | null): string {
67-
return `${year}-${title
68-
.toUpperCase()
69-
.replace(/ /g, "-")
70-
.replace(/[^A-Z0-9]/g, "")}`;
67+
return `${year}-${title.toUpperCase().replace(/[^A-Z0-9]/g, "")}`;
7168
}
7269

7370
/** Letterboxd list entries often use root-relative paths; keep absolute URLs for consumers. */
@@ -195,13 +192,3 @@ export function mergeTileStateForTab(
195192
export function getTileProviderNames(tileData: TileData | null | undefined): string[] {
196193
return (tileData?.movieProviders ?? []).map((p) => p.name);
197194
}
198-
199-
export function tileMatchesFilter(
200-
tileData: TileData | null | undefined,
201-
activeProviderNames: string[] | null | undefined,
202-
): boolean {
203-
if (!activeProviderNames?.length) return true;
204-
const names = getTileProviderNames(tileData);
205-
if (!names.length) return false;
206-
return activeProviderNames.some((n) => names.includes(n));
207-
}

src/client/src/noticeFunctions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export const toggleNotice = (msg: string | null | undefined): void => {
4949
};
5050

5151
export const hideNotice = (): void => {
52+
if (noticeSetter) {
53+
noticeSetter(null);
54+
return;
55+
}
5256
const impl = getToastImpl();
5357
if (impl?.dismissLoading && noticeId != null) {
5458
impl.dismissLoading(noticeId);

src/client/src/useMovieSearch.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useRef } from "react";
22
import { showMessage } from "./showMessage";
3+
import { showError } from "./showError";
34
import { PLACEHOLDER_POSTER, normalizePosterPath, type MergeData } from "./movieTiles";
45
import { captureFrontendException, captureFrontendMessage } from "./sentry";
56
import { SafeJsonResponseError, safeJsonResponse } from "./safeJsonResponse";
@@ -93,6 +94,8 @@ export function useMovieSearch(
9394
country: data.country,
9495
},
9596
});
97+
} else {
98+
showError("Movie search failed. Check your connection and try again.");
9699
}
97100
captureFrontendException(err, {
98101
tags: { source: "api", endpoint: HTTP_API_PATHS.searchMovie },

tests/stateTileManagement.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ const lakeMungoSearch = searchMovieData.find(
2121
)?.response;
2222

2323
function generateTileId(title: string, year: string | null): string {
24-
return `${year}-${title
25-
.toUpperCase()
26-
.replace(/ /g, "-")
27-
.replace(/[^A-Z0-9]/g, "")}`;
24+
return `${year}-${title.toUpperCase().replace(/[^A-Z0-9]/g, "")}`;
2825
}
2926

3027
interface TileData {
@@ -71,6 +68,10 @@ describe("state tile ID management", () => {
7168
);
7269
});
7370

71+
it("strips spaces instead of preserving hyphens", () => {
72+
expect(generateTileId("A Space Heavy Title", "2024")).toBe("2024-ASPACEHEAVYTITLE");
73+
});
74+
7475
it("handles null year", () => {
7576
expect(generateTileId("Her Private Hell", null)).toBe("null-HERPRIVATEHELL");
7677
});

0 commit comments

Comments
 (0)