@locii/biopass-cognito-sdk
Version:
Biopass Cognito Web SDK
99 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocalStorageMock = exports.LocalStorage = void 0;
class LocalStorage {
constructor(jest) {
Object.defineProperty(this, 'getItem', {
enumerable: false,
value: jest.fn((key) => (this[key] !== undefined ? this[key] : null))
});
Object.defineProperty(this, 'setItem', {
enumerable: false,
value: jest.fn((key, val) => {
this[key] = val + '';
})
});
Object.defineProperty(this, 'removeItem', {
enumerable: false,
value: jest.fn((key) => {
delete this[key];
})
});
Object.defineProperty(this, 'clear', {
enumerable: false,
value: jest.fn(() => {
Object.keys(this).map((key) => delete this[key]);
})
});
Object.defineProperty(this, 'toString', {
enumerable: false,
value: jest.fn(() => {
return '[object Storage]';
})
});
Object.defineProperty(this, 'key', {
enumerable: false,
value: jest.fn((idx) => Object.keys(this)[idx] || null)
});
}
get length() {
return Object.keys(this).length;
}
get __STORE__() {
return this;
}
}
exports.LocalStorage = LocalStorage;
class LocalStorageMock {
constructor() {
this.store = {};
this.length = 0;
}
key(n) {
if (typeof n === 'undefined') {
throw new Error("Uncaught TypeError: Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present.");
}
if (n >= Object.keys(this.store).length) {
return null;
}
return Object.keys(this.store)[n];
}
getItem(key) {
if (!Object.keys(this.store).includes(key)) {
return null;
}
return this.store[key];
}
setItem(key, value) {
if (typeof key === 'undefined' && typeof value === 'undefined') {
throw new Error("Uncaught TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 0 present.");
}
if (typeof value === 'undefined') {
throw new Error("Uncaught TypeError: Failed to execute 'setItem' on 'Storage': 2 arguments required, but only 1 present.");
}
if (!key)
return undefined;
this.store[key] = value.toString() || '';
this.length = Object.keys(this.store).length;
return undefined;
}
removeItem(key) {
if (typeof key === 'undefined') {
throw new Error("Uncaught TypeError: Failed to execute 'removeItem' on 'Storage': 1 argument required, but only 0 present.");
}
delete this.store[key];
this.length = Object.keys(this.store).length;
return undefined;
}
clear() {
this.store = {};
this.length = 0;
return undefined;
}
}
const getLocalStorageMock = () => {
return new LocalStorageMock();
};
exports.getLocalStorageMock = getLocalStorageMock;
global.localStorage = new LocalStorageMock();
//# sourceMappingURL=localstorage.js.map