Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): keep Backspace working with selected Caps

Akshar Patel04a0662a2675
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 926bdcd44..175589791 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -125,6 +125,7 @@ jobs:
- name: Test web React compatibility
run: >-
bun run --cwd apps/web test
+ __tests__/unit/caps-keyboard.test.ts
__tests__/unit/dashboard-analytics-cap-access.test.ts
__tests__/unit/dashboard-analytics-space-access.test.ts
__tests__/unit/embed-video-playback-chrome.test.ts
diff --git a/apps/web/__tests__/unit/caps-keyboard.test.ts b/apps/web/__tests__/unit/caps-keyboard.test.ts
new file mode 100644
index 000000000..747e2b292
--- /dev/null
+++ b/apps/web/__tests__/unit/caps-keyboard.test.ts
@@ -0,0 +1,155 @@
+// @vitest-environment jsdom
+
+import { act, createElement } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { Caps, type VideoData } from "@/app/(org)/dashboard/caps/Caps";
+
+const mocks = vi.hoisted(() => ({ mutate: vi.fn() }));
+
+vi.mock("@cap/ui", () => ({ Button: () => null }));
+vi.mock("@fortawesome/free-solid-svg-icons", () => ({
+ faFolderPlus: {},
+ faInfoCircle: {},
+}));
+vi.mock("@fortawesome/react-fontawesome", () => ({
+ FontAwesomeIcon: () => null,
+}));
+vi.mock("next/navigation", () => ({
+ useRouter: () => ({ refresh: vi.fn() }),
+ useSearchParams: () => new URLSearchParams(),
+}));
+vi.mock("sonner", () => ({ toast: {} }));
+vi.mock("@/lib/EffectRuntime", () => ({
+ useRpcClient: () => ({}),
+ useEffectMutation: () => ({ mutate: mocks.mutate, isPending: false }),
+}));
+vi.mock("@/lib/Queries/Analytics", () => ({
+ useVideosAnalyticsQuery: () => ({ data: {}, isLoading: false }),
+}));
+vi.mock("@/components/ChromeRecorderButton", () => ({
+ ChromeRecorderButton: () => null,
+}));
+vi.mock("@/app/(org)/dashboard/Contexts", () => ({
+ useDashboardContext: () => ({ user: { id: "user-1" } }),
+}));
+vi.mock("@/app/(org)/dashboard/caps/UploadingContext", () => ({
+ useUploadingStatus: () => [false, null],
+}));
+vi.mock("@/app/(org)/dashboard/caps/components", () => ({
+ NewFolderDialog: () => null,
+ SelectedCapsBar: () => null,
+ UploadCapButton: () => null,
+ UploadPlaceholderCard: () => null,
+ WebRecorderDialog: () => null,
+}));
+vi.mock("@/app/(org)/dashboard/caps/components/CapCard/CapCard", () => ({
+ CapCard: ({
+ onSelectToggle,
+ isSelected,
+ }: {
+ onSelectToggle: () => void;
+ isSelected: boolean;
+ }) =>
+ createElement("button", {
+ type: "button",
+ "aria-label": "Select Cap",
+ "aria-pressed": isSelected,
+ onClick: onSelectToggle,
+ }),
+}));
+vi.mock("@/app/(org)/dashboard/caps/components/CapPagination", () => ({
+ CapPagination: () => null,
+}));
+vi.mock("@/app/(org)/dashboard/caps/components/EmptyCapState", () => ({
+ EmptyCapState: () => null,
+}));
+vi.mock("@/app/(org)/dashboard/caps/components/FoldersSection", () => ({
+ FoldersSection: () => null,
+}));
+
+const data: VideoData = [
+ {
+ id: "cap-1" as VideoData[number]["id"],
+ ownerId: "user-1",
+ name: "Demo",
+ createdAt: new Date("2026-10-24T12:00:00Z"),
+ public: false,
+ totalComments: 0,
+ totalReactions: 0,
+ foldersData: [],
+ sharedOrganizations: [],
+ ownerName: "Test user",
+ source: null,
+ isScreenshot: false,
+ hasPassword: false,
+ hasActiveUpload: false,
+ },
+];
+
+let root: Root;
+let container: HTMLDivElement;
+let selection: HTMLButtonElement;
+
+beforeEach(async () => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ mocks.mutate.mockClear();
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ await act(async () => {
+ root.render(
+ createElement(Caps, {
+ data,
+ count: 1,
+ folders: [],
+ analyticsEnabled: false,
+ }),
+ );
+ });
+ const button = container.querySelector<HTMLButtonElement>("button");
+ if (!button) throw new Error("Selection button missing");
+ selection = button;
+ await act(async () => selection.click());
+ expect(selection.getAttribute("aria-pressed")).toBe("true");
+});
+
+afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+});
+
+describe("Cap selection keyboard shortcuts", () => {
+ it.each(["input", "textarea", "select"] as const)(
+ "preserves Backspace editing in a focused %s",
+ (tag) => {
+ const control = document.createElement(tag);
+ container.append(control);
+ control.focus();
+ const event = new KeyboardEvent("keydown", {
+ key: "Backspace",
+ bubbles: true,
+ cancelable: true,
+ });
+ control.dispatchEvent(event);
+ expect(event.defaultPrevented).toBe(false);
+ expect(mocks.mutate).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each(["Backspace", "Delete"])(
+ "still deletes the selected Cap with %s outside editing controls",
+ (key) => {
+ selection.focus();
+ const event = new KeyboardEvent("keydown", {
+ key,
+ bubbles: true,
+ cancelable: true,
+ });
+ selection.dispatchEvent(event);
+ expect(mocks.mutate).toHaveBeenCalledExactlyOnceWith([data[0]?.id]);
+ expect(event.defaultPrevented).toBe(key === "Backspace");
+ },
+ );
+});
diff --git a/apps/web/app/(org)/dashboard/caps/Caps.tsx b/apps/web/app/(org)/dashboard/caps/Caps.tsx
index c3fd088ff..d6721cded 100644
--- a/apps/web/app/(org)/dashboard/caps/Caps.tsx
+++ b/apps/web/app/(org)/dashboard/caps/Caps.tsx
@@ -193,15 +193,15 @@ export const Caps = ({
(e.key === "Delete" || e.key === "Backspace") &&
selectedCaps.length > 0
) {
- if (e.key === "Backspace") {
- e.preventDefault();
- }
-
if (
!["INPUT", "TEXTAREA", "SELECT"].includes(
document.activeElement?.tagName || "",
)
) {
+ if (e.key === "Backspace") {
+ e.preventDefault();
+ }
+
deleteCaps(selectedCaps);
}
}