I managed to resolve it. Thanks to @anoop because his answer was of help but I had to manage it with a major workaround.
I am using jsdom and it currently DOES NOT support localStorage
. I added a fake in my jsdom configuration.
if (!global.window.localStorage) {
global.window.localStorage = {
getItem() { return '{}'; },
setItem() {}
};
}
And asserted it with:
it('should fetch from local storage', () => {
const props = {
currentUser: 'UMAIR',
user: {
is_key: false
}
};
const spy = sinon.spy(global.window.localStorage, "setItem");
spy(props);
expect(spy.calledWith( {
currentUser: 'UMAIR',
user: {
is_key: false
}
}));
spy.restore();
const stub = sinon.stub(global.window.localStorage, 'getItem');
stub(props);
expect(stub.calledWith(Object.keys(props)));
// stub.restore();
});
Also see:
How to mock localStorage in JavaScript unit tests?
https://github.com/gor181/webpack-react-babel-mocha-boilerplate/tree/master/test/utils
I also found an internal issue in Sinon
a week ago related to this but that has been resolved.
See: https://github.com/sinonjs/sinon/issues/1129
Hope this helps someone.
location
object with the same procedure and its working fine. However, in this case its giving meAttempted to wrap undefined property setItem as function
– AgglutinogenlocalStorage
is not defined. – Agglutinogen