Reputation:
I am using Leaflet, and I am creating a marker on it. Once the mouse is out of this marker, I would like to remove the pop-up I am displaying on mouseenter
:
marker.on('mouseout', e => this.leafletMap.closePopup());
In my testing, I would like to know if the callback provided is what I expect.
I already test if the event happens on mouseout with
expect((mockedMarker.on as jasmine.Spy).calls.argsFor(0)[0]).toEqual('mouseover');
Any ideas on how to do that ?
I tried something like this
expect((mockedMarker.on as jasmine.Spy).calls.argsFor(0)[1]).toEqual(JSON.stringify(component.leafletMap.closePopup));
But I don't know what I'm doing (I'm new to unit testing), and I can't find a solution online because I don't really know how to call this kind of test.
I should state that I work in Typescript.
Upvotes: 5
Views: 717
Reputation: 28757
It seems like what you want to test is that the function callback does what you want it to. For that, you should not be checking the function string. That test would be too brittle.
Instead, you should check that when you invoke the callback, the leaflet is closed. Here is a simple test for that. It assumes that you have properly scaffolded the marker, the leaflet, and the callback:
describe('Marker', () => {
it('should invoke closePopup', () => {
let marker = createMarkerWithLeaflet();
spyOn(marker, 'on');
spyOn(marker.leaflet, 'closePopup');
marker.on.calls.argsFor(0)[1]();
expect(marker.leaflet.closePopup).toHaveBeenCalled();
});
});
Upvotes: 0
Reputation: 146620
What you are doing is not a unit test and but since you want to test I would give a solution. Below is a sample jasmine script I worked out for you
let marker = function() {
function on(a, b) {
console.log(a, b);
}
this.on = on;
}
describe('this', () => {
it('xya', () => {
y = new marker()
spyOn(y, 'on');
y.on('onmouseout', e => this.leafletMap.closePopup());
const lambda = y.on.calls.argsFor(0)[1]
console.log(lambda.toString())
expect(lambda.toString()).toEqual("e => this.leafletMap.closePopup()")
})
})
Running on terminal
$ npx jasmine mocking.js
Randomized with seed 33786
Started
e => this.leafletMap.closePopup()
.
1 spec, 0 failures
Finished in 0.006 seconds
Randomized with seed 33786 (jasmine --random=true --seed=33786)
Upvotes: 1