I have event listener that will call a function that handle authentication. I want to test that if that function receives the wrong data, it will return a data and if not, will return another data.
But I not understanding how to mock that function and make expectations with that.
That's the listener:
window.addEventListener('message', authentication, false);
The function that I want to make expectations depending on the result:
export function* authentication({ data }) {
// Data structure {
// action: 'authentication',
// id: '7293847829109932,
// displayName: 'User Name',
// avatar: 'https://steamcommunity.com/images/user.png',
// access: 'access_token_string',
// refresh: 'refresh_token_string',
// }
if (data.action === 'authentication') {
localStorage.setItem('dualbits:access', data.access);
localStorage.setItem('dualbits:refresh', data.refresh);
}
// Will dispatch the success action if the data is correct
yield put(signInSuccess(data));
}
What did until now was mock the window global variable and the method addEventListener. And I did that expectation:
expect(window.addEventListener).toHaveBeenCalledWith(
'message',
authentication,
false
);