Within my project, there is a function that calls a method from the bootstrapper object within the window. Here's the code for the function:
export default function measurement(analObj) {
if (window.Bootsrapper._trackAnalytics === function) {
window.Bootstrapper._trackAnalytics(analObj);
}
}
To test this function in Jest, I wrote the following code snippet:
import measurement from "../helpers/measurement";
describe('Test measurement', () => {
beforeAll(() => {
const Bootstrapper = {
_trackAnalytics: function(obj) {
return obj;
},
};
window.Bootstrapper = Bootstrapper;
})
test('should send analytics object to rtrack analyitics', () => {
const testObj = {
pageName: "Leave Abasence"
}
const result = measurement(testObj);
expect(testObj).toEqual(result);
})
})
When running the test, the variable "result" returns "undefined" as I am facing issues with making the
window.measurement._trackAnalytics
function available during runtime.
I have a couple of questions:
Is my approach to unit testing this scenario correct? If yes, how can I ensure the availability of the
_trackAnalytics
function for themeasurement
function during runtime?If you have any better suggestions or alternative approaches, please feel free to share.