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

Add a way to check what params was instance initialised with #223

Open
TomasRM2112 opened this issue Mar 17, 2022 · 5 comments
Open

Add a way to check what params was instance initialised with #223

TomasRM2112 opened this issue Mar 17, 2022 · 5 comments
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@TomasRM2112
Copy link

It would be good if we could check what params was each instance initialised with. Similar to toHaveBeenCalledWith

For example:

const createPolygon = () => 
  new google.maps.Polygon({
    strokeWidth: 1
  });

it('constructed polygon', () => {
  createPolygon();
  const polygonMocks = mockInstances.get(Polygon);

  expect(polygonMocks[0]).toHaveBeenCalledWith(expect.objectContaining({strokeWidth: 1}))
});

Probably not working example, but just to get the idea. After reading the code I believe there is no way of doing this at the moment?

@jpoehnelt
Copy link
Contributor

@TomasRM2112 Please take a moment to fill out this short survey. Thank you!

This is an automated message, feel free to ignore.

@jpoehnelt
Copy link
Contributor

@TomasRM2112 Thank you for opening this issue. 🙏
Please check out these other resources that might be applicable:

This is an automated message, feel free to ignore.

@TomasRM2112
Copy link
Author

Sorry, just saw this issue: #215 but I agree with the creator that preferably we wouldn't have to change the way we write code just to be able to test it

@eegli
Copy link
Contributor

eegli commented Mar 17, 2022

If you're okay with setting up a rather verbose spy, the following could work. I haven't tested it thoroughly though.
Note that you will need to create the spy after initialize has been called and provide a mock implementation using the new keyword, otherwise, Jest complains.

The following test passes:

import {initialize, Polygon} from '@googlemaps/jest-mocks';

type PolyArgs = ConstructorParameters<typeof Polygon>;

test('Polygon', () => {
  initialize();

  const polySpy = jest
    .spyOn(google.maps, 'Polygon')
    .mockImplementation((...args: PolyArgs) => new Polygon(...args));

  new google.maps.Polygon({strokeWeight: 2});

  expect(polySpy).toHaveBeenCalledWith<PolyArgs>({strokeWeight: 2});
});

@jpoehnelt jpoehnelt removed their assignment Jun 17, 2022
@usefulthink usefulthink added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p3 Desirable enhancement or fix. May not be included in next release. labels Sep 29, 2023
@usefulthink
Copy link
Contributor

It would be a very useful addition to automatically add the spies as described in @eegli's comment for all API objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

4 participants