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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
- name: Test web React compatibility
run: >-
bun run --cwd apps/web test
__tests__/unit/add-videos-dialog-retry.test.ts
__tests__/unit/analytics-track-route.test.ts
__tests__/unit/caps-analytics-round-trips.test.ts
__tests__/unit/caps-keyboard.test.ts
Expand Down
246 changes: 246 additions & 0 deletions apps/web/__tests__/unit/add-videos-dialog-retry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
// @vitest-environment jsdom

import { Video } from "@cap/web-domain";
import { TooltipProvider } from "@radix-ui/react-tooltip";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import AddVideosDialogBase, {
type VideoData,
} from "@/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialogBase";

vi.mock("@cap/utils", async () => await import("@cap/utils/helpers"));
vi.mock("@cap/ui", async () => ({
...(await import("../../../../packages/ui/src/components/Button")),
...(await import("../../../../packages/ui/src/components/Dialog")),
...(await import("../../../../packages/ui/src/components/LoadingSpinner")),
...(await import("../../../../packages/ui/src/components/LogoSpinner")),
...(await import("../../../../packages/ui/src/components/input/Input")),
}));
vi.mock(
"@/app/(org)/dashboard/Contexts",
async () => import("@/app/(org)/dashboard/DashboardContext"),
);
vi.mock("@rive-app/react-canvas", () => ({
useRive: () => ({ RiveComponent: () => null }),
Layout: class {},
Fit: { Contain: "contain" },
}));

const newId = Video.VideoId.make("new-video");
const existingId = Video.VideoId.make("existing-video");
const videoData: VideoData[] = [newId, existingId].map((id) => ({
id,
name: id,
ownerId: "owner",
ownerName: "Owner",
createdAt: new Date("2026-01-01"),
totalComments: 0,
totalReactions: 0,
}));

let root: Root;
let container: HTMLDivElement;
let queryClient: QueryClient;

beforeEach(() => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response(null, { status: 503 })),
);
vi.stubGlobal("matchMedia", (media: string) => ({ matches: false, media }));
vi.stubGlobal(
"IntersectionObserver",
class {
observe() {}
unobserve() {}
disconnect() {}
},
);
vi.stubGlobal(
"ResizeObserver",
class {
constructor(private callback: ResizeObserverCallback) {}
observe(target: Element) {
this.callback(
[
{
target,
contentRect: new DOMRect(0, 0, 640, 300),
borderBoxSize: [{ inlineSize: 640, blockSize: 300 }],
contentBoxSize: [{ inlineSize: 640, blockSize: 300 }],
devicePixelContentBoxSize: [],
},
],
this,
);
}
unobserve() {}
disconnect() {}
},
);
vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockReturnValue(640);
vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(300);
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
queryClient = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
});
});

afterEach(async () => {
await act(async () => root.unmount());
container.remove();
queryClient.clear();
vi.unstubAllGlobals();
});

function button(label: string) {
const element = [...document.querySelectorAll("button")].find((element) =>
element.textContent?.includes(label),
);
if (!element) throw new Error(`Button not found: ${label}`);
return element;
}

async function flushUpdates() {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 30));
});
}

async function renderDialog(
props: Parameters<typeof AddVideosDialogBase<string>>[0],
) {
await act(async () => {
root.render(
createElement(
TooltipProvider,
null,
createElement(
QueryClientProvider,
{ client: queryClient },
createElement(AddVideosDialogBase<string>, props),
),
),
);
});
}

it.each(["addition", "removal"])(
"retries a failed %s without reversing the successful operation",
async (failure) => {
const membership = new Set([existingId]);
const addVideos = vi.fn(async (_entityId: string, ids: Video.VideoId[]) => {
for (const id of ids) membership.add(id);
return { success: true };
});
const removeVideos = vi.fn(
async (_entityId: string, ids: Video.VideoId[]) => {
for (const id of ids) membership.delete(id);
return { success: true };
},
);
if (failure === "addition")
addVideos.mockResolvedValueOnce({ success: false });
else removeVideos.mockResolvedValueOnce({ success: false });
const onClose = vi.fn();
await renderDialog({
open: true,
onClose,
entityId: "folder",
entityName: "Demo folder",
addVideos,
removeVideos,
getVideos: async () => ({ success: true, data: videoData }),
getEntityVideoIds: async () => ({
success: true,
data: [...membership],
}),
});
await flushUpdates();
await act(async () => button(newId).click());
await act(async () => button(existingId).click());
await act(async () => button("Update videos").click());
await flushUpdates();
expect([...membership]).toEqual(
failure === "addition" ? [] : [existingId, newId],
);
expect(onClose).not.toHaveBeenCalled();
await act(async () => button("Update videos").click());
await flushUpdates();
expect([...membership]).toEqual([newId]);
expect(addVideos).toHaveBeenCalledTimes(failure === "addition" ? 2 : 1);
expect(removeVideos).toHaveBeenCalledTimes(failure === "removal" ? 2 : 1);
expect(removeVideos).toHaveBeenLastCalledWith("folder", [existingId]);
expect(onClose).toHaveBeenCalledOnce();
},
);

