Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ipt-utils into main
  • Loading branch information
201flaviosilva committed Oct 10, 2022
2 parents 5d248aa + bd15949 commit 921adba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/DeployWiki.yml
Expand Up @@ -21,6 +21,6 @@ jobs:
env:
WIKI_DIR: wiki/ # WIKI_DIR's default is wiki/ - Make sure you have that / at the end. We use rsync
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_MAIL: ${{ secrets.EMAIL }} # github.event.pusher.email
GH_MAIL: ${{ github.event.pusher.email }}
GH_NAME: ${{ github.repository_owner }}
EXCLUDED_FILES: "_ignore/"
41 changes: 41 additions & 0 deletions snippets/Jest/MockStorage.js
@@ -0,0 +1,41 @@
// Jest - localStorage and sessionStorage
// This simulates the localStorage and sessionStorage in the jest environment
// Based: https://stackoverflow.com/questions/32911630/how-do-i-deal-with-localstorage-in-jest-tests

/**
* This simulate the localStorage and sessionStorage in the jest environment
*
* @example import "./MockStorage.js"; // only need to do this
*
* @see {@link https://stackoverflow.com/questions/32911630/how-do-i-deal-with-localstorage-in-jest-tests}
*/
class StorageMock {
constructor() { this.store = {}; }
/**
* Removes all key/value pairs, if there are any.
*
* Dispatches a storage event on Window objects holding an equivalent Storage object.
*/
clear() { this.store = {}; }

/** Returns the current value associated with the given key, or null if the given key does not exist. */
getItem(key) { return this.store[key] || null; }

/**
* Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
*
* Throws a "QuotaExceededError" DOMException exception if the new value couldn't be set. (Setting could fail if, e.g., the user has disabled storage for the site, or if the quota has been exceeded.)
*
* Dispatches a storage event on Window objects holding an equivalent Storage object.
*/
setItem(key, value) { this.store[key] = String(value); }

/**
* Removes the key/value pair with the given key, if a key/value pair with the given key exists.
*
* Dispatches a storage event on Window objects holding an equivalent Storage object.
*/
removeItem(key) { delete this.store[key]; }
}
global.localStorage = new StorageMock;
global.sessionStorage = new StorageMock;

0 comments on commit 921adba

Please sign in to comment.