Skip to content

Instantly share code, notes, and snippets.

@djpogo
Created February 7, 2020 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djpogo/28eb99431d9459180c974b9972bff041 to your computer and use it in GitHub Desktop.
Save djpogo/28eb99431d9459180c974b9972bff041 to your computer and use it in GitHub Desktop.
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