Skip to content

Commit

Permalink
feat: all the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Dec 23, 2020
1 parent f44a3e0 commit fef0ae8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ export class ConnectionManager {
chainId: ChainId
): AbstractConnector {
switch (providerType) {
case ProviderType.INJECTED:
return new InjectedConnector(chainId)
case ProviderType.FORTMATIC:
return new FortmaticConnector(chainId)
case ProviderType.INJECTED:
return new InjectedConnector(chainId)
case ProviderType.WALLET_CONNECT:
return new WalletConnectConnector(chainId)
default:
Expand Down
42 changes: 42 additions & 0 deletions test/ConnectionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import chaiAsPromised from 'chai-as-promised'
import sinon from 'sinon'
import { getConfiguration } from '../src/configuration'
import { ConnectionManager, connection } from '../src/ConnectionManager'
import {
FortmaticConnector,
InjectedConnector,
WalletConnectConnector
} from '../src/connectors'
import { LocalStorage } from '../src/storage'
import { ChainId, ClosableConnector, ProviderType } from '../src/types'
import { StubClosableConnector, StubConnector, StubStorage } from './utils'
Expand Down Expand Up @@ -238,6 +243,7 @@ describe('ConnectionManager', () => {
})

it('should throw if no successful connect occurred', () => {
connectionManager.connector = undefined
expect(connectionManager.getProvider()).to.eventually.throw(
new Error('No valid connector found. Please .connect() first')
)
Expand Down Expand Up @@ -265,4 +271,40 @@ describe('ConnectionManager', () => {
browser.window = undefined
})
})

describe('#getConnector', () => {
it('should throw if an invalid provider type is supplied', () => {
const providerType = 'Invalid Provider Type' as any
expect(() =>
connectionManager.getConnector(providerType, ChainId.MAINNET)
).to.throw(`Invalid provider ${providerType}`)
})

it('should return an instance of FortmaticConnector for the supplied chain', () => {
const connector = connectionManager.getConnector(
ProviderType.FORTMATIC,
ChainId.KOVAN
)
expect(connector).to.be.instanceOf(FortmaticConnector)
expect(connector.getChainId()).to.eventually.eq(ChainId.KOVAN)
})

it('should return an instance of InjectedConnector for the supplied chain', () => {
const connector = connectionManager.getConnector(
ProviderType.INJECTED,
ChainId.KOVAN
)
expect(connector).to.be.instanceOf(InjectedConnector)
expect(connector.getChainId()).to.eventually.eq(ChainId.KOVAN)
})

it('should return an instance of WalletConnectConnector for the supplied chain', () => {
const connector = connectionManager.getConnector(
ProviderType.WALLET_CONNECT,
ChainId.KOVAN
)
expect(connector).to.be.instanceOf(WalletConnectConnector)
expect(connector.getChainId()).to.eventually.eq(ChainId.KOVAN)
})
})
})
12 changes: 11 additions & 1 deletion test/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe('LocalStorage', () => {
const browser: any = global
const windowLocalStorage = {
getItem: (_key: string) => {},
setItem: (_key: string, _value: any) => {}
setItem: (_key: string, _value: any) => {},
clear: () => {}
}
let mockStorage: Sinon.SinonMock

Expand Down Expand Up @@ -67,4 +68,13 @@ describe('LocalStorage', () => {
localStorage.set(key, value)
})
})

describe('#clear', () => {
it('should call the window localStorage clear method', () => {
mockStorage.expects('clear').once()

const localStorage = new LocalStorage()
localStorage.clear()
})
})
})

0 comments on commit fef0ae8

Please sign in to comment.