Skip to content

Commit

Permalink
Rename file, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timvanoostrom committed Dec 17, 2024
1 parent 6ad47be commit 2a60ee4
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/server/services/adoptable-trash-containers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { forTesting } from './adoptable-trash-containers';
import { fetchAdoptableTrashContainers } from './adoptable-trash-containers';
import { fetchBRP } from './brp';
import { fetchDataset } from './buurt/buurt';
import { fetchMyLocation } from './home';
import { fetchMyLocation } from './my-locations';
import {
generateRandomPoints,
getAuthProfileAndToken,
Expand All @@ -19,7 +19,7 @@ vi.mock('./brp', () => ({
fetchBRP: vi.fn(),
}));

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

Expand Down
2 changes: 1 addition & 1 deletion 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
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
293 changes: 293 additions & 0 deletions src/server/services/my-locations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
import { describe, it, expect, vi, Mock } from 'vitest';

import { fetchBAG } from './bag';
import { fetchBRP } from './brp';
import { fetchKVK, getKvkAddresses } from './kvk';
import { fetchMyLocation, forTesting } from './my-locations';
import { getAuthProfileAndToken } from '../../testing/utils';
import { apiSuccessResult, apiErrorResult } from '../../universal/helpers/api';
import { Adres } from '../../universal/types';

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

vi.mock('./bag', () => ({
fetchBAG: vi.fn(),
}));

vi.mock('./kvk', async (importOriginal) => ({
...(await importOriginal()),
getKvkAddresses: vi.fn(),
fetchKVK: vi.fn(),
}));

const adres = { straatnaam: 'address1' } as Adres;
const adres2 = { straatnaam: 'address1' } as Adres;

describe('fetchPrivate', () => {
const requestID = 'test-request-id';
const authProfileAndToken = getAuthProfileAndToken();

it('should return private addresses if fetching BRP data is successful', async () => {
(fetchBRP as Mock).mockResolvedValueOnce(
apiSuccessResult({
mokum: true,
adres,
})
);

(fetchBAG as Mock).mockResolvedValueOnce(
apiSuccessResult({ latlng: { lat: 1, lng: 1 }, address: 'Een adres' })
);

const result = await forTesting.fetchPrivate(
requestID,
authProfileAndToken
);

expect(fetchBAG).toHaveBeenCalledWith(requestID, adres);

expect(result.status).toBe('OK');
expect(result.content).toHaveLength(1);
expect(result.content).toEqual([
{
address: 'Een adres',
latlng: {
lat: 1,
lng: 1,
},
profileType: 'private',
},
]);
});

it('should return default location if no BAG location is found', async () => {
(fetchBRP as Mock).mockResolvedValueOnce(
apiSuccessResult({
mokum: true,
adres,
})
);

(fetchBAG as Mock).mockResolvedValueOnce(
apiSuccessResult({ latlng: null, address: null })
);

const result = await forTesting.fetchPrivate(
requestID,
authProfileAndToken
);

expect(result).toStrictEqual({
content: [
{
address: null,
latlng: {
lat: 52.3676842478192,
lng: 4.90022569871861,
},
profileType: 'private',
},
],
status: 'OK',
});
});

it('should return a bare response if BRP data is not a Mokum address', async () => {
(fetchBRP as Mock).mockResolvedValueOnce(
apiSuccessResult({ mokum: false, adres: null })
);

const result = await forTesting.fetchPrivate(
requestID,
authProfileAndToken
);

expect(result.status).toBe('OK');
expect(result.content).toHaveLength(1);
expect(result.content).toEqual([
{
address: null,
latlng: null,
profileType: 'private',
},
]);
});

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

const result = await forTesting.fetchPrivate(
requestID,
authProfileAndToken
);
expect(result.status === 'DEPENDENCY_ERROR' && result.message).toBe(
'[BRP] Error fetching BRP data'
);
});
});

