I have the following method which works. I am trying to write a test for it.
How could I mock window.getStatus to return the values in the String so that I could mock a return value
and also be able to validate that the String was called with the correct value?
import get from 'lodash/get';
const getData = (name) => get(window.getStatus(),
`data.toJS()[\'base-path\'].current[\'${name}'].exclusions[\'period\']`);
Tried the following test.
But it throws this error.
TypeError: window.getStatus is not a function
Test
describe('My Test', () => {
let windowSpy;
beforeEach(() => {
windowSpy = jest.spyOn(window, "window", "get");
});
afterEach(() => {
windowSpy.mockRestore();
});
it('should ', () => {
windowSpy.mockImplementation(() => ({
getStatus: {
data: {
'base-path': {
current: {
'login-name': {
exclusions: {
'period': 'mock name',
}
}
}
}
}
}
}));
const result = getData('login-name');
expect(result).toEqual('mock name');
});
});