Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): clear stale converter selection on invalid input

Aditya Garudc4d8e2c07918
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f18abceea..9f707a019 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -126,6 +126,7 @@ jobs:
__tests__/unit/homepage-demo-accessibility.test.ts
__tests__/unit/linkified-text.test.ts
__tests__/unit/loom-import-ui.test.ts
+ __tests__/unit/media-format-converter-selection.test.ts
__tests__/unit/media-player-store-refs.test.ts
__tests__/unit/playback-store.test.ts
__tests__/unit/seo-demo-videos.test.ts
diff --git a/apps/web/__tests__/unit/media-format-converter-selection.test.ts b/apps/web/__tests__/unit/media-format-converter-selection.test.ts
new file mode 100644
index 000000000..7b4fc7862
--- /dev/null
+++ b/apps/web/__tests__/unit/media-format-converter-selection.test.ts
@@ -0,0 +1,80 @@
+// @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 { MediaFormatConverter } from "@/components/tools/MediaFormatConverter";
+
+vi.mock("@cap/ui", () => ({
+ Button: ({ children, ...props }: React.ComponentProps<"button">) =>
+ createElement("button", { type: "button", ...props }, children),
+}));
+vi.mock("next/link", () => ({
+ default: ({ children, ...props }: React.ComponentProps<"a">) =>
+ createElement("a", props, children),
+}));
+vi.mock("next/navigation", () => ({
+ usePathname: () => "/tools/convert/mp4-to-webm",
+ useRouter: () => ({ push: vi.fn() }),
+}));
+vi.mock("@/app/utils/analytics", () => ({ trackEvent: vi.fn() }));
+
+describe("media converter file selection", () => {
+ let container: HTMLDivElement;
+ let root: Root;
+
+ beforeEach(async () => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ vi.stubGlobal("MediaRecorder", { isTypeSupported: () => true });
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ await act(async () =>
+ root.render(
+ createElement(MediaFormatConverter, {
+ initialConversionPath: "mp4-to-webm",
+ }),
+ ),
+ );
+ });
+
+ afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+ });
+
+ const selectFile = async (container: HTMLElement, file: File) => {
+ const input = container.querySelector("input[type=file]");
+ if (!(input instanceof HTMLInputElement)) throw new Error("Missing input");
+ Object.defineProperty(input, "files", {
+ configurable: true,
+ value: [file],
+ });
+ await act(async () =>
+ input.dispatchEvent(new Event("change", { bubbles: true })),
+ );
+ };
+
+ it.each([
+ ["invalid type", new File(["wrong"], "notes.txt", { type: "text/plain" })],
+ ["oversized", new File(["x"], "huge.mp4", { type: "video/mp4" })],
+ ])(
+ "clears the previous video after an %s replacement",
+ async (reason, replacement) => {
+ if (reason === "oversized") {
+ Object.defineProperty(replacement, "size", {
+ value: 500 * 1024 * 1024 + 1,
+ });
+ }
+ await selectFile(
+ container,
+ new File(["video"], "first.mp4", { type: "video/mp4" }),
+ );
+ expect(container.textContent).toContain("first.mp4");
+ await selectFile(container, replacement);
+ expect(container.textContent).not.toContain("first.mp4");
+ expect(container.textContent).not.toContain("Convert to WEBM");
+ },
+ );
+});
diff --git a/apps/web/components/tools/MediaFormatConverter.tsx b/apps/web/components/tools/MediaFormatConverter.tsx
index da68d9cf4..42edec693 100644
--- a/apps/web/components/tools/MediaFormatConverter.tsx
+++ b/apps/web/components/tools/MediaFormatConverter.tsx
@@ -253,7 +253,12 @@ export const MediaFormatConverter = ({
const validateAndSetFile = (selectedFile: File) => {
setError(null);
+ setFile(null);
+ if (outputUrl) {
+ URL.revokeObjectURL(outputUrl);
+ }
setOutputUrl(null);
+ recordedChunksRef.current = [];
const expectedMimeType = getMimeType(currentSourceFormat);