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
101 changes: 83 additions & 18 deletions src/__tests__/hooks/useLocationClusters.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useLocationClusters } from "@/hooks/useLocationClusters";
import * as locationsApi from "@/lib/api/locations";
import * as searchApi from "@/lib/api/search";
import type { ReactNode } from "react";
import type { BoundingBox, ClusterResponse } from "@/types/map";

// Mock the locations API
jest.mock("@/lib/api/locations");
const mockGetClusters = locationsApi.getClusters as jest.MockedFunction<
typeof locationsApi.getClusters
// Mock the search API
jest.mock("@/lib/api/search");
const mockSearchClusters = searchApi.searchClusters as jest.MockedFunction<
typeof searchApi.searchClusters
>;

// Mock the mapbox config so clusterZoomThreshold is stable
Expand Down Expand Up @@ -45,7 +45,6 @@ const mockClusterResponse: ClusterResponse = {
latitude: 37.5,
longitude: -122.5,
count: 5,
videoIds: ["v1", "v2"],
},
{
id: "cluster-2",
Expand Down Expand Up @@ -74,7 +73,7 @@ describe("useLocationClusters", () => {
{ wrapper }
);

expect(mockGetClusters).not.toHaveBeenCalled();
expect(mockSearchClusters).not.toHaveBeenCalled();
});

