How can I mock an array in Jest? I want my unit test to use the mocked value of an array in JS.
console.log("inside getStatusAsync:" + listOfsubscriptionId); //gives me undefined
'use strict';
const React = require('react-native');
var listOfsubscriptionId;
const moduleReadyPromise = new Promise(function(resolve, reject) {
const moduleReadySubscription = subscribeToListner(async (result) => {
listOfsubscriptionId = result.listOfsubscriptionId;
resolve();
unsubscribe(moduleReadySubscription);
});
});
function subscribeToListner(listener) {
return EventEmitter.addListener(
NativeModule.Event,
listener
);
}
function unsubscribe(subscription) {
subscription.remove();
};
const ExportedModule = {};
ExportedModule.getModuleReadyPromise = () => {
return moduleReadyPromise;
};
ExportedModule.setSubscriptionList = (list) => {
listOfsubscriptionId = list;
};
ExportedModule.getStatusAsync = async(topic) => {
console.log("inside getStatusAsync:" + listOfsubscriptionId);
await ExportedModule.getModuleReadyPromise();
return xxx
};
module.exports = ExportedModule;
Here is the Test File
'use strict';
require('regenerator/runtime');
const listOfsubscriptionIdTest = ["XXX", "YYY"];
jest.setMock('react-native', REACT_MOCK);
jest.dontMock('../Module');
const Module = require('../Module');
describe('Notifications', () => {
beforeEach(() => {
// Module.listOfsubscriptionId = listOfsubscriptionIdTest; // DOES NOT WORK
// Module.listOfsubscriptionId = jest.fn(() => listOfsubscriptionIdTest); // DOES NOT WORK
// Module.setSubscriptionList(listOfsubscriptionIdTest); // WORKS
});
pit('should be able to call Native getStatusAsync', async() => {
await Module.getStatusAsync('XXX');
expect(NATIVE_MODULE_MOCK.getStatusAsync).toBeCalledWith('XXX');
});
});
Aucun commentaire:
Enregistrer un commentaire