-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now checking to ensure within the U.S. before calling NWS and returni…
…ngan error if it is not
- Loading branch information
1 parent
93edff9
commit f450f57
Showing
5 changed files
with
1,786 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { WeatherService, InvalidProviderLocationError } from './weatherService'; | ||
import * as nwsClient from './providers/nws/client'; | ||
|
||
jest.mock('./cache', () => { | ||
return { | ||
Cache: jest.fn().mockImplementation(() => { | ||
return { | ||
get: jest.fn().mockResolvedValue(null), | ||
set: jest.fn().mockResolvedValue(null), | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
jest.mock('./providers/nws/client', () => { | ||
return { | ||
getWeather: jest.fn().mockImplementation(async (lat: number, lng: number) => { | ||
return { lat, lng, weather: 'sunny' }; | ||
}), | ||
}; | ||
}); | ||
|
||
describe('WeatherService', () => { | ||
let weatherService: WeatherService; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
weatherService = new WeatherService({ provider: 'nws' }); | ||
}); | ||
|
||
it('should return weather data for a location inside the United States', async () => { | ||
const lat = 38.8977; // Washington, D.C. | ||
const lng = -77.0365; | ||
|
||
const weather = await weatherService.getWeather(lat, lng); | ||
|
||
expect(weather).toEqual({ lat, lng, weather: 'sunny' }); | ||
}); | ||
|
||
it('should throw InvalidProviderLocationError for a location outside the United States', async () => { | ||
const lat = 51.5074; // London, UK | ||
const lng = -0.1278; | ||
|
||
await expect(weatherService.getWeather(lat, lng)).rejects.toThrow( | ||
InvalidProviderLocationError | ||
); | ||
}); | ||
|
||
it('should not call NWS API for a location outside the United States', async () => { | ||
const getWeatherSpy = jest.spyOn(nwsClient, 'getWeather'); | ||
const lat = -33.8688; // Sydney, Australia | ||
const lng = 151.2093; | ||
|
||
await expect(weatherService.getWeather(lat, lng)).rejects.toThrow( | ||
InvalidProviderLocationError | ||
); | ||
|
||
expect(getWeatherSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle invalid latitude or longitude', async () => { | ||
const lat = 100; // Invalid latitude | ||
const lng = 200; | ||
|
||
await expect(weatherService.getWeather(lat, lng)).rejects.toThrow( | ||
'Invalid latitude or longitude' | ||
); | ||
}); | ||
|
||
it('should use cached weather data if available', async () => { | ||
const cacheGetMock = jest.fn().mockResolvedValue(JSON.stringify({ cached: true })); | ||
const cacheSetMock = jest.fn(); | ||
|
||
// Replace cache methods with mocks | ||
(weatherService as any).cache.get = cacheGetMock; | ||
(weatherService as any).cache.set = cacheSetMock; | ||
|
||
const lat = 38.8977; | ||
const lng = -77.0365; | ||
|
||
const weather = await weatherService.getWeather(lat, lng); | ||
|
||
expect(weather).toEqual({ cached: true }); | ||
expect(cacheGetMock).toHaveBeenCalled(); | ||
expect(cacheSetMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.