Skip to content

Instantly share code, notes, and snippets.

@wobsoriano
Created August 4, 2021 06:51
Show Gist options
  • Save wobsoriano/29980a9e25c9abde0ec90590f9935fbd to your computer and use it in GitHub Desktop.
Save wobsoriano/29980a9e25c9abde0ec90590f9935fbd to your computer and use it in GitHub Desktop.
Google Maps jest mock
type Suggestions = google.maps.places.AutocompletePrediction[] | null;
describe('Your Test', () => {
const getMaps = (type = 'success', data: Suggestions): any => ({
maps: {
places: {
AutocompleteService: jest.fn(() => ({
getPlacePredictions: (_: any, cb: (dataArg: Suggestions) => void) => {
setTimeout(() => {
cb(type === "success" ? data : null);
}, 500)
}
})),
AutocompleteSessionToken: jest.fn()
},
},
});
it('should blabla', () => {
global.google = getMaps('success', []); // array is the places suggestions
await new Promise(res => setTimeout(res, 1000));
expect(whatever).toBe([]);
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment