Skip to content

Commit

Permalink
fix: prevent passing empty parameters to ApiLoader (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
usefulthink committed Feb 1, 2024
1 parent 24ae7ab commit 0601753
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/components/__tests__/api-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ test('passes parameters to GoogleMapsAPILoader', () => {
});
});

test('passes parameters to GoogleMapsAPILoader', () => {
render(<APIProvider apiKey={'apikey'}></APIProvider>);

const actual = apiLoadSpy.mock.lastCall[0];
expect(Object.keys(actual)).toMatchObject(['key']);
});

test('renders inner components', async () => {
const LoadingStatus = () => {
const mapsLoaded = useApiIsLoaded();
Expand Down
19 changes: 9 additions & 10 deletions src/components/api-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import React, {
useState
} from 'react';

import {GoogleMapsApiLoader} from '../libraries/google-maps-api-loader';
import {
ApiParams,
GoogleMapsApiLoader
} from '../libraries/google-maps-api-loader';
import {APILoadingStatus} from '../libraries/api-loading-status';

type ImportLibraryFunction = typeof google.maps.importLibrary;
Expand Down Expand Up @@ -143,15 +146,11 @@ function useGoogleMapsApiLoader(props: APIProviderProps) {
() => {
(async () => {
try {
await GoogleMapsApiLoader.load(
{
key: apiKey,
v: version,
libraries: librariesString,
...otherApiParams
},
status => setStatus(status)
);
const params: ApiParams = {key: apiKey, ...otherApiParams};
if (version) params.v = version;
if (librariesString?.length > 0) params.libraries = librariesString;

await GoogleMapsApiLoader.load(params, status => setStatus(status));

for (const name of ['core', 'maps', ...libraries]) {
await importLibrary(name);
Expand Down

0 comments on commit 0601753

Please sign in to comment.