I currently use lodashs random and shuffle functions but want to switch to use the provided random plugin. However, can't figure out how to mock this plugin in my Jest scenario tests since it is provided as a function parameter and not imported as a module.
What is the recommended way to do this?
I think you could pass a replacement plugin in your game definition:
import MyGame from '../MyGame';
const MockRandom = {
name: 'random',
api: () => {
D6: (diceCount) => 3,
Shuffle: (arr) => arr.reverse(),
},
}
const game = {
...MyGame,
plugins: [...MyGame.plugins, MockRandom],
};
Haven’t tried this, but I don’t think boardgame.io does any checks to prevent you creating a plugin with the same name as an existing plugin, so this would overwrite the default random plugin. You could specify as many of the API methods as you needed (D4, Number, Die, etc…)
Thanks! I tried it and it seems to work like this:
const dieMock = jest.fn();
dieMock.mockReturnValue(42);
const mockRandomPlugin = {
name: "random",
api: () => ({
Die: dieMock,
_obj: {
getState: () => {},
isUsed: () => false,
},
}),
};
I also need to add the getState and isUsed functions in the _obj fields to prevent errors when boardgame.io tries to access those.