@@ -76,7 +76,8 @@ describe("events", () => {
76
76
77
77
test ( "subscribe should pipe the events to the callback" , async ( ) => {
78
78
const filter = { eventTypes : [ "A" ] }
79
- const callback = jest . fn ( )
79
+ const onData = jest . fn ( )
80
+ const onError = jest . fn ( )
80
81
81
82
const mockEvents = [ { type : "A" } , { type : "B" } ] as any [ ]
82
83
@@ -85,59 +86,82 @@ describe("events", () => {
85
86
return mockSubscription
86
87
} )
87
88
88
- events ( filter ) . subscribe ( callback )
89
+ events ( filter ) . subscribe ( onData , onError )
89
90
90
91
// Flush the event loop
91
92
await new Promise ( resolve => setTimeout ( resolve , 0 ) )
92
93
93
- expect ( callback . mock . calls ) . toEqual ( [
94
- [ mockEvents [ 0 ] , null ] ,
95
- [ mockEvents [ 1 ] , null ] ,
96
- ] )
94
+ expect ( onData . mock . calls ) . toEqual ( [ [ { type : "A" } ] , [ { type : "B" } ] ] )
95
+ expect ( onError ) . not . toHaveBeenCalled ( )
97
96
} )
98
97
99
98
test ( "subscribe should pipe the errors to the callback" , async ( ) => {
100
99
const filter = { eventTypes : [ "A" ] }
101
- const callback = jest . fn ( )
100
+ const onData = jest . fn ( )
101
+ const onError = jest . fn ( )
102
102
103
103
const mockError = new Error ( "mock error" )
104
104
105
- jest . mocked ( subscribe ) . mockImplementation ( ( { onError} ) => {
106
- onError ( mockError )
105
+ jest . mocked ( subscribe ) . mockImplementation ( ( { onError : _onError } ) => {
106
+ _onError ( mockError )
107
107
return mockSubscription
108
108
} )
109
109
110
- events ( filter ) . subscribe ( callback )
110
+ events ( filter ) . subscribe ( onData , onError )
111
111
112
112
// Flush the event loop
113
113
await new Promise ( resolve => setTimeout ( resolve , 0 ) )
114
114
115
- expect ( callback . mock . calls ) . toEqual ( [ [ null , mockError ] ] )
115
+ expect ( onData ) . not . toHaveBeenCalled ( )
116
+ expect ( onError ) . toHaveBeenCalledTimes ( 1 )
117
+ expect ( onError ) . toHaveBeenCalledWith ( mockError )
116
118
} )
117
119
118
120
test ( "fallback to legacy polling if subscriptions are not supported" , async ( ) => {
119
121
const filter = "A"
120
- const callback = jest . fn ( )
122
+ const onData = jest . fn ( )
123
+ const onError = jest . fn ( )
121
124
122
- const mockError = new SubscriptionsNotSupportedError ( )
125
+ const mockNotSupportedError = new SubscriptionsNotSupportedError ( )
123
126
124
- jest . mocked ( subscribe ) . mockImplementation ( ( { onError} ) => {
125
- onError ( mockError )
127
+ jest . mocked ( subscribe ) . mockImplementation ( ( { onError : _onError } ) => {
128
+ _onError ( mockNotSupportedError )
126
129
return mockSubscription
127
130
} )
128
131
129
- const unsubscribe = events ( filter ) . subscribe ( callback )
132
+ const unsubscribe = events ( filter ) . subscribe ( onData , onError )
130
133
131
134
// Flush the event loop
132
135
await new Promise ( resolve => setTimeout ( resolve , 0 ) )
133
136
134
- // Check that the callback was called
135
- expect ( callback ) . toHaveBeenCalledTimes ( 0 )
136
- expect ( subscribe ) . toHaveBeenCalledTimes ( 1 )
137
+ // Check that the error did not propagate to the onError callback
138
+ expect ( onData ) . not . toHaveBeenCalled ( )
139
+ expect ( onError ) . not . toHaveBeenCalled ( )
137
140
138
- // Check that legacy polling was called
141
+ // Check that the legacy subscribe was called
142
+ expect ( mockLegacySubscribeObject . subscribe ) . toHaveBeenCalledTimes ( 1 )
143
+ expect ( mockLegacyUnsubscribe ) . not . toHaveBeenCalled ( )
144
+
145
+ // Check that the legacy events were called with the correct filter
139
146
expect ( legacyEvents ) . toHaveBeenCalledWith ( filter )
140
- expect ( mockLegacySubscribeObject . subscribe ) . toHaveBeenCalledWith ( callback )
147
+ expect ( mockLegacySubscribeObject . subscribe ) . toHaveBeenCalledWith (
148
+ expect . any ( Function )
149
+ )
150
+
151
+ // Mock the legacy subscribe to call the onData callback
152
+ const legacyOnData = (
153
+ mockLegacySubscribeObject . subscribe . mock . calls as any
154
+ ) [ 0 ] [ 0 ] as jest . Mock
155
+ const mockLegacyEvents = [ { type : "A" } , { type : "B" } ] as any [ ]
156
+ mockLegacyEvents . forEach ( event => legacyOnData ( event ) )
157
+
158
+ // Flush the event loop
159
+ await new Promise ( resolve => setTimeout ( resolve , 0 ) )
160
+
161
+ // Check that the onData callback was called with the legacy events
162
+ expect ( onData . mock . calls ) . toEqual ( [ [ { type : "A" } ] , [ { type : "B" } ] ] )
163
+ expect ( onError ) . not . toHaveBeenCalled ( )
164
+ expect ( mockLegacyUnsubscribe ) . not . toHaveBeenCalled ( )
141
165
142
166
// Unsubscribe
143
167
unsubscribe ( )
@@ -151,16 +175,19 @@ describe("events", () => {
151
175
152
176
test ( "emulator should fallback to legacy polling" , async ( ) => {
153
177
const filter = "A"
154
- const callback = jest . fn ( )
178
+ const onData = jest . fn ( )
179
+ const onError = jest . fn ( )
155
180
156
181
jest . mocked ( getChainId ) . mockResolvedValue ( "local" )
157
182
158
- events ( filter ) . subscribe ( callback )
183
+ events ( filter ) . subscribe ( onData , onError )
159
184
160
185
// Flush the event loop
161
186
await new Promise ( resolve => setTimeout ( resolve , 10 ) )
162
187
163
188
expect ( legacyEvents ) . toHaveBeenCalledWith ( filter )
164
- expect ( mockLegacySubscribeObject . subscribe ) . toHaveBeenCalledWith ( callback )
189
+ expect ( mockLegacySubscribeObject . subscribe ) . toHaveBeenCalledWith (
190
+ expect . any ( Function )
191
+ )
165
192
} )
166
193
} )
0 commit comments