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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snap/react-camera-kit",
"version": "0.3.0",
"version": "0.3.1",
"description": "React Camera Kit for web applications",
"type": "module",
"main": "./dist/cjs/index.js",
Expand Down
55 changes: 44 additions & 11 deletions src/useApplyLens.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, waitFor } from "@testing-library/react";
import { act, renderHook, waitFor } from "@testing-library/react";
import hash from "stable-hash";
import { useApplyLens } from "./useApplyLens";
import { useInternalCameraKit } from "./CameraKitProvider";
Expand All @@ -15,6 +15,16 @@ const mockUseInternalCameraKit = useInternalCameraKit as jest.MockedFunction<typ
const mockHash = hash as jest.MockedFunction<typeof hash>;
const mockReportCount = metricsReporter.reportCount as jest.Mock;

function deferred<T>() {
let resolve: (value: T) => void = () => {};
let reject: (reason?: unknown) => void = () => {};
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}

describe("useApplyLens", () => {
let mockApplyLens: jest.Mock;
let mockRemoveLens: jest.Mock;
Expand Down Expand Up @@ -387,14 +397,9 @@ describe("useApplyLens", () => {
});

describe("Cancellation handling", () => {
it("should remove lens if component unmounts during application", async () => {
let resolveApply: any;
mockApplyLens.mockImplementation(
() =>
new Promise((resolve) => {
resolveApply = resolve;
}),
);
it("should not remove lens again if component unmounts during application", async () => {
const apply = deferred<boolean>();
mockApplyLens.mockReturnValue(apply.promise);

const { unmount } = renderHook(() => useApplyLens("lens-123", "group-456"));

Expand All @@ -403,11 +408,39 @@ describe("useApplyLens", () => {
});

unmount();
resolveApply(true);
await act(async () => {
apply.resolve(true);
await apply.promise;
});

expect(mockRemoveLens).toHaveBeenCalledTimes(1);
});

it("should not remove the current lens when a superseded apply resolves late", async () => {
const firstApply = deferred<boolean>();
mockApplyLens.mockImplementationOnce(() => firstApply.promise).mockResolvedValueOnce(true);

const { rerender } = renderHook(({ lensId }) => useApplyLens(lensId, "group-1"), {
initialProps: { lensId: "lens-1" },
});

await waitFor(() => {
expect(mockApplyLens).toHaveBeenCalledWith("lens-1", "group-1", undefined, undefined);
});

rerender({ lensId: "lens-2" });

await waitFor(() => {
expect(mockRemoveLens).toHaveBeenCalledTimes(2); // Once after apply, once on unmount
expect(mockApplyLens).toHaveBeenCalledWith("lens-2", "group-1", undefined, undefined);
});
expect(mockRemoveLens).toHaveBeenCalledTimes(1);

await act(async () => {
firstApply.resolve(true);
await firstApply.promise;
});

expect(mockRemoveLens).toHaveBeenCalledTimes(1);
});
});

Expand Down
5 changes: 1 addition & 4 deletions src/useApplyLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ export function useApplyLens(
(async () => {
try {
await applyLens(lensId, lensGroupId, safeLaunchData, guardRef.current);
if (cancelled) {
await removeLens();
return;
}
if (cancelled) return;
log.info("apply_success", {
lensId,
groupId: lensGroupId,
Expand Down
Loading