11import { ethErrors } from 'eth-rpc-errors' ;
22import AutoPairingPostMessageConnection from '../utils/messaging/AutoPairingPostMessageConnection' ;
33import { 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+ } ) ;
420
521jest . mock ( './utils/onDomReady' ) ;
622jest . mock ( '../utils/messaging/AutoPairingPostMessageConnection' , ( ) => {
@@ -21,10 +37,15 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
2137 expect ( channelMock . connect ) . toHaveBeenCalled ( ) ;
2238 expect ( channelMock . request ) . not . toHaveBeenCalled ( ) ;
2339 } ) ;
24- it ( 'waits for message channel to be connected' , async ( ) => {
40+ it ( 'should wait for message channel to be connected' , async ( ) => {
2541 const mockedChannel = new AutoPairingPostMessageConnection ( false ) ;
2642
2743 const provider = new ChainAgnosticProvider ( channelMock ) ;
44+
45+ await new Promise ( process . nextTick ) ;
46+
47+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
48+
2849 expect ( mockedChannel . connect ) . toHaveBeenCalled ( ) ;
2950 expect ( mockedChannel . request ) . not . toHaveBeenCalled ( ) ;
3051
@@ -35,13 +56,68 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
3556 } ) ;
3657 expect ( mockedChannel . request ) . toHaveBeenCalled ( ) ;
3758 } ) ;
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+ } ) ;
3881 } ) ;
3982
4083 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+ } ) ;
41113 it ( 'should use the rate limits on `eth_requestAccounts` requests' , async ( ) => {
42114 const provider = new ChainAgnosticProvider ( channelMock ) ;
43115 ( channelMock . request as jest . Mock ) . mockResolvedValue ( 'success' ) ;
44116
117+ await new Promise ( process . nextTick ) ;
118+
119+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
120+
45121 const firstCallCallback = jest . fn ( ) ;
46122 const secondCallCallback = jest . fn ( ) ;
47123 provider
@@ -69,6 +145,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
69145 const provider = new ChainAgnosticProvider ( channelMock ) ;
70146 ( channelMock . request as jest . Mock ) . mockResolvedValue ( 'success' ) ;
71147
148+ await new Promise ( process . nextTick ) ;
149+
150+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
151+
72152 const firstCallCallback = jest . fn ( ) ;
73153 const secondCallCallback = jest . fn ( ) ;
74154 provider
@@ -93,6 +173,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
93173 const provider = new ChainAgnosticProvider ( channelMock ) ;
94174 ( channelMock . request as jest . Mock ) . mockResolvedValueOnce ( 'success' ) ;
95175
176+ await new Promise ( process . nextTick ) ;
177+
178+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
179+
96180 await provider . request ( {
97181 data : { method : 'some-method' , params : [ { param1 : 1 } ] } ,
98182 sessionId : '00000000-0000-0000-0000-000000000000' ,
@@ -106,6 +190,10 @@ describe('src/background/providers/ChainAgnosticProvider', () => {
106190 // response for the actual call
107191 ( channelMock . request as jest . Mock ) . mockResolvedValueOnce ( 'success' ) ;
108192
193+ await new Promise ( process . nextTick ) ;
194+
195+ ( onDomReady as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
196+
109197 provider . request ( {
110198 data : { method : 'some-method' , params : [ { param1 : 1 } ] } ,
111199 sessionId : '00000000-0000-0000-0000-000000000000' ,
0 commit comments