Reputation: 14793
I have a module using react-native-sound
in the following ways:
const Sound = require('react-native-sound');
...
const something = Sound.DOCUMENT;
const someOtherThing = new Sound();
How do I mock such a module?
Upvotes: 0
Views: 1265
Reputation: 8662
I've mocked react-native-sound using a manual mock (in the __mocks__ folder) which looks like this:
const isFakeFilename = filename => /^blah.*/.test(filename);
const mockFunctions = {
play: jest.fn(cb => {
console.log('*** Playing sound! ***');
cb && cb(isFakeFilename(this.filename) ? false : true);
}),
setCategory: jest.fn(),
getDuration: jest.fn(() => 100),
getNumberOfChannels: jest.fn(() => 8)
};
const Sound = function(filename, blah2, cb) {
this.play = mockFunctions.play.bind(this);
this.filename = filename;
const savedFilename = filename;
setTimeout(() => {
if (isFakeFilename(savedFilename)) {
cb && cb(new Error('File does not exist! (mocked condition)'));
} else {
cb && cb();
}
});
};
Sound.prototype.play = mockFunctions.play.bind(Sound.prototype);
Sound.prototype.getDuration = mockFunctions.getDuration;
Sound.prototype.getNumberOfChannels = mockFunctions.getNumberOfChannels;
Sound.setCategory = mockFunctions.setCategory;
export default Sound;
export { mockFunctions };
Note how methods on the Sound import are added directly (Sound.setCategory
), and methods on instances of the class (play
, getDuration
etc.) are added using the prototype.
There's a little added complexity that you may not need using the mockFunctions
export. I use that to check for calls to the mocked functions by importing it separately into the test file like
import { mockFunctions } from 'react-native-sound';
// ...
expect(mockFunctions.play).toHaveBeenCalled();
Upvotes: 1