Skip to content

Instantly share code, notes, and snippets.

@djpogo
Created February 7, 2020 18:01
Embed
What would you like to do?
a unit testable localStorage mock
const fakeWindowLocalStorage = {
localStorage: {
data: {},
length: 0,
setItem(name, value) {
this.data[name] = value.toString();
this.length = Object.keys(this.data).length;
},
getItem(name) {
return this.data[name];
},
removeItem(name) {
delete this.data[name];
this.length = Object.keys(this.data).length;
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment