diff options
author | Mike Greiling <mike@pixelcog.com> | 2019-06-04 21:20:31 +0000 |
---|---|---|
committer | Mike Greiling <mike@pixelcog.com> | 2019-06-04 21:20:31 +0000 |
commit | 2e32e39350671e9d059592f91827a58ad04f0269 (patch) | |
tree | 2eef9d0bb7c18f9e851e03c05085a3e7688277cf /spec/frontend/helpers | |
parent | 632427bcc24403be21df5afe8e6bae9cf41c8bc7 (diff) | |
parent | a9850b25c3624fe31d0230ad3f0df00fec6b7d48 (diff) | |
download | gitlab-ce-2e32e39350671e9d059592f91827a58ad04f0269.tar.gz |
Merge branch 'fe-jestify-specs-starting-with-a-1' into 'master'
Jestify some specs that start with "A"
See merge request gitlab-org/gitlab-ce!28727
Diffstat (limited to 'spec/frontend/helpers')
-rw-r--r-- | spec/frontend/helpers/local_storage_helper.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/spec/frontend/helpers/local_storage_helper.js b/spec/frontend/helpers/local_storage_helper.js new file mode 100644 index 00000000000..48e66b11767 --- /dev/null +++ b/spec/frontend/helpers/local_storage_helper.js @@ -0,0 +1,41 @@ +/** + * Manage the instance of a custom `window.localStorage` + * + * This only encapsulates the setup / teardown logic so that it can easily be + * reused with different implementations (i.e. a spy or a [fake][1]) + * + * [1]: https://stackoverflow.com/a/41434763/1708147 + * + * @param {() => any} fn Function that returns the object to use for localStorage + */ +const useLocalStorage = fn => { + const origLocalStorage = window.localStorage; + let currentLocalStorage; + + Object.defineProperty(window, 'localStorage', { + get: () => currentLocalStorage, + }); + + beforeEach(() => { + currentLocalStorage = fn(); + }); + + afterEach(() => { + currentLocalStorage = origLocalStorage; + }); +}; + +/** + * Create an object with the localStorage interface but `jest.fn()` implementations. + */ +export const createLocalStorageSpy = () => ({ + clear: jest.fn(), + getItem: jest.fn(), + setItem: jest.fn(), + removeItem: jest.fn(), +}); + +/** + * Before each test, overwrite `window.localStorage` with a spy implementation. + */ +export const useLocalStorageSpy = () => useLocalStorage(createLocalStorageSpy); |