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
11 changes: 6 additions & 5 deletions app/src/tests/fixtures/frames/populationMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RootState } from '@/store';
import { FlowComponentProps } from '@/types/flow';
import { Geography } from '@/types/ingredients/Geography';
import { Household } from '@/types/ingredients/Household';
import { UserGeographicAssociation } from '@/types/userIngredientAssociations';
import { UserGeographyPopulation } from '@/types/ingredients/UserPopulation';

// Test IDs and labels
export const TEST_USER_ID = 'test-user-123';
Expand Down Expand Up @@ -220,12 +220,13 @@ export const mockRootState: Partial<RootState> = {
};

// Mock geographic association
export const mockGeographicAssociation: UserGeographicAssociation = {
export const mockGeographicAssociation: UserGeographyPopulation = {
type: 'geography',
id: `${TEST_USER_ID}-${Date.now()}`,
userId: TEST_USER_ID,
countryCode: TEST_COUNTRIES.US,
geographyType: 'national',
geographyIdentifier: TEST_COUNTRIES.US,
countryId: TEST_COUNTRIES.US,
scope: 'national',
geographyId: TEST_COUNTRIES.US,
label: 'United States',
createdAt: new Date().toISOString(),
};
Expand Down
22 changes: 11 additions & 11 deletions app/src/tests/fixtures/hooks/hooksMocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { QueryClient } from '@tanstack/react-query';
import { vi } from 'vitest';
import { UserHouseholdPopulation } from '@/types/ingredients/UserPopulation';
import {
UserGeographyPopulation,
UserHouseholdPopulation,
} from '@/types/ingredients/UserPopulation';
import { HouseholdMetadata } from '@/types/metadata/householdMetadata';
import { HouseholdCreationPayload } from '@/types/payloads';
import { UserGeographicAssociation } from '@/types/userIngredientAssociations';

// ============= TEST CONSTANTS =============

Expand Down Expand Up @@ -140,25 +142,23 @@ export const mockUserHouseholdPopulationList: UserHouseholdPopulation[] = [
},
];

