Skip to content

Commit

Permalink
feat: test storage
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Dec 17, 2020
1 parent 8d4c33f commit 98bbfd4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
9 changes: 8 additions & 1 deletion test/ConnectionManager.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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()
Expand Down
51 changes: 51 additions & 0 deletions test/storage.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit 98bbfd4

Please sign in to comment.