I have a Redux action, which itself dispatches two other actions. Each action is retrieved from an imported function. One from a local module, the other from an external library.
import { functionA } from './moduleA';
import { functionB } from 'libraryB';
export function myAction() {
return (dispatch) => {
dispatch(functionA());
...
dispatch(functionB());
}
}
In my tests I'm using a sinon sandbox to stub out the functions but only two of the tests pass. I'm expecting all 3 to pass.
import * as A from './moduleA';
import * as B from 'libraryB';
describe(__filename, async () => {
it('Calls 2 other actions', () => {
sandbox = sinon.sandbox.create();
const dispatch = sandbox.stub();
sandbox.stub(A, 'functionA');
sandbox.stub(B, 'functionB');
await myAction()(dispatch);
// passes
expect(dispatch.callCount).to.equal(2);
//passes
expect(A.functionA).to.have.been.called();
// fails
expect(B.functionB).to.have.been.called();
});
});
The last expect fails with the error:
TypeError: [Function: functionB] is not a spy or a call to a spy!
When I output the functions to the console I get this, which seems to be related to the way Babel imports an exported export (ES6 re-exported values are wrapped into Getter). The functions work live, just not in the test.
{ functionA: [Function: functionA] }
{ functionB: [Getter] }
I've tried using stub.get(getterFn) but that just gives me the error:
TypeError: Cannot redefine property: fetchTopicAnnotations