Created
          August 4, 2021 06:51 
        
      - 
      
- 
        Save wobsoriano/29980a9e25c9abde0ec90590f9935fbd to your computer and use it in GitHub Desktop. 
    Google Maps jest mock
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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