我想模擬與@googlemaps/js-api-loader 包一起使用的谷歌地圖API。
現在,我需要確保呼叫了地理編碼器,并且我正在嘗試這樣做:
const mockGeocoder = jest.fn();
jest.mock('google.maps.Geocoder', () => () => ({
geocoder: mockGeocoder,
}));
但我明白了
Cannot find module 'google.maps.Geocoder' from 'tests/unit/usecases/maps/getPlaceByAddress.spec.ts'
3 | const mockGeocoder = jest.fn();
4 |
> 5 | jest.mock('google.maps.Geocoder', () => () => ({
不知道如何進行。誰能幫我這個?
uj5u.com熱心網友回復:
檢查包https://www.npmjs.com/package/@googlemaps/jest-mocks以獲取模擬和示例。我不確定 Geocoder 是否有模擬,但應該可以在那里遵循模式(并發送 pr)。
uj5u.com熱心網友回復:
我實際上是在 Stackoverflow 上找到的。不知道為什么昨天谷歌沒有向我推薦這個。
這是我找到答案的地方
所以我做了以下事情:
const setupGoogleMock = () => {
global.window.google = {
maps: {
Geocoder: jest.fn(() => ({
geocode: mockGeocoder,
})),
GeocoderStatus: {
ERROR: 'ERROR',
INVALID_REQUEST: 'INVALID_REQUEST',
OK: 'OK',
OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
REQUEST_DENIED: 'REQUEST_DENIED',
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
ZERO_RESULTS: 'ZERO_RESULTS',
},
} as any,
};
};
然后 beforeAll(() => { setupGoogleMock(); });
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427061.html
標籤:javascript 谷歌地图 开玩笑的
