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.1.0",
"version": "0.2.0",
"description": "React Camera Kit for web applications",
"type": "module",
"main": "./dist/cjs/index.js",
Expand Down
56 changes: 56 additions & 0 deletions src/CameraKitProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,62 @@ describe("CameraKitProvider", () => {
expect(result.current.lenses).toHaveLength(2);
});

it("should clear lens cache on re-bootstrap", async () => {
const mockLens = { id: "lens-1", groupId: "group-1" } as Lens;
(mockKit.lensRepository.loadLens as any).mockResolvedValue(mockLens);

const mockKit2 = {
destroy: jest.fn().mockResolvedValue(undefined),
createSession: jest.fn().mockResolvedValue(mockSession),
lensRepository: {
loadLens: jest.fn().mockResolvedValue(mockLens),
loadLensGroups: jest.fn(),
},
} as any;

mockBootstrapCameraKit.mockReset().mockResolvedValueOnce(mockKit).mockResolvedValueOnce(mockKit2);

let stabilityKey = "key-1";
const wrapper = ({ children }: { children: React.ReactNode }) => (
<CameraKitProvider apiToken="test-token" stabilityKey={stabilityKey}>
{children}
</CameraKitProvider>
);

const { result, rerender } = renderHook(() => useCameraKit(), { wrapper });

await waitFor(() => {
expect(result.current.sdkStatus).toBe("ready");
});

// Fetch a lens — populates the cache
await act(async () => {
await result.current.fetchLens("lens-1", "group-1");
});

expect(mockKit.lensRepository.loadLens).toHaveBeenCalledTimes(1);
expect(result.current.lenses).toHaveLength(1);

// Trigger re-bootstrap by changing stabilityKey
stabilityKey = "key-2";
rerender();

await waitFor(() => {
expect(result.current.sdkStatus).toBe("ready");
expect(mockBootstrapCameraKit).toHaveBeenCalledTimes(2);
});

// Lenses state should have been cleared
expect(result.current.lenses).toHaveLength(0);

// Fetch the same lens again — should NOT hit cache, should call new kit's loadLens
await act(async () => {
await result.current.fetchLens("lens-1", "group-1");
});

expect(mockKit2.lensRepository.loadLens).toHaveBeenCalledWith("lens-1", "group-1");
});

it("should apply lens", async () => {
const mockLens = { id: "lens-1", groupId: "group-1" } as Lens;
(mockKit.lensRepository.loadLens as any).mockResolvedValue(mockLens);
Expand Down
4 changes: 4 additions & 0 deletions src/CameraKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ export const CameraKitProvider: React.FC<CameraKitProviderProps> = ({
const abortController = new AbortController();
const emit = eventFactoryRef.current?.();

// Clear stale lens cache — the old kit's lensRepository is no longer valid.
lensCache.current.clear();
setLenses([]);

setCameraKitState({ status: "initializing" });
emit?.({ kind: "bootstrap-attempt" });
log.info("bootstrap_attempt");
Expand Down
Loading