Add a way to check what params was instance initialised with
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?
@TomasRM2112 Please take a moment to fill out this short survey. Thank you!
This is an automated message, feel free to ignore.
@TomasRM2112 Thank you for opening this issue. 🙏 Please check out these other resources that might be applicable:
- Support Console - If you have a support contract.
- Discord - chat with other developers
-
StackOverflow - use the
google-mapstag - Google Maps Platform issue trackers
This is an automated message, feel free to ignore.
Sorry, just saw this issue: https://github.com/googlemaps/js-jest-mocks/issues/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
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});
});
It would be a very useful addition to automatically add the spies as described in @eegli's comment for all API objects.