UNPKG

@shopify/jest-dom-mocks

Version:
46 lines (42 loc) 1.22 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); 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]; } } exports["default"] = Storage;