1
1
import { ethErrors } from 'eth-rpc-errors' ;
2
2
import AutoPairingPostMessageConnection from '../utils/messaging/AutoPairingPostMessageConnection' ;
3
3
import { ChainAgnosticProvider } from './ChainAgnosticProvider' ;
4
+ import onDomReady from './utils/onDomReady' ;
5
+ import { DAppProviderRequest } from '../connections/dAppConnection/models' ;
6
+
7
+ jest . mock ( '../utils/messaging/AutoPairingPostMessageConnection' , ( ) => {
8
+ const mocks = {
9
+ connect : jest . fn ( ) . mockResolvedValue ( undefined ) ,
10
+ on : jest . fn ( ) ,
11
+ request : jest . fn ( ) . mockResolvedValue ( { } ) ,
12
+ } ;
13
+ return jest . fn ( ) . mockReturnValue ( mocks ) ;
14
+ } ) ;
15
+
16
+ export const matchingPayload = ( payload ) =>
17
+ expect . objectContaining ( {
18
+ data : expect . objectContaining ( payload ) ,
19
+ } ) ;
4
20
5
21
jest . mock ( './utils/onDomReady' ) ;
6
22
jest . mock ( '../utils/messaging/AutoPairingPostMessageConnection' , ( ) => {
@@ -21,10 +37,15 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
21
37
expect ( channelMock . connect ) . toHaveBeenCalled ( ) ;
22
38
expect ( channelMock . request ) . not . toHaveBeenCalled ( ) ;
23
39
} ) ;
24
- it ( 'waits for message channel to be connected' , async ( ) => {
40
+ it ( 'should wait for message channel to be connected' , async ( ) => {
25
41
const mockedChannel = new AutoPairingPostMessageConnection ( false ) ;
26
42
27
43
const provider = new ChainAgnosticProvider ( channelMock ) ;
44
+
45
+ await new Promise ( process . nextTick ) ;
46
+
47
+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
48
+
28
49
expect ( mockedChannel . connect ) . toHaveBeenCalled ( ) ;
29
50
expect ( mockedChannel . request ) . not . toHaveBeenCalled ( ) ;
30
51
@@ -35,13 +56,68 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
35
56
} ) ;
36
57
expect ( mockedChannel . request ) . toHaveBeenCalled ( ) ;
37
58
} ) ;
59
+ it ( 'should call the `DOMAIN_METADATA_METHOD` adter domReady' , async ( ) => {
60
+ new ChainAgnosticProvider ( channelMock ) ;
61
+ await new Promise ( process . nextTick ) ;
62
+ expect ( channelMock . request ) . toHaveBeenCalledTimes ( 0 ) ;
63
+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
64
+ await new Promise ( process . nextTick ) ;
65
+
66
+ expect ( channelMock . request ) . toHaveBeenCalledTimes ( 1 ) ;
67
+
68
+ expect ( channelMock . request ) . toHaveBeenCalledWith (
69
+ // matchingPayload({
70
+ // method: DAppProviderRequest.INIT_DAPP_STATE,
71
+ // })
72
+ expect . objectContaining ( {
73
+ params : expect . objectContaining ( {
74
+ request : expect . objectContaining ( {
75
+ method : DAppProviderRequest . DOMAIN_METADATA_METHOD ,
76
+ } ) ,
77
+ } ) ,
78
+ } )
79
+ ) ;
80
+ } ) ;
38
81
} ) ;
39
82
40
83
describe ( 'request' , ( ) => {
84
+ it ( 'should collect pending requests till the dom is ready' , async ( ) => {
85
+ const provider = new ChainAgnosticProvider ( channelMock ) ;
86
+ // wait for init to finish
87
+ await new Promise ( process . nextTick ) ;
88
+
89
+ expect ( channelMock . request ) . toHaveBeenCalledTimes ( 0 ) ;
90
+
91
+ ( channelMock . request as jest . Mock ) . mockResolvedValue ( 'success' ) ;
92
+ const rpcResultCallback = jest . fn ( ) ;
93
+ provider
94
+ . request ( {
95
+ data : {
96
+ method : 'some-method' ,
97
+ params : [ { param1 : 1 } ] ,
98
+ } ,
99
+ } )
100
+ . then ( rpcResultCallback ) ;
101
+ await new Promise ( process . nextTick ) ;
102
+
103
+ expect ( channelMock . request ) . toHaveBeenCalledTimes ( 0 ) ;
104
+
105
+ // domReady triggers sending pending requests as well
106
+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
107
+ await new Promise ( process . nextTick ) ;
108
+
109
+ expect ( channelMock . request ) . toHaveBeenCalledTimes ( 2 ) ;
110
+
111
+ expect ( rpcResultCallback ) . toHaveBeenCalledWith ( 'success' ) ;
112
+ } ) ;
41
113
it ( 'should use the rate limits on `eth_requestAccounts` requests' , async ( ) => {
42
114
const provider = new ChainAgnosticProvider ( channelMock ) ;
43
115
( channelMock . request as jest . Mock ) . mockResolvedValue ( 'success' ) ;
44
116
117
+ await new Promise ( process . nextTick ) ;
118
+
119
+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
120
+
45
121
const firstCallCallback = jest . fn ( ) ;
46
122
const secondCallCallback = jest . fn ( ) ;
47
123
provider
@@ -69,6 +145,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
69
145
const provider = new ChainAgnosticProvider ( channelMock ) ;
70
146
( channelMock . request as jest . Mock ) . mockResolvedValue ( 'success' ) ;
71
147
148
+ await new Promise ( process . nextTick ) ;
149
+
150
+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
151
+
72
152
const firstCallCallback = jest . fn ( ) ;
73
153
const secondCallCallback = jest . fn ( ) ;
74
154
provider
@@ -93,6 +173,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
93
173
const provider = new ChainAgnosticProvider ( channelMock ) ;
94
174
( channelMock . request as jest . Mock ) . mockResolvedValueOnce ( 'success' ) ;
95
175
176
+ await new Promise ( process . nextTick ) ;
177
+
178
+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
179
+
96
180
await provider . request ( {
97
181
data : { method : 'some-method' , params : [ { param1 : 1 } ] } ,
98
182
sessionId : '00000000-0000-0000-0000-000000000000' ,
@@ -106,6 +190,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
106
190
// response for the actual call
107
191
( channelMock . request as jest . Mock ) . mockResolvedValueOnce ( 'success' ) ;
108
192
193
+ await new Promise ( process . nextTick ) ;
194
+
195
+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
196
+
109
197
provider . request ( {
110
198
data : { method : 'some-method' , params : [ { param1 : 1 } ] } ,
111
199
sessionId : '00000000-0000-0000-0000-000000000000' ,
0 commit comments