describe('fetchCommercial', () => {
const requestID = 'test-request-id';
const authProfileAndToken = getAuthProfileAndToken('commercial');

it('should return commercial addresses if fetching KVK data is successful', async () => {
(fetchKVK as Mock).mockResolvedValueOnce(
apiSuccessResult({ vestigingen: [adres, adres2] })
);

(getKvkAddresses as Mock).mockReturnValueOnce([adres, adres2]);

(fetchBAG as Mock).mockResolvedValueOnce(
apiSuccessResult({ latlng: { lat: 1, lng: 1 }, address: 'Een adres' })
);
(fetchBAG as Mock).mockResolvedValueOnce(
apiSuccessResult({ latlng: { lat: 1, lng: 1 }, address: 'Een 2e adres' })
);

const result = await forTesting.fetchCommercial(
requestID,
authProfileAndToken
);

expect(fetchBAG).toHaveBeenCalledWith(requestID, adres);
expect(fetchBAG).toHaveBeenCalledWith(requestID, adres2);

expect(result).toMatchInlineSnapshot(`
{
"content": [
{
"address": "Een adres",
"latlng": {
"lat": 1,
"lng": 1,
},
"profileType": "commercial",
},
{
"address": "Een 2e adres",
"latlng": {
"lat": 1,
"lng": 1,
},
"profileType": "commercial",
},
],
"status": "OK",
}
`);
});

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

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

describe('fetchMyLocation', () => {
const requestID = 'test-request-id';
const authProfileAndTokenPrivate = getAuthProfileAndToken();
const authProfileAndTokenCommercial = getAuthProfileAndToken('commercial');

it('should return locations if fetching commercial and private data is successful', async () => {
(fetchBRP as Mock).mockResolvedValueOnce(
apiSuccessResult({
mokum: true,
adres,
})
);

(fetchBAG as Mock).mockResolvedValueOnce(
apiSuccessResult({ latlng: { lat: 1, lng: 1 }, address: 'Een adres' })
);

(fetchBAG as Mock).mockResolvedValueOnce(
apiSuccessResult({ latlng: { lat: 2, lng: 2 }, address: 'Een 2e adres' })
);

(fetchKVK as Mock).mockResolvedValueOnce(
apiSuccessResult({ vestigingen: [adres2] })
);

(getKvkAddresses as Mock).mockReturnValueOnce([adres2]);

const result = await fetchMyLocation(requestID, authProfileAndTokenPrivate);
expect(result).toEqual({
content: [
{
address: 'Een 2e adres',
latlng: {
lat: 2,
lng: 2,
},
profileType: 'private',
},
{
address: 'Een adres',
latlng: {
lat: 1,
lng: 1,
},
profileType: 'commercial',
},
],
status: 'OK',
});
});

it('should return commercial locations if profile type is commercial', async () => {
(fetchBAG as Mock).mockResolvedValueOnce(
apiSuccessResult({ latlng: { lat: 2, lng: 2 }, address: 'Een 2e adres' })
);

(fetchKVK as Mock).mockResolvedValueOnce(
apiSuccessResult({ vestigingen: [adres2] })
);

(getKvkAddresses as Mock).mockReturnValueOnce([adres2]);

const result = await fetchMyLocation(
requestID,
authProfileAndTokenCommercial
);
expect(result.status).toBe('OK');
expect(result.content).toHaveLength(1);
});

it('should return an error if fetching commercial data fails', async () => {
(fetchKVK as Mock).mockResolvedValueOnce(
apiErrorResult('Server down', null)
);

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

it('should return an error if no locations found', async () => {
(fetchKVK as Mock).mockResolvedValueOnce(
apiSuccessResult({ vestigingen: [] })
);
(getKvkAddresses as Mock).mockReturnValueOnce([]);
(fetchBRP as Mock).mockResolvedValueOnce(apiErrorResult('mwa', null));

const result = await fetchMyLocation(requestID, authProfileAndTokenPrivate);
expect(result).toEqual({
content: null,
message: 'Could not fetch locations.',
status: 'ERROR',
});
});
});
Loading

0 comments on commit 2a60ee4

Please sign in to comment.