skyline-console/test/unit/local-storage-mock.js
Hanxiang Gao 4eb68e7fb2 feat: Add initial code of skyline-console
Add initial code of skyline-console

Change-Id: Icba2152a014761f53789357b5085d0779cd0e2a4
2021-06-01 23:29:30 +08:00

47 lines
1.2 KiB
JavaScript

export class LocalStorage {
constructor(jest) {
Object.defineProperty(this, 'getItem', {
enumerable: false,
value: jest.fn((key) => this[key] || null),
});
Object.defineProperty(this, 'setItem', {
enumerable: false,
// not mentioned in the spec, but we must always coerce to a string
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),
});
} // end constructor
get length() {
return Object.keys(this).length;
}
// for backwards compatibility
get __STORE__() {
return this;
}
}