Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement mock for importLibrary #538

Merged
merged 2 commits into from
Oct 17, 2023

Conversation

usefulthink
Copy link
Contributor

Implemented a mock for importLibrary that returns the correct collection of api-objects in their mocked versions.

closes #468

Implemented a mock for importLibrary that returns the correct collection of api-objects in their mocked versions.
@usefulthink usefulthink enabled auto-merge (squash) October 17, 2023 20:32
@usefulthink usefulthink merged commit 339a281 into googlemaps:main Oct 17, 2023
9 checks passed
@usefulthink usefulthink deleted the feat/import-library branch October 17, 2023 20:43
github-actions bot pushed a commit that referenced this pull request Oct 17, 2023
Implemented a mock for importLibrary that returns the correct collection of api-objects in their mocked versions. 339a281
@douar
Copy link

douar commented Jun 12, 2024

Hi @usefulthink!

I'm using the loader.importLibrary from @googlemaps/js-api-loader:
Screenshot 2024-06-12 at 12 06 31 PM

Does the mock importLibrary allow us to simulate an error to trigger catch block?

Any information would be much appreciated, thank you!

@usefulthink
Copy link
Contributor Author

In this case you should probably use jest.mock() to to mock out the actual implementation of the @googlemaps/js-api-loader module and replace the functions involved in the code you're testing. Then you can fully control what you want the loader to do in order to test how your application responds instead of testing with the api-loader in the loop which can be quite error-prone.

I've done something like that here, and it greatly simplified the whole testing setup: https://github.com/googlemaps/react-wrapper/blob/4667a308533f54591d89057775eb6ec8c4e73b35/src/index.test.tsx

@douar
Copy link

douar commented Jun 12, 2024

I will give that a try! Thank you!

@usefulthink
Copy link
Contributor Author

@douar One more thing I just thought about - if you need the actual return values of importLibrary in your tests, you can do that as well. Here's an example for that: https://github.com/visgl/react-google-maps/blob/main/src/components/__tests__/info-window.test.tsx#L24-L36 (in that test, the useMapsLibraryMock is roughly equivalent to the Loader.importLibrary function).

@douar
Copy link

douar commented Jun 13, 2024

@douar One more thing I just thought about - if you need the actual return values of importLibrary in your tests, you can do that as well. Here's an example for that: https://github.com/visgl/react-google-maps/blob/main/src/components/__tests__/info-window.test.tsx#L24-L36 (in that test, the useMapsLibraryMock is roughly equivalent to the Loader.importLibrary function).

That is definitely helpful! Thank you for the references and resources 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Core: importLibrary method is missing in provided google object
2 participants