Ive the following code which use the local storage that working as expected and I want to create unit test for it the issue is that i use in this function the local storage
This is the function that I want to test
open: function(url) {
var that = this;
if (!localStorage.getItem(“alert”)) {
that_run(url)
.then(function(isBlocked) {
if (isBlocked) {
that._notify();
localStorage.setItem(“alert”, true);
} else {
localStorage.setItem(“alert”, true);
that._exe(“page”,url)
}
}).done();
} else {
that._exe(“page”,url)
}
},
This is the test which is working but I think to rewrite the window is bad practice and my question if I can write this test better ?
it.only("test for open", function () {
var url = "http://mytesturl”;
winlocalStorage.getItem = function(){
return false;
};
var oSimulateSpy = sandbox.spy(exc "_simulate");
return orun.open(url).then(function(){
expect(oSimulateSpy.called).to.be.true;
});
});
I saw this post and this use functional programing https://stackoverflow.com/a/20153543/6124024 but I think to pass the local storage as parameter is a bit overkill in this case since this function (open) is called many times from many places... is there a better/cleaner way to handle this?