export const mockUserGeographicAssociation: UserGeographicAssociation = {
export const mockUserGeographicAssociation: UserGeographyPopulation = {
type: 'geography',
id: TEST_IDS.GEOGRAPHY_ID,
userId: TEST_IDS.USER_ID,
countryCode: GEO_CONSTANTS.COUNTRY_US,
geographyType: GEO_CONSTANTS.TYPE_SUBNATIONAL,
geographyIdentifier: `${GEO_CONSTANTS.COUNTRY_US}-${GEO_CONSTANTS.REGION_CA}`,
regionCode: GEO_CONSTANTS.REGION_CA,
regionType: GEO_CONSTANTS.REGION_TYPE_STATE,
countryId: GEO_CONSTANTS.COUNTRY_US,
scope: GEO_CONSTANTS.TYPE_SUBNATIONAL,
geographyId: GEO_CONSTANTS.REGION_CA,
label: TEST_LABELS.GEOGRAPHY,
createdAt: TEST_IDS.TIMESTAMP,
};

export const mockUserGeographicAssociationList: UserGeographicAssociation[] = [
export const mockUserGeographicAssociationList: UserGeographyPopulation[] = [
mockUserGeographicAssociation,
{
...mockUserGeographicAssociation,
id: TEST_IDS.GEOGRAPHY_ID_2,
geographyIdentifier: `${GEO_CONSTANTS.COUNTRY_US}-${GEO_CONSTANTS.REGION_NY}`,
regionCode: GEO_CONSTANTS.REGION_NY,
geographyId: GEO_CONSTANTS.REGION_NY,
label: TEST_LABELS.GEOGRAPHY_2,
},
];
Expand Down
26 changes: 14 additions & 12 deletions app/src/tests/fixtures/pages/populationsMocks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { vi } from 'vitest';
import { UserHouseholdPopulation } from '@/types/ingredients/UserPopulation';
import {
UserGeographyPopulation,
UserHouseholdPopulation,
} from '@/types/ingredients/UserPopulation';
import { HouseholdMetadata } from '@/types/metadata/householdMetadata';
import { UserGeographicAssociation } from '@/types/userIngredientAssociations';

// ============= TEST CONSTANTS =============

Expand Down Expand Up @@ -209,24 +211,24 @@ export const mockHouseholdAssociation2: UserHouseholdPopulation = {
};

// Mock geographic associations
export const mockGeographicAssociation1: UserGeographicAssociation = {
export const mockGeographicAssociation1: UserGeographyPopulation = {
type: 'geography',
id: POPULATION_TEST_IDS.GEOGRAPHIC_ID_1,
userId: POPULATION_TEST_IDS.USER_ID,
countryCode: POPULATION_GEO.COUNTRY_US,
geographyType: POPULATION_GEO.TYPE_SUBNATIONAL,
geographyIdentifier: `${POPULATION_GEO.COUNTRY_US}-${POPULATION_GEO.STATE_CA}`,
regionCode: POPULATION_GEO.STATE_CA,
regionType: POPULATION_GEO.REGION_TYPE_STATE,
countryId: POPULATION_GEO.COUNTRY_US,
scope: POPULATION_GEO.TYPE_SUBNATIONAL,
geographyId: POPULATION_GEO.STATE_CA,
label: POPULATION_LABELS.GEOGRAPHIC_1,
createdAt: POPULATION_TEST_IDS.TIMESTAMP_1,
};

export const mockGeographicAssociation2: UserGeographicAssociation = {
export const mockGeographicAssociation2: UserGeographyPopulation = {
type: 'geography',
id: POPULATION_TEST_IDS.GEOGRAPHIC_ID_2,
userId: POPULATION_TEST_IDS.USER_ID,
countryCode: POPULATION_GEO.COUNTRY_UK,
geographyType: POPULATION_GEO.TYPE_NATIONAL,
geographyIdentifier: POPULATION_GEO.COUNTRY_UK,
countryId: POPULATION_GEO.COUNTRY_UK,
scope: POPULATION_GEO.TYPE_NATIONAL,
geographyId: POPULATION_GEO.COUNTRY_UK,
label: POPULATION_LABELS.GEOGRAPHIC_2,
createdAt: POPULATION_TEST_IDS.TIMESTAMP_2,
};
Expand Down
11 changes: 2 additions & 9 deletions app/src/tests/fixtures/utils/populationOpsMocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { vi } from 'vitest';
import { countryIds } from '@/libs/countries';
import {
UserGeographyPopulation,
UserHouseholdPopulation,
Expand Down Expand Up @@ -150,14 +151,6 @@ export const mockUserGeographyPopInvalid: UserGeographyPopulation = {
userId: POPULATION_IDS.USER_1,
};

export const mockUserGeographyPopNoCountry: UserGeographyPopulation = {
type: 'geography',
geographyId: POPULATION_IDS.GEOGRAPHY_1,
countryId: '',
scope: POPULATION_SCOPES.NATIONAL as any,
userId: POPULATION_IDS.USER_1,
};

// ============= EXPECTED RESULTS =============

// Expected API payloads
Expand Down Expand Up @@ -217,7 +210,7 @@ export const createUserHouseholdPop = (
// Helper to create a user geography population
export const createUserGeographyPop = (
geographyId: string,
countryId: string,
countryId: (typeof countryIds)[number],
scope: 'national' | 'subnational',
userId: string,
label?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,17 @@ describe('GeographicConfirmationFrame', () => {
parameters: {},
entities: {},
variableModules: {},
economyOptions: { region: [], time_period: [], datasets: [] },
economyOptions: {
region: [
{ name: 'us', label: 'United States' },
{ name: 'state/ca', label: 'California' },
{ name: 'state/ny', label: 'New York' },
{ name: 'uk', label: 'United Kingdom' },
{ name: 'constituency/london', label: 'London' },
],
time_period: [],
datasets: [],
},
currentLawId: 0,
basicInputs: [],
modelledPolicies: { core: {}, filtered: {} },
Expand Down Expand Up @@ -203,10 +213,10 @@ describe('GeographicConfirmationFrame', () => {
expect(mockCreateGeographicAssociation).toHaveBeenCalledWith(
expect.objectContaining({
userId: TEST_USER_ID,
countryCode: TEST_COUNTRIES.US,
geographyType: 'national',
geographyIdentifier: TEST_COUNTRIES.US,
label: 'United States',
countryId: TEST_COUNTRIES.US,
scope: 'national',
geographyId: TEST_COUNTRIES.US,
label: 'Test National',
})
);
});
Expand All @@ -233,11 +243,10 @@ describe('GeographicConfirmationFrame', () => {
expect(mockCreateGeographicAssociation).toHaveBeenCalledWith(
expect.objectContaining({
userId: TEST_USER_ID,
geographyType: 'subnational',
geographyIdentifier: `${TEST_COUNTRIES.US}-ca`,
regionCode: 'ca',
regionType: 'state',
label: 'California',
countryId: TEST_COUNTRIES.US,
scope: 'subnational',
geographyId: 'ca',
label: 'Test State',
})
);
});
Expand Down
51 changes: 21 additions & 30 deletions app/src/tests/unit/hooks/useUserGeographic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('useUserGeographic hooks', () => {
(SessionStorageGeographicStore as any)();

// Set default mock implementations
mockStore.create.mockResolvedValue(mockUserGeographicAssociation);
mockStore.create.mockImplementation((input: any) => Promise.resolve(input));
mockStore.findByUser.mockResolvedValue(mockUserGeographicAssociationList);
mockStore.findById.mockResolvedValue(mockUserGeographicAssociation);
});
Expand Down Expand Up @@ -230,9 +230,9 @@ describe('useUserGeographic hooks', () => {
const newAssociation = {
id: TEST_IDS.GEOGRAPHY_ID,
userId: TEST_IDS.USER_ID,
countryCode: GEO_CONSTANTS.COUNTRY_US,
geographyType: GEO_CONSTANTS.TYPE_NATIONAL,
geographyIdentifier: GEO_CONSTANTS.COUNTRY_US,
countryId: GEO_CONSTANTS.COUNTRY_US,
scope: GEO_CONSTANTS.TYPE_NATIONAL,
geographyId: GEO_CONSTANTS.COUNTRY_US,
label: TEST_LABELS.GEOGRAPHY,
} as const;

Expand All @@ -248,36 +248,30 @@ describe('useUserGeographic hooks', () => {

// Verify store was called
const mockStore = (SessionStorageGeographicStore as any)();
expect(mockStore.create).toHaveBeenCalledWith(newAssociation);
expect(mockStore.create).toHaveBeenCalledWith({ ...newAssociation, type: 'geography' });

// Verify cache invalidation
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({
queryKey: QUERY_KEY_PATTERNS.GEO_ASSOCIATION_BY_USER(TEST_IDS.USER_ID),
});
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({
queryKey: QUERY_KEY_PATTERNS.GEO_ASSOCIATION_BY_GEOGRAPHY(
`${GEO_CONSTANTS.COUNTRY_US}-${GEO_CONSTANTS.REGION_CA}`
),
queryKey: QUERY_KEY_PATTERNS.GEO_ASSOCIATION_BY_GEOGRAPHY(GEO_CONSTANTS.COUNTRY_US),
});

// Verify cache update
expect(queryClient.setQueryData).toHaveBeenCalledWith(
QUERY_KEY_PATTERNS.GEO_ASSOCIATION_SPECIFIC(
TEST_IDS.USER_ID,
`${GEO_CONSTANTS.COUNTRY_US}-${GEO_CONSTANTS.REGION_CA}`
),
mockUserGeographicAssociation
QUERY_KEY_PATTERNS.GEO_ASSOCIATION_SPECIFIC(TEST_IDS.USER_ID, GEO_CONSTANTS.COUNTRY_US),
{ ...newAssociation, type: 'geography' }
);
});

test('given subnational association when created then updates cache with full identifier', async () => {
// Given
const subnationalAssociation = {
...mockUserGeographicAssociation,
geographyType: GEO_CONSTANTS.TYPE_SUBNATIONAL,
geographyIdentifier: `${GEO_CONSTANTS.COUNTRY_US}-${GEO_CONSTANTS.REGION_CA}`,
regionCode: GEO_CONSTANTS.REGION_CA,
regionType: GEO_CONSTANTS.REGION_TYPE_STATE,
scope: GEO_CONSTANTS.TYPE_SUBNATIONAL,
geographyId: GEO_CONSTANTS.REGION_CA,
countryId: GEO_CONSTANTS.COUNTRY_US,
} as const;

const mockStore = (SessionStorageGeographicStore as any)();
Expand All @@ -289,10 +283,7 @@ describe('useUserGeographic hooks', () => {

// Then
expect(queryClient.setQueryData).toHaveBeenCalledWith(
QUERY_KEY_PATTERNS.GEO_ASSOCIATION_SPECIFIC(
TEST_IDS.USER_ID,
`${GEO_CONSTANTS.COUNTRY_US}-${GEO_CONSTANTS.REGION_CA}`
),
QUERY_KEY_PATTERNS.GEO_ASSOCIATION_SPECIFIC(TEST_IDS.USER_ID, GEO_CONSTANTS.REGION_CA),
subnationalAssociation
);
});
Expand All @@ -309,9 +300,9 @@ describe('useUserGeographic hooks', () => {
result.current.mutateAsync({
id: TEST_IDS.GEOGRAPHY_ID,
userId: TEST_IDS.USER_ID,
countryCode: GEO_CONSTANTS.COUNTRY_US,
geographyType: GEO_CONSTANTS.TYPE_NATIONAL,
geographyIdentifier: GEO_CONSTANTS.COUNTRY_US,
countryId: GEO_CONSTANTS.COUNTRY_US,
scope: GEO_CONSTANTS.TYPE_NATIONAL,
geographyId: GEO_CONSTANTS.COUNTRY_US,
label: TEST_LABELS.GEOGRAPHY,
})
).rejects.toThrow('Creation failed');
Expand All @@ -327,18 +318,18 @@ describe('useUserGeographic hooks', () => {
const association1 = {
id: TEST_IDS.GEOGRAPHY_ID,
userId: TEST_IDS.USER_ID,
countryCode: GEO_CONSTANTS.COUNTRY_US,
geographyType: GEO_CONSTANTS.TYPE_NATIONAL,
geographyIdentifier: GEO_CONSTANTS.COUNTRY_US,
countryId: GEO_CONSTANTS.COUNTRY_US,
scope: GEO_CONSTANTS.TYPE_NATIONAL,
geographyId: GEO_CONSTANTS.COUNTRY_US,
label: TEST_LABELS.GEOGRAPHY,
} as const;

const association2 = {
id: TEST_IDS.GEOGRAPHY_ID_2,
userId: TEST_IDS.USER_ID,
countryCode: GEO_CONSTANTS.COUNTRY_UK,
geographyType: GEO_CONSTANTS.TYPE_NATIONAL,
geographyIdentifier: GEO_CONSTANTS.COUNTRY_UK,
countryId: GEO_CONSTANTS.COUNTRY_UK,
scope: GEO_CONSTANTS.TYPE_NATIONAL,
geographyId: GEO_CONSTANTS.COUNTRY_UK,
label: TEST_LABELS.GEOGRAPHY_2,
} as const;

Expand Down
28 changes: 20 additions & 8 deletions app/src/tests/unit/pages/Populations.page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('PopulationsPage', () => {
vi.clearAllMocks();
consoleMocks = setupMockConsole();

// Create a mock store with flow reducer
// Create a mock store with flow and metadata reducers
store = configureStore({
reducer: {
flow: (state = { current: null }, action: any) => {
Expand All @@ -53,6 +53,7 @@ describe('PopulationsPage', () => {
}
return state;
},
metadata: (state = { economyOptions: { region: [] } }, _action: any) => state,
},
});

Expand Down Expand Up @@ -152,8 +153,8 @@ describe('PopulationsPage', () => {
// Then
expect(screen.getByText(POPULATION_DETAILS.SUBNATIONAL)).toBeInTheDocument();
expect(screen.getByText(POPULATION_DETAILS.NATIONAL)).toBeInTheDocument();
expect(screen.getByText(POPULATION_GEO.COUNTRY_US.toUpperCase())).toBeInTheDocument();
expect(screen.getByText(POPULATION_GEO.COUNTRY_UK.toUpperCase())).toBeInTheDocument();
expect(screen.getByText('United States')).toBeInTheDocument();
expect(screen.getByText('United Kingdom')).toBeInTheDocument();
});

test('given subnational geography then displays region details', () => {
Expand All @@ -171,8 +172,17 @@ describe('PopulationsPage', () => {
renderPage();

// Then
const date1 = new Date(POPULATION_TEST_IDS.TIMESTAMP_1).toLocaleDateString();
const date2 = new Date(POPULATION_TEST_IDS.TIMESTAMP_2).toLocaleDateString();
// Format dates as 'short-month-day-year' format: "Jan 15, 2024"
const date1 = new Date(POPULATION_TEST_IDS.TIMESTAMP_1).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
});
const date2 = new Date(POPULATION_TEST_IDS.TIMESTAMP_2).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
});

// Use getAllByText since dates might appear multiple times
const date1Elements = screen.getAllByText(date1);
Expand Down Expand Up @@ -375,7 +385,7 @@ describe('PopulationsPage', () => {
).toBeInTheDocument();
});

test('given household without created date then displays just now', () => {
test('given household without created date then displays empty date', () => {
// Given
const dataWithoutDate = [
{
Expand All @@ -397,8 +407,10 @@ describe('PopulationsPage', () => {
// When
renderPage();

// Then
expect(screen.getByText('Just now')).toBeInTheDocument();
// Then - Check that the household data is displayed (but without checking for specific date text)
expect(
screen.getByText(mockUserHouseholdsData[0].association.label || 'Household')
).toBeInTheDocument();
});

test('given household with no people then displays zero count', () => {
Expand Down
Loading
Loading