Skip to content

Commit 7e13c83

Browse files
committed
Update PU-API repository tests for private geodata helper.
Assert map and search behavior through public repository methods and expected endpoint URLs.
1 parent 02185f7 commit 7e13c83

2 files changed

Lines changed: 40 additions & 32 deletions

File tree

server/src/repositories/map.repository.spec.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ describe(MapRepository.name, () => {
1616
getEnv: vitest.fn().mockReturnValue({ puApiHost: 'http://pu-api' }),
1717
} as unknown as ConfigRepository;
1818
const puApiRepository = {
19-
requestGeodata: vitest.fn().mockResolvedValue({ country: 'NL', state: 'NH', city: 'Amsterdam' }),
19+
reverseGeocode: vitest.fn().mockResolvedValue({ country: 'NL', state: 'NH', city: 'Amsterdam' }),
2020
} as unknown as PuApiRepository;
2121
const sut = new MapRepository(configRepository, metadataRepository, puApiRepository, logger as never, db as never);
2222

2323
const result = await sut.reverseGeocode({ latitude: 1, longitude: 2 });
2424

25-
expect(puApiRepository.requestGeodata).toHaveBeenCalledWith({
26-
path: '/reverse-geocode',
27-
query: { lat: 1, lon: 2 },
28-
context: 'reverse geocode',
29-
});
25+
expect(puApiRepository.reverseGeocode).toHaveBeenCalledWith({ latitude: 1, longitude: 2 });
3026
expect(result).toEqual({ country: 'NL', state: 'NH', city: 'Amsterdam' });
3127
});
3228

@@ -35,7 +31,7 @@ describe(MapRepository.name, () => {
3531
getEnv: vitest.fn().mockReturnValue({ puApiHost: null }),
3632
} as unknown as ConfigRepository;
3733
const puApiRepository = {
38-
requestGeodata: vitest.fn(),
34+
reverseGeocode: vitest.fn(),
3935
} as unknown as PuApiRepository;
4036
const sut = new MapRepository(configRepository, metadataRepository, puApiRepository, logger as never, db as never);
4137
const fallbackSpy = vitest
@@ -45,7 +41,7 @@ describe(MapRepository.name, () => {
4541
const result = await sut.reverseGeocode({ latitude: 3, longitude: 4 });
4642

4743
expect(fallbackSpy).toHaveBeenCalledWith({ latitude: 3, longitude: 4 });
48-
expect(puApiRepository.requestGeodata).not.toHaveBeenCalled();
44+
expect(puApiRepository.reverseGeocode).not.toHaveBeenCalled();
4945
expect(result).toEqual({ country: null, state: null, city: null });
5046
});
5147
});

server/src/repositories/pu-api.repository.spec.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,20 @@ describe(PuApiRepository.name, () => {
7272
expect(logger.debug).toHaveBeenCalledWith('Skipping PU API tenant sync, PU_API_HOST or PU_TENANT_NAME is not configured');
7373
});
7474

75-
it('should return null request when token cannot be read', async () => {
75+
it('should return null reverse geocode when token cannot be read', async () => {
7676
vitest.mocked(readFile).mockRejectedValue(new Error('not found'));
7777

78-
const result = await sut.requestGeodata({
79-
path: '/reverse-geocode',
80-
query: { lat: 1.23, lon: 4.56 },
81-
context: 'reverse geocode',
78+
const result = await sut.reverseGeocode<{ country: string; state: string; city: string }>({
79+
latitude: 1.23,
80+
longitude: 4.56,
8281
});
8382

8483
expect(result).toBeNull();
8584
expect(global.fetch).not.toHaveBeenCalled();
8685
expect(logger.error).toHaveBeenCalled();
8786
});
8887

89-
it('should return geodata response payload', async () => {
88+
it('should return reverse geocode payload', async () => {
9089
vitest.spyOn(global, 'fetch').mockResolvedValue({
9190
ok: true,
9291
headers: new Headers(),
@@ -99,36 +98,49 @@ describe(PuApiRepository.name, () => {
9998
statusText: 'OK',
10099
} as Response);
101100

102-
const result = await sut.requestGeodata<{ country: string; state: string; city: string }>({
103-
path: '/reverse-geocode',
104-
query: { lat: 52.37, lon: 4.89 },
105-
context: 'reverse geocode',
101+
const result = await sut.reverseGeocode<{ country: string; state: string; city: string }>({
102+
latitude: 52.37,
103+
longitude: 4.89,
106104
});
107105

108106
expect(result).toEqual({ country: 'NL', state: 'North Holland', city: 'Amsterdam' });
107+
expect(global.fetch).toHaveBeenCalledWith(
108+
new URL('/internal/api/map/reverse-geocode?lat=52.37&lon=4.89', 'http://pu-api'),
109+
expect.anything(),
110+
);
109111
});
110112

111-
it('should call reverse geocode endpoint via helper method', async () => {
112-
const requestSpy = vitest.spyOn(sut, 'requestGeodata').mockResolvedValue({ country: null, state: null, city: null });
113+
it('should call reverse geocode endpoint', async () => {
114+
vitest.spyOn(global, 'fetch').mockResolvedValue({
115+
ok: true,
116+
headers: new Headers(),
117+
json: async () => ({ country: null, state: null, city: null }),
118+
status: 200,
119+
statusText: 'OK',
120+
} as Response);
113121

114122
await sut.reverseGeocode({ latitude: 52.37, longitude: 4.89 });
115123

116-
expect(requestSpy).toHaveBeenCalledWith({
117-
path: '/reverse-geocode',
118-
query: { lat: 52.37, lon: 4.89 },
119-
context: 'reverse geocode',
120-
});
124+
expect(global.fetch).toHaveBeenCalledWith(
125+
new URL('/internal/api/map/reverse-geocode?lat=52.37&lon=4.89', 'http://pu-api'),
126+
expect.anything(),
127+
);
121128
});
122129

123-
it('should call search places endpoint via helper method', async () => {
124-
const requestSpy = vitest.spyOn(sut, 'requestGeodata').mockResolvedValue([]);
130+
it('should call search places endpoint', async () => {
131+
vitest.spyOn(global, 'fetch').mockResolvedValue({
132+
ok: true,
133+
headers: new Headers(),
134+
json: async () => [],
135+
status: 200,
136+
statusText: 'OK',
137+
} as Response);
125138

126139
await sut.searchPlaces('amst');
127140

128-
expect(requestSpy).toHaveBeenCalledWith({
129-
path: '/search-places',
130-
query: { q: 'amst' },
131-
context: 'search places',
132-
});
141+
expect(global.fetch).toHaveBeenCalledWith(
142+
new URL('/internal/api/map/search-places?q=amst', 'http://pu-api'),
143+
expect.anything(),
144+
);
133145
});
134146
});

0 commit comments

Comments
 (0)