LGTM
This commit is contained in:
parent
9248e26af2
commit
f2a2651b8a
95 changed files with 3993 additions and 1471 deletions
134
frontend/src/hooks/useHexagonSelection.test.ts
Normal file
134
frontend/src/hooks/useHexagonSelection.test.ts
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { useHexagonSelection } from './useHexagonSelection';
|
||||
import type { FeatureMeta, HexagonStatsResponse, PostcodeGeometry } from '../types';
|
||||
|
||||
vi.mock('../lib/pocketbase', () => ({
|
||||
default: { authStore: { isValid: false, token: '' } },
|
||||
}));
|
||||
|
||||
vi.mock('../lib/analytics', () => ({
|
||||
trackEvent: vi.fn(),
|
||||
}));
|
||||
|
||||
const postcodeGeometry: PostcodeGeometry = {
|
||||
type: 'Polygon',
|
||||
coordinates: [
|
||||
[
|
||||
[-0.12, 51.5],
|
||||
[-0.11, 51.5],
|
||||
[-0.11, 51.51],
|
||||
[-0.12, 51.51],
|
||||
[-0.12, 51.5],
|
||||
],
|
||||
],
|
||||
};
|
||||
|
||||
function stats(count: number): HexagonStatsResponse {
|
||||
return {
|
||||
count,
|
||||
numeric_features: [],
|
||||
enum_features: [],
|
||||
central_postcode: 'SW1A 1AA',
|
||||
};
|
||||
}
|
||||
|
||||
function jsonResponse(body: unknown): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
describe('useHexagonSelection', () => {
|
||||
const requests: string[] = [];
|
||||
const features: FeatureMeta[] = [{ name: 'Price', type: 'numeric', min: 0, max: 100 }];
|
||||
|
||||
beforeEach(() => {
|
||||
requests.length = 0;
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn((input: string | URL | Request) => {
|
||||
const url = new URL(String(input), 'http://localhost');
|
||||
requests.push(`${url.pathname}${url.search}`);
|
||||
|
||||
if (url.pathname === '/api/postcode-stats') {
|
||||
return Promise.resolve(jsonResponse(stats(url.searchParams.has('filters') ? 0 : 4)));
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/hexagon-stats') {
|
||||
return Promise.resolve(jsonResponse(stats(12)));
|
||||
}
|
||||
|
||||
return Promise.resolve(new Response(null, { status: 404 }));
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('keeps a postcode search selected when filters exclude its properties', async () => {
|
||||
const { result } = renderHook(() =>
|
||||
useHexagonSelection({
|
||||
filters: { Price: [0, 50] },
|
||||
features,
|
||||
hexagonData: [],
|
||||
resolution: 9,
|
||||
usePostcodeView: true,
|
||||
travelTimeEntries: [],
|
||||
})
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.handleLocationSearch('SW1A 1AA', postcodeGeometry, 51.505, -0.115);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.selectedHexagon).toEqual({
|
||||
id: 'SW1A 1AA',
|
||||
type: 'postcode',
|
||||
resolution: 9,
|
||||
});
|
||||
});
|
||||
|
||||
expect(result.current.selectedPostcodeGeometry).toBe(postcodeGeometry);
|
||||
expect(result.current.areaStats?.count).toBe(0);
|
||||
expect(result.current.unfilteredAreaCount).toBe(4);
|
||||
expect(requests.some((url) => url.startsWith('/api/hexagon-stats'))).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps a postcode search selected when stats are based on all properties', async () => {
|
||||
const { result } = renderHook(() =>
|
||||
useHexagonSelection({
|
||||
filters: { Price: [0, 50] },
|
||||
features,
|
||||
hexagonData: [],
|
||||
resolution: 9,
|
||||
usePostcodeView: true,
|
||||
travelTimeEntries: [],
|
||||
})
|
||||
);
|
||||
|
||||
act(() => {
|
||||
result.current.setAreaStatsUseFilters(false);
|
||||
});
|
||||
act(() => {
|
||||
result.current.handleLocationSearch('SW1A 1AA', postcodeGeometry, 51.505, -0.115);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.selectedHexagon).toEqual({
|
||||
id: 'SW1A 1AA',
|
||||
type: 'postcode',
|
||||
resolution: 9,
|
||||
});
|
||||
});
|
||||
|
||||
expect(result.current.areaStats?.count).toBe(4);
|
||||
expect(result.current.unfilteredAreaCount).toBeNull();
|
||||
expect(requests.some((url) => url.startsWith('/api/hexagon-stats'))).toBe(false);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue