I am writing a Jest test for a Vue component's method. Specifically, I want to test that a Vuex action (I've mapped to this action in the component with mapActions) was called with a certain argument.
I mock the argument that should be passed to the action-- my test is close, but the argument the action receives has a bunch of extra information, and I'm not sure why.
The method in my component that I'm testing is:
triggerSave (trip) {
if (this.canSave) {
const tripToSave = { tripInProcess: true, trip: { tripId: trip.tripId, }, };
this.updateMyTrips(tripToSave);
}
},
Where updateMyTrips is the Vuex action I've mapped to in my component.
In my jest tests, I mock the action that I'm testing in a beforeEach (along with the store)
describe('TripPlanner', () => {
let actions;
let store;
beforeEach(() => {
actions = {
updateMyTrips: jest.fn(),
},
store = new Vuex.Store({
modules: {
trips: {
namespaced: true,
actions,
},
},
});
});
The test itself that I have is
test('trigger save calls updateMyTrips action', () => {
const mockTrip = [
{
tripId: 1234,
tripDestination: 'Rio di Janeiro',
},
];
const wrapper = shallowMount(TripPlanner, { store, localVue, propsData: { trip: mockTrip, canSave: true, }, },);
wrapper.vm.updateMyTrips(mockTrip[0]);
const mockUpdate = { tripInProcess: true, trip: { tripId: mockTrip.tripId }, };
expect(actions.updateMyTrips).toHaveBeenCalledWith(mockUpdate);
});
When I run the test, and look at the Expected/Received values, it does receive my mockUpdate object, but also receives
"commit": [Function anonymous],
+ "dispatch": [Function anonymous],
+ "getters": Object {},
+ "rootGetters": Object {},
+ "rootState": Object {
+ "addOns": Object {},
+ "state": Object {},
So the test fails.
If I just test that expect(actions.updateMyTrips).toHaveBeenCalled the test passes...but when I test that it was called WITH that specific argument, it fails.
I don't understand why the store's dispatch, getters, state, etc. are also being received!
vm.updateMyTrips !== actions.updateMyTrips.