forked from Uniswap/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.test.tsx
75 lines (61 loc) · 2.29 KB
/
connect.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @jest-environment hardhat/dist/jsdom
*/
import '@ethersproject/providers'
import 'jest-environment-hardhat'
import { SwapWidget } from 'index'
import React, { ReactElement } from 'react'
import { cleanup, render, waitFor } from './test'
const HARDHAT_ACCOUNT_DISPLAY_STRING = `${hardhat.account.address?.substring(
0,
6
)}...${hardhat.account.address?.substring(hardhat.account.address.length - 4)}`
// We are testing different configurations of the widget, so we must run cleanup between tests.
afterEach(cleanup)
describe('connect', () => {
// MetaMask uses a 3000ms timeout to detect window.ethereum.
// Use fake timers to prevent this from slowing tests.
jest.useFakeTimers()
function itPromptsForWalletConnection(ui: ReactElement) {
it('prompts for wallet connection', async () => {
const widget = render(ui)
expect((await widget.findByTestId('connect-wallet')).textContent).toBe('Connect wallet')
})
}
function itExpectsWidgetToBeEnabled(ui: ReactElement) {
it('widget is enabled', async () => {
const widget = render(ui)
const tokenSelects = await widget.findAllByTestId('token-select')
await waitFor(() => tokenSelects.forEach((tokenSelect) => expect(tokenSelect).toHaveProperty('disabled', false)))
})
}
describe('with no params', () => {
const ui = <SwapWidget />
itPromptsForWalletConnection(ui)
itExpectsWidgetToBeEnabled(ui)
})
describe('with jsonRpcUrlMap', () => {
describe('with an array', () => {
const ui = <SwapWidget jsonRpcUrlMap={{ 1: [hardhat.url] }} />
itPromptsForWalletConnection(ui)
itExpectsWidgetToBeEnabled(ui)
})
describe('with a singleton', () => {
const ui = <SwapWidget jsonRpcUrlMap={{ 1: hardhat.url }} />
itPromptsForWalletConnection(ui)
itExpectsWidgetToBeEnabled(ui)
})
})
describe('with provider', () => {
// The real hardhat.provider relies on real timeouts when providing data.
jest.useRealTimers()
const ui = <SwapWidget provider={hardhat.provider} />
itExpectsWidgetToBeEnabled(ui)
it('displays connected account chip', async () => {
const widget = render(ui)
await waitFor(() =>
expect(widget.getByTestId('account').textContent?.toLowerCase()).toBe(HARDHAT_ACCOUNT_DISPLAY_STRING)
)
})
})
})