it.each([false, true])(
"keeps reopened selections when the previous removal completes with success=%s",
async (success) => {
const membership = new Set([existingId]);
const pending = Promise.withResolvers<{ success: boolean }>();
const addVideos = vi.fn(async (_entityId: string, ids: Video.VideoId[]) => {
for (const id of ids) membership.add(id);
return { success: true };
});
const removeVideos = vi
.fn(async (_entityId: string, ids: Video.VideoId[]) => {
for (const id of ids) membership.delete(id);
return { success: true };
})
.mockImplementationOnce(async (_entityId, ids) => {
const result = await pending.promise;
if (result.success) {
for (const id of ids) membership.delete(id);
}
return result;
});
const onClose = vi.fn();
const onVideosAdded = vi.fn();
const props = {
open: true,
onClose,
onVideosAdded,
entityId: "folder",
entityName: "Demo folder",
addVideos,
removeVideos,
getVideos: async () => ({ success: true, data: videoData }),
getEntityVideoIds: async () => ({
success: true,
data: [...membership],
}),
};
await renderDialog(props);
await flushUpdates();
await act(async () => button(newId).click());
await act(async () => button(existingId).click());
await act(async () => button("Update videos").click());
await flushUpdates();
expect(removeVideos).toHaveBeenCalledOnce();
await act(async () => button("Cancel").click());
await renderDialog({ ...props, open: false });
await renderDialog(props);
await flushUpdates();
await act(async () => button(newId).click());
expect(document.body.textContent).toContain("1 video selected");
await act(async () => pending.resolve({ success }));
await flushUpdates();
expect(document.body.textContent).toContain("1 video selected");
expect(onClose).toHaveBeenCalledOnce();
expect(onVideosAdded).toHaveBeenCalledTimes(success ? 1 : 0);
await act(async () => button("Update videos").click());
await flushUpdates();
expect([...membership]).toEqual(success ? [] : [existingId]);
expect(addVideos).toHaveBeenCalledOnce();
expect(removeVideos).toHaveBeenLastCalledWith("folder", [newId]);
expect(onClose).toHaveBeenCalledTimes(2);
expect(onVideosAdded).toHaveBeenCalledTimes(success ? 2 : 1);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import clsx from "clsx";
import { Search } from "lucide-react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import * as z from "zod";
Expand Down Expand Up @@ -90,6 +90,7 @@ function AddVideosDialogBase<T>({
getEntityVideoIds,
}: AddVideosDialogBaseProp<T>) {
const [selectedVideos, setSelectedVideos] = useState<Video.VideoId[]>([]);
const dialogSession = useRef(0);
const [searchTerm, setSearchTerm] = useState("");
const filterTabs = ["all", "added", "notAdded"];

Expand Down Expand Up @@ -139,6 +140,7 @@ function AddVideosDialogBase<T>({
}: {
toAdd: Video.VideoId[];
toRemove: Video.VideoId[];
session: number;
}) => {
let addResult: UpdateVideosResult = { success: true };
let removeResult: UpdateVideosResult = { success: true };
Expand All @@ -150,8 +152,9 @@ function AddVideosDialogBase<T>({
}
return { addResult, removeResult };
},
onSuccess: async (result) => {
const { addResult, removeResult } = result || {};
onSuccess: async (result, { toAdd, toRemove, session }) => {
const { addResult, removeResult } = result;
const completed = addResult.success && removeResult.success;

// Invalidate both queries to ensure UI updates
await Promise.all([
Expand All @@ -161,10 +164,20 @@ function AddVideosDialogBase<T>({
}),
]);

if ((addResult?.success ?? true) && (removeResult?.success ?? true)) {
if (completed) onVideosAdded?.();
if (session !== dialogSession.current) return;

setSelectedVideos((selected) =>
selected.filter(
(id) =>
!(addResult.success && toAdd.includes(id)) &&
!(removeResult.success && toRemove.includes(id)),
),
);

if (completed) {
toast.success("Videos updated successfully");
setSelectedVideos([]);
onVideosAdded?.();
onClose();
} else {
toast.error(
Expand Down Expand Up @@ -214,7 +227,11 @@ function AddVideosDialogBase<T>({
const toAdd = selectedVideos.filter((id) => !entityVideoIds.includes(id));
// To remove: selected and already in entity
const toRemove = selectedVideos.filter((id) => entityVideoIds.includes(id));
updateVideosMutation.mutate({ toAdd, toRemove });
updateVideosMutation.mutate({
toAdd,
toRemove,
session: dialogSession.current,
});
};

// Reset state when dialog closes
Expand All @@ -224,6 +241,9 @@ function AddVideosDialogBase<T>({
setSearchTerm("");
form.reset();
}
return () => {
dialogSession.current += 1;
};
}, [open, form]);

return (
Expand Down
16 changes: 14 additions & 2 deletions apps/web/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ export default defineConfig({
optimizer: {
ssr: {
enabled: true,
include: ["next/link", "next/image", "react-remove-scroll"],
include: [
"next/link",
"next/image",
"react-remove-scroll",
"react-hook-form",
"@virtual-grid/react",
],
},
web: {
enabled: true,
include: ["next/link", "next/image", "react-remove-scroll"],
include: [
"next/link",
"next/image",
"react-remove-scroll",
"react-hook-form",
"@virtual-grid/react",
],
esbuildOptions: {
banner: {
js: `import { createRequire } from "node:module"; const require = createRequire(import.meta.url);`,
Expand Down
Loading