Skip to content

Commit

Permalink
feat: support calling connect with no arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Dec 17, 2020
1 parent 093ec10 commit fd2a2b7
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 22 deletions.
19 changes: 15 additions & 4 deletions src/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
InjectedConnector,
WalletConnectConnector
} from './connectors'
import { LocalStorage, Storage } from './storage'
import {
RequestArguments,
ProviderType,
Expand All @@ -14,18 +15,28 @@ import {
ClosableConnector,
LegacyProvider
} from './types'
import { getConfiguration } from './configuration'
import './declarations'

export class ConnectionManager {
providerType?: ProviderType
connector?: AbstractConnector

constructor(public storage: Storage) {}

async connect(
providerType: ProviderType,
providerType?: ProviderType,
chainId: ChainId = ChainId.MAINNET
): Promise<ConnectResponse> {
this.providerType = providerType
this.connector = this.getConnector(providerType, chainId)
const { storageKey } = getConfiguration()

this.providerType = providerType || this.storage.get(storageKey)
if (!this.providerType) {
throw new Error('connect called without a provider and none was stored')
}

this.storage.set(storageKey, providerType)
this.connector = this.getConnector(this.providerType!, chainId)

const {
provider,
Expand Down Expand Up @@ -114,4 +125,4 @@ export class ConnectionManager {
}
}

export const connection = new ConnectionManager()
export const connection = new ConnectionManager(new LocalStorage())
7 changes: 0 additions & 7 deletions src/storage.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/storage/LocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Storage } from './Storage'

export class LocalStorage extends Storage {
get(key: string): any | undefined {
const result = window.localStorage.getItem(key)
return result === null ? undefined : result
}

set(key: string, value: any): void {
window.localStorage.setItem(key, value)
}
}
4 changes: 4 additions & 0 deletions src/storage/Storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export abstract class Storage {
abstract get(key: string): any | undefined
abstract set(key: string, value: any): void
}
2 changes: 2 additions & 0 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Storage'
export * from './LocalStorage'
58 changes: 49 additions & 9 deletions test/ConnectionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import chaiAsPromised from 'chai-as-promised'
import sinon from 'sinon'
import { ConnectionManager } from '../src/ConnectionManager'
import { ChainId, ClosableConnector, ProviderType } from '../src/types'
import { StubClosableConnector, StubConnector } from './utils'
import { StubClosableConnector, StubConnector, StubStorage } from './utils'

chai.use(chaiAsPromised)
const { expect } = chai

describe('ConnectionManager', () => {
let storage: StubStorage
let connectionManager: ConnectionManager

beforeEach(() => {
connectionManager = new ConnectionManager()
storage = new StubStorage()
connectionManager = new ConnectionManager(storage)
})

afterEach(() => {
sinon.restore()
storage.clean()
})

describe('#connect', () => {
Expand Down Expand Up @@ -76,29 +79,66 @@ describe('ConnectionManager', () => {
it('should not patch the provider with the request method if it already exists', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)
sinon.stub(stubConnector, 'activate').resolves({
account: '0x123123',
provider: { request: () => {} }
})

const result = await connectionManager.connect(
ProviderType.INJECTED,
ChainId.ROPSTEN
)
const activateResult = await stubConnector.activate()
const { account } = await stubConnector.activate()

expect(JSON.stringify(result)).to.eq(
JSON.stringify({
provider: {
request: () => {}
},
account: activateResult.account,
account,
chainId: ChainId.ROPSTEN
})
)
})

it('should store the last provider and chain')
it('should throw if called wihtout provider type and none is found on storage', () => {
expect(connectionManager.connect()).to.eventually.throw(
new Error('connect called without a provider and none was stored')
)
})

it('should store the last provider and chain', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)

await connectionManager.connect(ProviderType.INJECTED)

expect(storage.get()).to.eq(ProviderType.INJECTED)
})

it('should connect to the last supplied provider', async () => {
const stubConnector = new StubConnector()
const getConnectorStub = sinon
.stub(connectionManager, 'getConnector')
.returns(stubConnector)

await connectionManager.connect(ProviderType.FORTMATIC)
const result = await connectionManager.connect()
const { account } = await stubConnector.activate()

expect(
getConnectorStub.firstCall.calledWith(ProviderType.FORTMATIC)
).to.eq(true)
expect(
getConnectorStub.secondCall.calledWith(ProviderType.FORTMATIC)
).to.eq(true)

expect(JSON.stringify(result)).to.eq(
JSON.stringify({
provider: {
request: () => {}
},
account,
chainId: ChainId.MAINNET
})
)
})
})

describe('#disconnect', () => {
Expand Down
23 changes: 21 additions & 2 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { AbstractConnector } from '../src/connectors/AbstractConnector'
import { ConnectorUpdate } from '@web3-react/types'
import { AbstractConnector } from '../src/connectors/AbstractConnector'
import { ChainId } from '../src/types'
import { Storage } from '../src/storage'

export class StubConnector extends AbstractConnector {
async activate(): Promise<ConnectorUpdate> {
return {
provider: {
send: () => {}
send: () => {
// no-op
}
},
account: '0xdeadbeef'
}
Expand Down Expand Up @@ -34,3 +37,19 @@ export class StubClosableConnector extends StubConnector {
// no-op
}
}

export class StubStorage extends Storage {
value: any

get() {
return this.value
}

set(_key: string, value: any) {
this.value = value
}

clean() {
this.value = undefined
}
}

0 comments on commit fd2a2b7

Please sign in to comment.