I am working on a Redux app & trying to create a spy using Jasmine on an ES6 default exported function. I have attempted a few different ways of spying on the function, including using a wildcard import to access the 'default' property of the import, but nothing I've tried has worked thus far. Below is an example, where I would want to test widgets.js
and spy on the widget function. Is there a way to achieve this without having to change the way im exporting the function from widget.js
?
widget.js
import { Map } from 'immutable'; import { CREATE_WIDGET } from 'actions'; const initialState = Map({ id: undefined, name: undefined }); export default function widget(state=initialState, action) { switch (action.type) { case CREATE_WIDGET: return state.update((widget) => widget.merge(action.widget)); default: return state; } }
widgets.js
import { OrderedMap } from 'immutable'; import { CREATE_ROOM } from 'actions'; import widget from './widget'; const initialState = OrderedMap(); export default function widgets(state=initialState, action={}) { switch (action.type) { case CREATE_ROOM: return state.set(action.widget.id, widget(undefined, action)); default: return state; } }
You say you tried importing the wildcard and spying on default? What was the issue with that approach? I just ran into this problem and doing this solved it for me:
import * as widget from './widget'; describe('widget spec', () => { beforeEach(() => { spyOn(widget, 'default'); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With