UNPKG

@shopify/jest-dom-mocks

Version:
42 lines (40 loc) 1.14 kB
class Storage { constructor() { this.getItem = jest.fn(this.unmockedGetItem); this.setItem = jest.fn(this.unmockedSetItem); this.removeItem = jest.fn(this.unmockedRemoveItem); this.clear = jest.fn(this.unmockedClearItem); this.key = jest.fn(this.unmockedKey); this.store = Object.create(null); } get length() { return Object.keys(this.store).length; } restore() { this.getItem.mockClear(); this.getItem.mockImplementation(this.unmockedGetItem); this.setItem.mockClear(); this.setItem.mockImplementation(this.unmockedSetItem); this.removeItem.mockClear(); this.removeItem.mockImplementation(this.unmockedRemoveItem); this.clear.mockClear(); this.clear.mockImplementation(this.unmockedClearItem); this.clear(); } unmockedGetItem(key) { return this.store[key] || null; } unmockedSetItem(key, value) { this.store[key] = value.toString(); } unmockedRemoveItem(key) { delete this.store[key]; } unmockedClearItem() { this.store = {}; } unmockedKey(index) { return Object.keys(this.store)[index]; } } export { Storage as default };