From 98bbfd472f49d6ac7d21b6a2f330777df102aa4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Sant=C3=A1ngelo?= Date: Thu, 17 Dec 2020 18:53:24 -0300 Subject: [PATCH] feat: test storage --- test/ConnectionManager.spec.ts | 9 +++++- test/storage.spec.ts | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/storage.spec.ts diff --git a/test/ConnectionManager.spec.ts b/test/ConnectionManager.spec.ts index 0be2513..da5673e 100644 --- a/test/ConnectionManager.spec.ts +++ b/test/ConnectionManager.spec.ts @@ -1,7 +1,8 @@ import chai from 'chai' import chaiAsPromised from 'chai-as-promised' import sinon from 'sinon' -import { ConnectionManager } from '../src/ConnectionManager' +import { ConnectionManager, connection } from '../src/ConnectionManager' +import { LocalStorage } from '../src/storage' import { ChainId, ClosableConnector, ProviderType } from '../src/types' import { StubClosableConnector, StubConnector, StubStorage } from './utils' @@ -22,6 +23,12 @@ describe('ConnectionManager', () => { storage.clean() }) + describe('connection', () => { + it('should use LocalStorage as its storage', () => { + expect(connection.storage).to.instanceOf(LocalStorage) + }) + }) + describe('#connect', () => { it('should set the provider type', async () => { const stubConnector = new StubConnector() diff --git a/test/storage.spec.ts b/test/storage.spec.ts new file mode 100644 index 0000000..4dc55a6 --- /dev/null +++ b/test/storage.spec.ts @@ -0,0 +1,51 @@ +import Sinon from 'sinon' +import sinon from 'sinon' +import { LocalStorage } from '../src/storage' + +describe('LocalStorage', () => { + const browser: any = global + const windowLocalStorage = { + getItem: (_key: string) => {}, + setItem: (_key: string, _value: any) => {} + } + let mockStorage: Sinon.SinonMock + + before(() => { + mockStorage = sinon.mock(windowLocalStorage) + browser.window = { localStorage: windowLocalStorage } + }) + + after(() => { + mockStorage.restore() + browser.window = undefined + }) + + describe('#get', () => { + it('should call the window localStorage get method', () => { + const key = 'key' + mockStorage + .expects('getItem') + .once() + .withArgs(key) + + const localStorage = new LocalStorage() + localStorage.get(key) + + mockStorage.verify() + }) + }) + + describe('#set', () => { + it('should call the window localStorage set method', () => { + const key = 'key' + const value = 'value' + mockStorage + .expects('setItem') + .once() + .withArgs(key, value) + + const localStorage = new LocalStorage() + localStorage.set(key, value) + }) + }) +})