Every locale formatter takes the language as an argument. Under the React
Compiler a formatter that read the i18n singleton was memoized on its other
arguments, so a card kept "2 hr 4 min" after a switch to Hungarian; plexLang()
had the same flaw and left Plex queries in the old language. Components now
take the language from useTranslation, and the signature makes forgetting it
a type error.
- ratings, file sizes, stream stats: the language's decimal mark ("8,3")
- view and subscriber counts compact in the language ("1,2 E")
- seconds as the language's unit ("6 mp") in the sliders and the stats
- a person's birth and death dates as dates, not ISO strings
- A-Z lists by the shown name in the language's alphabet (compareText),
the tag catalog re-sorted by its translated names
- lint local/locale-format (was date-format) also refuses Intl.NumberFormat
outside lib/format.ts, localeCompare, and a component reading the language
off the i18n singleton
97 lines
3.4 KiB
TypeScript
97 lines
3.4 KiB
TypeScript
// @vitest-environment jsdom
|
|
//
|
|
// The up-next end-card's core behaviour (E6 S4): the countdown that auto-advances at ~10s (the whole
|
|
// point — it REPLACED the old instant jump, so a regression that fires early or never is exactly the
|
|
// bug to guard), the "Play now"/Cancel controls, and the episode heading's null season/episode
|
|
// fallback. i18n is key-passthrough; `../lib/api` is mocked so no network runs; the countdown uses
|
|
// performance.now, which vitest's fake timers advance alongside setInterval.
|
|
import { describe, it, expect, afterEach, vi } from "vitest";
|
|
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
|
vi.mock("react-i18next", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("react-i18next")>();
|
|
return {
|
|
...actual,
|
|
useTranslation: () => ({ i18n: { language: "en" }, t: (key: string) => key }),
|
|
};
|
|
});
|
|
|
|
vi.mock("../lib/api", () => ({
|
|
api: { plexItem: vi.fn(), plexImageUrl: () => "/img" },
|
|
}));
|
|
|
|
import { UpNextCard } from "./UpNextCard";
|
|
import { api } from "../lib/api";
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
vi.useRealTimers();
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
function renderCard(opts: { onAdvance?: () => void; onCancel?: () => void } = {}) {
|
|
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
return render(
|
|
<QueryClientProvider client={qc}>
|
|
<UpNextCard
|
|
nextId="e7"
|
|
onAdvance={opts.onAdvance ?? vi.fn()}
|
|
onCancel={opts.onCancel ?? vi.fn()}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
}
|
|
|
|
describe("UpNextCard", () => {
|
|
it("auto-advances after the ~10s countdown, and NOT before", () => {
|
|
vi.useFakeTimers();
|
|
// A never-resolving detail query: the heading is irrelevant here and this avoids async state
|
|
// updates racing the fake clock.
|
|
(api.plexItem as ReturnType<typeof vi.fn>).mockReturnValue(new Promise(() => {}));
|
|
const onAdvance = vi.fn();
|
|
renderCard({ onAdvance });
|
|
|
|
act(() => {
|
|
vi.advanceTimersByTime(9000);
|
|
});
|
|
expect(onAdvance).not.toHaveBeenCalled(); // still counting down
|
|
|
|
act(() => {
|
|
vi.advanceTimersByTime(1200);
|
|
});
|
|
expect(onAdvance).toHaveBeenCalledTimes(1); // crossed zero → advanced exactly once
|
|
});
|
|
|
|
it("'Play now' advances immediately, without waiting for the countdown", () => {
|
|
(api.plexItem as ReturnType<typeof vi.fn>).mockReturnValue(new Promise(() => {}));
|
|
const onAdvance = vi.fn();
|
|
renderCard({ onAdvance });
|
|
fireEvent.click(screen.getByRole("button", { name: /playNow/ }));
|
|
expect(onAdvance).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("Cancel dismisses via onCancel and does not advance", () => {
|
|
(api.plexItem as ReturnType<typeof vi.fn>).mockReturnValue(new Promise(() => {}));
|
|
const onAdvance = vi.fn();
|
|
const onCancel = vi.fn();
|
|
renderCard({ onAdvance, onCancel });
|
|
fireEvent.click(screen.getByText("plex.player.upNext.cancel"));
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
expect(onAdvance).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("an episode heading falls back to S?·E? when season/episode are null", async () => {
|
|
(api.plexItem as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
id: "e7",
|
|
kind: "episode",
|
|
show_title: "Westworld",
|
|
title: "The Adversary",
|
|
season_number: null,
|
|
episode_number: null,
|
|
});
|
|
renderCard();
|
|
expect(await screen.findByText("Westworld · S?·E?")).toBeTruthy();
|
|
});
|
|
});
|