updated tests

This commit is contained in:
jackiettran
2026-01-15 18:47:43 -05:00
parent 35d5050286
commit 63385e049c
13 changed files with 256 additions and 201 deletions

View File

@@ -11,18 +11,26 @@ jest.mock('@googlemaps/google-maps-services-js', () => ({
}))
}));
const mockLoggerInfo = jest.fn();
const mockLoggerError = jest.fn();
jest.mock('../../../utils/logger', () => ({
info: mockLoggerInfo,
error: mockLoggerError,
warn: jest.fn(),
withRequestId: jest.fn(() => ({
info: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
})),
}));
describe('GoogleMapsService', () => {
let service;
let consoleSpy, consoleErrorSpy;
beforeEach(() => {
// Clear all mocks
jest.clearAllMocks();
// Set up console spies
consoleSpy = jest.spyOn(console, 'log').mockImplementation();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
// Reset environment
delete process.env.GOOGLE_MAPS_API_KEY;
@@ -30,18 +38,13 @@ describe('GoogleMapsService', () => {
jest.resetModules();
});
afterEach(() => {
consoleSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
describe('Constructor', () => {
it('should initialize with API key and log success', () => {
process.env.GOOGLE_MAPS_API_KEY = 'test-api-key';
service = require('../../../services/googleMapsService');
expect(consoleSpy).toHaveBeenCalledWith('Google Maps service initialized');
expect(mockLoggerInfo).toHaveBeenCalledWith('Google Maps service initialized');
expect(service.isConfigured()).toBe(true);
});
@@ -50,7 +53,7 @@ describe('GoogleMapsService', () => {
service = require('../../../services/googleMapsService');
expect(consoleErrorSpy).toHaveBeenCalledWith('Google Maps API key not configured in environment variables');
expect(mockLoggerError).toHaveBeenCalledWith('Google Maps API key not configured in environment variables');
expect(service.isConfigured()).toBe(false);
});
@@ -299,10 +302,11 @@ describe('GoogleMapsService', () => {
status: 'ZERO_RESULTS'
});
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Places Autocomplete API error:',
'ZERO_RESULTS',
'No results found'
expect(mockLoggerError).toHaveBeenCalledWith(
'Places Autocomplete API error',
expect.objectContaining({
status: 'ZERO_RESULTS',
})
);
});
@@ -325,7 +329,7 @@ describe('GoogleMapsService', () => {
await expect(service.getPlacesAutocomplete('test input')).rejects.toThrow('Failed to fetch place predictions');
expect(consoleErrorSpy).toHaveBeenCalledWith('Places Autocomplete service error:', 'Network error');
expect(mockLoggerError).toHaveBeenCalledWith('Places Autocomplete service error', expect.objectContaining({ error: expect.any(Error) }));
});
});
});
@@ -557,10 +561,11 @@ describe('GoogleMapsService', () => {
await expect(service.getPlaceDetails('ChIJ123')).rejects.toThrow('The specified place was not found');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Place Details API error:',
'NOT_FOUND',
'Place not found'
expect(mockLoggerError).toHaveBeenCalledWith(
'Place Details API error',
expect.objectContaining({
status: 'NOT_FOUND',
})
);
});
@@ -582,7 +587,7 @@ describe('GoogleMapsService', () => {
await expect(service.getPlaceDetails('ChIJ123')).rejects.toThrow(originalError);
expect(consoleErrorSpy).toHaveBeenCalledWith('Place Details service error:', 'Network error');
expect(mockLoggerError).toHaveBeenCalledWith('Place Details service error', expect.objectContaining({ error: expect.any(Error) }));
});
});
});
@@ -769,10 +774,11 @@ describe('GoogleMapsService', () => {
status: 'ZERO_RESULTS'
});
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Geocoding API error:',
'ZERO_RESULTS',
'No results found'
expect(mockLoggerError).toHaveBeenCalledWith(
'Geocoding API error',
expect.objectContaining({
status: 'ZERO_RESULTS',
})
);
});
@@ -796,7 +802,7 @@ describe('GoogleMapsService', () => {
await expect(service.geocodeAddress('123 Test St')).rejects.toThrow('Failed to geocode address');
expect(consoleErrorSpy).toHaveBeenCalledWith('Geocoding service error:', 'Network error');
expect(mockLoggerError).toHaveBeenCalledWith('Geocoding service error', expect.objectContaining({ error: expect.any(Error) }));
});
});
});