Created
January 19, 2021 17:14
-
-
Save jogilvyt/23648644abfb1cde24851a0eb6016ab8 to your computer and use it in GitHub Desktop.
LocalStorageMock to enable testing of localStorage on older versions of jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocalStorageMock { | |
constructor() { | |
this.store = {}; | |
} | |
clear() { | |
this.store = {}; | |
} | |
getItem(key) { | |
return this.store[key] || null; | |
} | |
setItem(key, value) { | |
this.store[key] = value; | |
} | |
removeItem(key) { | |
delete this.store[key]; | |
} | |
} | |
global.localStorage = new LocalStorageMock(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment