Skip to content

Commit

Permalink
Mijn-9880-Bug - Add features check + test, (#1667)
Browse files Browse the repository at this point in the history
* MIJN-9880-Bug - Hot fix (#1666)

* Fix pageChangeHook

* Don’t show tips when there are no filteredfeatures

* Add test for adoptable trash container

* Change name

* Add mocked responses to var, fix expectation value

* Update src/server/services/home.ts

Lijkt me een verbetering.

Co-authored-by: Terry van Walen <[email protected]>

* Rename file, add tests

* Clarify testing data

* Change number in fake address

* Change error messages

---------

Co-authored-by: Terry van Walen <[email protected]>
  • Loading branch information
timvanoostrom and TerryvanWalen authored Dec 18, 2024
1 parent 9c91f26 commit c6ef033
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 47 deletions.
8 changes: 6 additions & 2 deletions src/client/hooks/usePageChange.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ReactNode } from 'react';

import { renderHook } from '@testing-library/react';
import * as rrd from 'react-router-dom';
import { afterAll, afterEach, describe, expect, it, test, vi } from 'vitest';
Expand All @@ -6,7 +8,6 @@ import { trackPageViewWithCustomDimension } from './analytics.hook';
import { usePageChange } from './usePageChange';
import type { TrackingConfig } from '../config/routes';
import { NOT_FOUND_TITLE } from '../config/thema';
import { ReactNode } from 'react';

const mocks = vi.hoisted(() => {
return {
Expand All @@ -24,8 +25,11 @@ vi.mock('react-router-dom', async (requireActual) => {
__setPathname: (name: string) => {
mocks.pathname = name;
},
useLocation: () => {
return { pathname: mocks.pathname };
},
useHistory: () => {
return { action: 'PUSH', location: { pathname: mocks.pathname } };
return { action: 'PUSH' };
},
};
});
Expand Down
4 changes: 2 additions & 2 deletions src/client/hooks/usePageChange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef } from 'react';

import { matchPath, useHistory } from 'react-router-dom';
import { matchPath, useHistory, useLocation } from 'react-router-dom';

import { trackPageViewWithCustomDimension } from './analytics.hook';
import { useProfileTypeValue } from './useProfileType';
Expand All @@ -25,7 +25,7 @@ const sortedPageTitleRoutes = Object.keys(DocumentTitles).sort((a, b) => {

export function usePageChange(isAuthenticated: boolean) {
const history = useHistory();
const location = history.location;
const location = useLocation();
const termReplace = useTermReplacement();
const profileType = useProfileTypeValue();
const userCity = useUserCity();
Expand Down
151 changes: 151 additions & 0 deletions src/server/services/adoptable-trash-containers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,155 @@
import { describe, it, expect, vi, Mock } from 'vitest';

import { forTesting } from './adoptable-trash-containers';
import { fetchAdoptableTrashContainers } from './adoptable-trash-containers';
import { fetchBRP } from './brp';
import { fetchDataset } from './buurt/buurt';
import { fetchMyLocation } from './my-locations';
import {
generateRandomPoints,
getAuthProfileAndToken,
} from '../../testing/utils';
import {
DEFAULT_LAT,
DEFAULT_LNG,
} from '../../universal/config/myarea-datasets';
import { apiSuccessResult, apiErrorResult } from '../../universal/helpers/api';

vi.mock('./brp', () => ({
fetchBRP: vi.fn(),
}));

vi.mock('./my-locations', () => ({
fetchMyLocation: vi.fn(),
}));

vi.mock('./buurt/buurt', () => ({
fetchDataset: vi.fn(),
}));

describe('fetchAdoptableTrashContainers', () => {
const requestID = 'test-request-id';
const authProfileAndToken = getAuthProfileAndToken();
const defaultLatLng = { lat: DEFAULT_LAT, lng: DEFAULT_LNG };
const locationApiResponse = apiSuccessResult([{ latlng: defaultLatLng }]);
const brpApiResponse = apiSuccessResult({
persoon: { geboortedatum: '2000-01-01' },
});

it('should return tips if all fetches are successful and age is above LATE_TEEN_AGE', async () => {
(fetchBRP as Mock).mockResolvedValue(brpApiResponse);
(fetchMyLocation as Mock).mockResolvedValue(locationApiResponse);

const [coord] = generateRandomPoints(
{ lat: DEFAULT_LAT, lng: DEFAULT_LNG },
90, // 90 meters
1
);

(fetchDataset as Mock).mockResolvedValue(
apiSuccessResult({
features: [
{
geometry: { coordinates: [coord.lng, coord.lat] },
properties: {
datasetId: 'afvalcontainers',
geadopteerd_ind: 'Nee',
},
},
],
})
);

const result = await fetchAdoptableTrashContainers(
requestID,
authProfileAndToken
);
expect(result.status).toBe('OK');
expect(result.content?.tips).toHaveLength(1);
});

it('should return an error if fetching BRP data fails', async () => {
(fetchBRP as Mock).mockResolvedValue(
apiErrorResult('Error fetching BRP data', null)
);

const result = await fetchAdoptableTrashContainers(
requestID,
authProfileAndToken
);
expect(result.status).toBe('DEPENDENCY_ERROR');
});

it('should return an error if fetching location fails', async () => {
(fetchBRP as Mock).mockResolvedValue(brpApiResponse);
(fetchMyLocation as Mock).mockResolvedValue(
apiErrorResult('Error fetching BAG location', null)
);

const result = await fetchAdoptableTrashContainers(
requestID,
authProfileAndToken
);
expect(result.status).toBe('DEPENDENCY_ERROR');
});

it('should return an error if fetching dataset fails', async () => {
(fetchBRP as Mock).mockResolvedValue(brpApiResponse);
(fetchMyLocation as Mock).mockResolvedValue(locationApiResponse);
(fetchDataset as Mock).mockResolvedValue(
apiErrorResult('Error fetching Map locations dataset', null)
);

const result = await fetchAdoptableTrashContainers(
requestID,
authProfileAndToken
);
expect(result.status).toBe('DEPENDENCY_ERROR');
});

it('should return no tips if age is less than LATE_TEEN_AGE', async () => {
(fetchBRP as Mock).mockResolvedValue(
apiSuccessResult({ persoon: { geboortedatum: '2010-01-01' } })
);

const result = await fetchAdoptableTrashContainers(
requestID,
authProfileAndToken
);
expect(result.status).toBe('OK');
expect(result.content?.tips).toHaveLength(0);
});

it('should not return tips if there are no adoptable trashcontainers found within the given radius', async () => {
(fetchBRP as Mock).mockResolvedValue(brpApiResponse);
(fetchMyLocation as Mock).mockResolvedValue(locationApiResponse);

// A coord outside of the radius from the Home location returned by fetchMyLocation
const COORD_LAT_OFFSET = 5;
const coordinates = [DEFAULT_LNG, DEFAULT_LAT + COORD_LAT_OFFSET];

(fetchDataset as Mock).mockResolvedValue(
apiSuccessResult({
features: [
{
geometry: { coordinates },
properties: {
datasetId: 'afvalcontainers',
geadopteerd_ind: 'Nee',
},
},
],
})
);

const result = await fetchAdoptableTrashContainers(
requestID,
authProfileAndToken
);
expect(result.status).toBe('OK');
expect(result.content?.tips).toHaveLength(0);
});
});

describe('determineDescriptionText Tests', () => {
test('Returns adults text when adult', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/server/services/adoptable-trash-containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
filterFeaturesinRadius,
getBboxFromFeatures,
} from './buurt/helpers';
import { fetchMyLocation } from './home';
import { fetchMyLocation } from './my-locations';
import { AppRoutes } from '../../universal/config/routes';
import { Themas } from '../../universal/config/thema';
import {
Expand Down Expand Up @@ -107,7 +107,7 @@ export async function fetchAdoptableTrashContainers(
);

return apiSuccessResult({
tips: [buildNotification(age, bbox)],
tips: filteredFeatures.length ? [buildNotification(age, bbox)] : [],
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/services/afval/afval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
apiSuccessResult,
} from '../../../universal/helpers/api';
import { AuthProfileAndToken } from '../../auth/auth-types';
import { fetchMyLocation } from '../home';
import { fetchMyLocation } from '../my-locations';
import { fetchAfvalpuntenByLatLng } from './afvalpunten';
import { fetchAfvalwijzer } from './afvalwijzer';

Expand Down
2 changes: 1 addition & 1 deletion src/server/services/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { fetchBRP } from './brp';
import { fetchCMSCONTENT } from './cms-content';
import { fetchMaintenanceNotificationsActual } from './cms-maintenance-notifications';
import { fetchHLI } from './hli/hli';
import { fetchMyLocation } from './home';
import { fetchMyLocation } from './my-locations';
import { fetchHorecaVergunningen } from './horeca';
import { fetchAllKlachten } from './klachten/klachten';
import { fetchKrefia } from './krefia';
Expand Down
Loading

0 comments on commit c6ef033

Please sign in to comment.