strapi/packages/admin-test-utils/lib/mocks/LocalStorageMock.js

38 lines
631 B
JavaScript
Raw Normal View History

'use strict';
class LocalStorageMock {
constructor() {
2023-04-20 17:12:32 +01:00
this.store = new Map();
}
clear() {
2023-04-20 17:12:32 +01:00
this.store.clear();
}
getItem(key) {
2023-04-20 17:12:32 +01:00
/**
* We return null to avoid returning `undefined`
* because `undefined` is not a valid JSON value.
*/
return this.store.get(key) ?? null;
}
setItem(key, value) {
2023-04-20 17:12:32 +01:00
this.store.set(key, String(value));
}
removeItem(key) {
2023-04-20 17:12:32 +01:00
this.store.delete(key);
}
get length() {
return this.store.size;
}
}
2023-04-20 17:12:32 +01:00
// eslint-disable-next-line no-undef
Object.defineProperty(window, 'localStorage', {
writable: true,
value: new LocalStorageMock(),
});