it("should not fetch when enabled is false", () => {
Expand All @@ -90,7 +89,7 @@ describe("useLocationClusters", () => {
{ wrapper }
);

expect(mockGetClusters).not.toHaveBeenCalled();
expect(mockSearchClusters).not.toHaveBeenCalled();
});

it("should not fetch when zoom >= clusterZoomThreshold (8)", () => {
Expand All @@ -105,7 +104,7 @@ describe("useLocationClusters", () => {
{ wrapper }
);

expect(mockGetClusters).not.toHaveBeenCalled();
expect(mockSearchClusters).not.toHaveBeenCalled();
});

it("should not fetch when zoom is above threshold", () => {
Expand All @@ -120,11 +119,11 @@ describe("useLocationClusters", () => {
{ wrapper }
);

expect(mockGetClusters).not.toHaveBeenCalled();
expect(mockSearchClusters).not.toHaveBeenCalled();
});

it("should fetch when bounds provided and zoom < 8", async () => {
mockGetClusters.mockResolvedValueOnce(mockClusterResponse);
mockSearchClusters.mockResolvedValueOnce(mockClusterResponse);
const wrapper = createWrapper();

const { result } = renderHook(
Expand All @@ -140,12 +139,12 @@ describe("useLocationClusters", () => {
expect(result.current.isSuccess).toBe(true);
});

expect(mockGetClusters).toHaveBeenCalledTimes(1);
expect(mockSearchClusters).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual(mockClusterResponse);
});

it("should include Math.floor(zoom) in the query key", async () => {
mockGetClusters.mockResolvedValue(mockClusterResponse);
mockSearchClusters.mockResolvedValue(mockClusterResponse);
const wrapper = createWrapper();

// First render with zoom 5.7
Expand All @@ -169,18 +168,18 @@ describe("useLocationClusters", () => {
rerender({ zoom: 5.3 });

// Still only one call because Math.floor(5.7) === Math.floor(5.3) === 5
expect(mockGetClusters).toHaveBeenCalledTimes(1);
expect(mockSearchClusters).toHaveBeenCalledTimes(1);

// Rerender with zoom 6.1 - different Math.floor, should refetch
rerender({ zoom: 6.1 });

await waitFor(() => {
expect(mockGetClusters).toHaveBeenCalledTimes(2);
expect(mockSearchClusters).toHaveBeenCalledTimes(2);
});
});

it("should pass correct params to getClusters", async () => {
mockGetClusters.mockResolvedValueOnce(mockClusterResponse);
it("should pass correct params to searchClusters", async () => {
mockSearchClusters.mockResolvedValueOnce(mockClusterResponse);
const wrapper = createWrapper();

const { result } = renderHook(
Expand All @@ -196,9 +195,75 @@ describe("useLocationClusters", () => {
expect(result.current.isSuccess).toBe(true);
});

expect(mockGetClusters).toHaveBeenCalledWith({
expect(mockSearchClusters).toHaveBeenCalledWith({
bbox: mockBounds,
zoom: 5, // Math.floor(5.9)
amendments: undefined,
participants: undefined,
});
});

it("should pass filter params to searchClusters", async () => {
mockSearchClusters.mockResolvedValueOnce(mockClusterResponse);
const wrapper = createWrapper();

const { result } = renderHook(
() =>
useLocationClusters({
bounds: mockBounds,
zoom: 5,
filters: {
amendments: ["FIRST", "FOURTH"],
participants: ["POLICE"],
},
}),
{ wrapper }
);

await waitFor(() => {
expect(result.current.isSuccess).toBe(true);
});

expect(mockSearchClusters).toHaveBeenCalledWith({
bbox: mockBounds,
zoom: 5,
amendments: ["FIRST", "FOURTH"],
participants: ["POLICE"],
});
});

it("should refetch when filters change", async () => {
mockSearchClusters.mockResolvedValue(mockClusterResponse);
const wrapper = createWrapper();

const { result, rerender } = renderHook(
({ filters }) =>
useLocationClusters({
bounds: mockBounds,
zoom: 5,
filters,
}),
{
wrapper,
initialProps: {
filters: { amendments: [] as string[], participants: [] as string[] },
},
}
);

await waitFor(() => {
expect(result.current.isSuccess).toBe(true);
});

expect(mockSearchClusters).toHaveBeenCalledTimes(1);

// Change filters - should trigger refetch
rerender({
filters: { amendments: ["FIRST"], participants: [] },
});

await waitFor(() => {
expect(mockSearchClusters).toHaveBeenCalledTimes(2);
});
});
});
82 changes: 0 additions & 82 deletions src/__tests__/lib/api/locations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ jest.mock("@/lib/api/client", () => ({
}));

import {
getClusters,
getLocation,
createLocation,
reverseGeocode,
Expand All @@ -18,87 +17,6 @@ describe("api/locations", () => {
jest.clearAllMocks();
});

describe("getClusters", () => {
const mockApiResponse = {
clusters: [
{
id: "cluster-1",
coordinates: { latitude: 40.7128, longitude: -74.006 },
count: 5,
sampleVideoIds: ["vid-1", "vid-2"],
},
{
id: "cluster-2",
coordinates: { latitude: 34.0522, longitude: -118.2437 },
count: 3,
sampleVideoIds: undefined,
},
],
totalLocations: 8,
zoom: 10,
};

it("transforms API response coordinates from nested to flat lat/lng", async () => {
(apiClient.get as jest.Mock).mockResolvedValue({ data: mockApiResponse });

const result = await getClusters({
bbox: [-74.1, 40.6, -73.9, 40.8],
zoom: 10,
});

expect(result.clusters[0].latitude).toBe(40.7128);
expect(result.clusters[0].longitude).toBe(-74.006);
expect(result.clusters[1].latitude).toBe(34.0522);
expect(result.clusters[1].longitude).toBe(-118.2437);
});

it("passes bbox as a comma-separated string", async () => {
(apiClient.get as jest.Mock).mockResolvedValue({ data: mockApiResponse });

await getClusters({ bbox: [-74.1, 40.6, -73.9, 40.8], zoom: 10 });

expect(apiClient.get).toHaveBeenCalledWith("/locations/cluster", {
params: {
bbox: "-74.1,40.6,-73.9,40.8",
zoom: 10,
},
});
});

it("passes zoom parameter", async () => {
(apiClient.get as jest.Mock).mockResolvedValue({ data: mockApiResponse });

await getClusters({ bbox: [-74.1, 40.6, -73.9, 40.8], zoom: 15 });

expect(apiClient.get).toHaveBeenCalledWith("/locations/cluster", {
params: expect.objectContaining({ zoom: 15 }),
});
});

it("maps sampleVideoIds to videoIds", async () => {
(apiClient.get as jest.Mock).mockResolvedValue({ data: mockApiResponse });

const result = await getClusters({
bbox: [-74.1, 40.6, -73.9, 40.8],
zoom: 10,
});

expect(result.clusters[0].videoIds).toEqual(["vid-1", "vid-2"]);
expect(result.clusters[1].videoIds).toBeUndefined();
});

it("returns zoom from the API response", async () => {
(apiClient.get as jest.Mock).mockResolvedValue({ data: mockApiResponse });

const result = await getClusters({
bbox: [-74.1, 40.6, -73.9, 40.8],
zoom: 10,
});

expect(result.zoom).toBe(10);
});
});

describe("getLocation", () => {
it("calls GET with correct path and returns response.data", async () => {
const locationData = {
Expand Down
Loading
Loading