Skip to content

Commit fd2a2b7

Browse files
feat: support calling connect with no arguments
1 parent 093ec10 commit fd2a2b7

File tree

7 files changed

+103
-22
lines changed

7 files changed

+103
-22
lines changed

src/ConnectionManager.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
InjectedConnector,
66
WalletConnectConnector
77
} from './connectors'
8+
import { LocalStorage, Storage } from './storage'
89
import {
910
RequestArguments,
1011
ProviderType,
@@ -14,18 +15,28 @@ import {
1415
ClosableConnector,
1516
LegacyProvider
1617
} from './types'
18+
import { getConfiguration } from './configuration'
1719
import './declarations'
1820

1921
export class ConnectionManager {
2022
providerType?: ProviderType
2123
connector?: AbstractConnector
2224

25+
constructor(public storage: Storage) {}
26+
2327
async connect(
24-
providerType: ProviderType,
28+
providerType?: ProviderType,
2529
chainId: ChainId = ChainId.MAINNET
2630
): Promise<ConnectResponse> {
27-
this.providerType = providerType
28-
this.connector = this.getConnector(providerType, chainId)
31+
const { storageKey } = getConfiguration()
32+
33+
this.providerType = providerType || this.storage.get(storageKey)
34+
if (!this.providerType) {
35+
throw new Error('connect called without a provider and none was stored')
36+
}
37+
38+
this.storage.set(storageKey, providerType)
39+
this.connector = this.getConnector(this.providerType!, chainId)
2940

3041
const {
3142
provider,
@@ -114,4 +125,4 @@ export class ConnectionManager {
114125
}
115126
}
116127

117-
export const connection = new ConnectionManager()
128+
export const connection = new ConnectionManager(new LocalStorage())

src/storage.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/storage/LocalStorage.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Storage } from './Storage'
2+
3+
export class LocalStorage extends Storage {
4+
get(key: string): any | undefined {
5+
const result = window.localStorage.getItem(key)
6+
return result === null ? undefined : result
7+
}
8+
9+
set(key: string, value: any): void {
10+
window.localStorage.setItem(key, value)
11+
}
12+
}

src/storage/Storage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export abstract class Storage {
2+
abstract get(key: string): any | undefined
3+
abstract set(key: string, value: any): void
4+
}

src/storage/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './Storage'
2+
export * from './LocalStorage'

test/ConnectionManager.spec.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ import chaiAsPromised from 'chai-as-promised'
33
import sinon from 'sinon'
44
import { ConnectionManager } from '../src/ConnectionManager'
55
import { ChainId, ClosableConnector, ProviderType } from '../src/types'
6-
import { StubClosableConnector, StubConnector } from './utils'
6+
import { StubClosableConnector, StubConnector, StubStorage } from './utils'
77

88
chai.use(chaiAsPromised)
99
const { expect } = chai
1010

1111
describe('ConnectionManager', () => {
12+
let storage: StubStorage
1213
let connectionManager: ConnectionManager
1314

1415
beforeEach(() => {
15-
connectionManager = new ConnectionManager()
16+
storage = new StubStorage()
17+
connectionManager = new ConnectionManager(storage)
1618
})
1719

1820
afterEach(() => {
1921
sinon.restore()
22+
storage.clean()
2023
})
2124

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

8483
const result = await connectionManager.connect(
8584
ProviderType.INJECTED,
8685
ChainId.ROPSTEN
8786
)
88-
const activateResult = await stubConnector.activate()
87+
const { account } = await stubConnector.activate()
8988

9089
expect(JSON.stringify(result)).to.eq(
9190
JSON.stringify({
9291
provider: {
9392
request: () => {}
9493
},
95-
account: activateResult.account,
94+
account,
9695
chainId: ChainId.ROPSTEN
9796
})
9897
)
9998
})
10099

101-
it('should store the last provider and chain')
100+
it('should throw if called wihtout provider type and none is found on storage', () => {
101+
expect(connectionManager.connect()).to.eventually.throw(
102+
new Error('connect called without a provider and none was stored')
103+
)
104+
})
105+
106+
it('should store the last provider and chain', async () => {
107+
const stubConnector = new StubConnector()
108+
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)
109+
110+
await connectionManager.connect(ProviderType.INJECTED)
111+
112+
expect(storage.get()).to.eq(ProviderType.INJECTED)
113+
})
114+
115+
it('should connect to the last supplied provider', async () => {
116+
const stubConnector = new StubConnector()
117+
const getConnectorStub = sinon
118+
.stub(connectionManager, 'getConnector')
119+
.returns(stubConnector)
120+
121+
await connectionManager.connect(ProviderType.FORTMATIC)
122+
const result = await connectionManager.connect()
123+
const { account } = await stubConnector.activate()
124+
125+
expect(
126+
getConnectorStub.firstCall.calledWith(ProviderType.FORTMATIC)
127+
).to.eq(true)
128+
expect(
129+
getConnectorStub.secondCall.calledWith(ProviderType.FORTMATIC)
130+
).to.eq(true)
131+
132+
expect(JSON.stringify(result)).to.eq(
133+
JSON.stringify({
134+
provider: {
135+
request: () => {}
136+
},
137+
account,
138+
chainId: ChainId.MAINNET
139+
})
140+
)
141+
})
102142
})
103143

104144
describe('#disconnect', () => {

test/utils.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { AbstractConnector } from '../src/connectors/AbstractConnector'
21
import { ConnectorUpdate } from '@web3-react/types'
2+
import { AbstractConnector } from '../src/connectors/AbstractConnector'
33
import { ChainId } from '../src/types'
4+
import { Storage } from '../src/storage'
45

56
export class StubConnector extends AbstractConnector {
67
async activate(): Promise<ConnectorUpdate> {
78
return {
89
provider: {
9-
send: () => {}
10+
send: () => {
11+
// no-op
12+
}
1013
},
1114
account: '0xdeadbeef'
1215
}
@@ -34,3 +37,19 @@ export class StubClosableConnector extends StubConnector {
3437
// no-op
3538
}
3639
}
40+
41+
export class StubStorage extends Storage {
42+
value: any
43+
44+
get() {
45+
return this.value
46+
}
47+
48+
set(_key: string, value: any) {
49+
this.value = value
50+
}
51+
52+
clean() {
53+
this.value = undefined
54+
}
55+
}

0 commit comments

Comments
 (0)