ermakovich/pptr-mock-server

How to mock loading Google Map API & support regex on the handler

ferryarman opened this issue · 2 comments

Hi @ermakovich

1). Just wondering whether your library supports mocking the connection to Google Map API ?

this.mockRequest.on('GET', `https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=${GOOGLE_MAP_API_KEY}`, 200, { body: {} })

Coz, I kept receiving this, even though I have provided valid GOOGLE_MAP_API_KEY

ReferenceError: google is not defined

2). Does the mockRequest handler supports regex on the API url ?

Eg.

this.mockRequest.on('GET', /^https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key/`, 200, { body: {} })

Thanks in advance,
Ferdinand

Hi @ferryarman

  1. From my experience this error means that Google Maps API is not initialized on the page. Take a look at google-maps NPM package, it does what you need.

Regarding mocking the connection - it definitely supports. By the way you can omit query params if they are not essential. Very simplified version would look like:

this.mockRequest.get('https://maps.googleapis.com/maps/api/js', 200)

If you need to continue all requests to Google Maps API, you can do this in InitOptions.onRequest, for example:

this.mockServer = await mockServer.init(page, {
    baseAppUrl,
    baseApiUrl,
    onRequest: request => {
      if (request.url().startsWith('https://maps.googleapis.com')) {
        request.continue();
        return true;
      }
    },
  });
  1. No, unfortunately regexes are not supported at the moment.

Thank you for the suggestion 👍