Skip to content

Commit 8a730fd

Browse files
committed
Tests now all green
1 parent e87319d commit 8a730fd

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/wallet/substrates/__tests/XDM.test.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,57 @@
1-
/**
2-
* @jest-environment jsdom
3-
*/
4-
51
import XDMSubstrate from '../../../wallet/substrates/XDM'
62
import { WalletError } from '../../../wallet/WalletError'
73
import { Utils } from '../../../primitives/index'
84

95
describe('XDMSubstrate', () => {
10-
let xdmSubstrate
11-
let originalWindow
12-
let eventHandlers: Record<string, (event: any) => void> = {}
6+
let xdmSubstrate: XDMSubstrate
7+
let originalWindow: Window & typeof globalThis | undefined
8+
let addEventListenerMock: jest.Mock
139

1410
beforeEach(() => {
15-
// Save the original window object
1611
originalWindow = global.window
12+
addEventListenerMock = jest.fn()
1713

18-
// Reset event handlers
19-
eventHandlers = {}
20-
21-
// Mock window object
2214
global.window = {
2315
postMessage: jest.fn(),
2416
parent: {
2517
postMessage: jest.fn()
2618
} as unknown as Window,
27-
addEventListener: jest.fn((event, handler) => {
28-
eventHandlers[event] = handler
29-
})
19+
addEventListener: addEventListenerMock,
20+
removeEventListener: jest.fn()
3021
} as unknown as Window & typeof globalThis
3122

3223
jest.spyOn(window.parent, 'postMessage')
3324
})
3425

3526
afterEach(() => {
36-
// Restore the original window object
37-
global.window = originalWindow
27+
global.window = originalWindow as any
3828
jest.restoreAllMocks()
3929
})
4030

31+
const getMessageListener = () => {
32+
const calls = addEventListenerMock.mock.calls
33+
const lastCall = calls[calls.length - 1]
34+
if (!lastCall) {
35+
throw new Error('No message listener registered.')
36+
}
37+
return lastCall[1] as (event: MessageEvent) => void
38+
}
39+
40+
const dispatchMessage = (event: Partial<MessageEvent> & { data: any }) => {
41+
getMessageListener()(event as MessageEvent)
42+
}
43+
4144
describe('constructor', () => {
4245
it('should throw if window is not an object', () => {
43-
delete (global as any).window
46+
;(global as any).window = undefined
4447
expect(() => {
4548
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4649
const _ = new XDMSubstrate()
4750
}).toThrow('The XDM substrate requires a global window object.')
4851
})
4952

5053
it('should throw if window.postMessage is not an object', () => {
51-
delete (global as any).window.postMessage
54+
;(global.window as any).postMessage = undefined
5255
expect(() => {
5356
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5457
const _ = new XDMSubstrate()
@@ -76,7 +79,7 @@ describe('XDMSubstrate', () => {
7679

7780
jest.spyOn(Utils, 'toBase64').mockReturnValue(mockId)
7881

79-
xdmSubstrate.invoke(call, args)
82+
xdmSubstrate.invoke(call as any, args) as any
8083
expect(window.parent.postMessage).toHaveBeenCalledWith(
8184
{
8285
type: 'CWI',
@@ -97,7 +100,7 @@ describe('XDMSubstrate', () => {
97100

98101
jest.spyOn(Utils, 'toBase64').mockReturnValue(mockId)
99102

100-
const invokePromise = xdmSubstrate.invoke(call, args)
103+
const invokePromise = xdmSubstrate.invoke(call as any, args)
101104

102105
// Simulate receiving the message
103106
const event = {
@@ -111,7 +114,7 @@ describe('XDMSubstrate', () => {
111114
isTrusted: true
112115
}
113116

114-
eventHandlers.message(event)
117+
dispatchMessage(event)
115118

116119
const res = await invokePromise
117120

@@ -127,7 +130,7 @@ describe('XDMSubstrate', () => {
127130

128131
jest.spyOn(Utils, 'toBase64').mockReturnValue(mockId)
129132

130-
const invokePromise = xdmSubstrate.invoke(call, args)
133+
const invokePromise = xdmSubstrate.invoke(call as any, args)
131134

132135
// Simulate receiving the message
133136
const event = {
@@ -142,7 +145,7 @@ describe('XDMSubstrate', () => {
142145
isTrusted: true
143146
}
144147

145-
eventHandlers.message(event)
148+
dispatchMessage(event)
146149

147150
await expect(invokePromise).rejects.toThrow(WalletError)
148151
await expect(invokePromise).rejects.toThrow(errorDescription)
@@ -161,7 +164,7 @@ describe('XDMSubstrate', () => {
161164

162165
jest.spyOn(Utils, 'toBase64').mockReturnValue(mockId)
163166

164-
const invokePromise = xdmSubstrate.invoke(call, args)
167+
const invokePromise = xdmSubstrate.invoke(call as any, args)
165168

166169
// Simulate receiving an unrelated message
167170
const event = {
@@ -175,7 +178,7 @@ describe('XDMSubstrate', () => {
175178
isTrusted: true
176179
}
177180

178-
eventHandlers.message(event)
181+
dispatchMessage(event)
179182

180183
// The promise should still be pending
181184
let isResolved = false
@@ -196,7 +199,7 @@ describe('XDMSubstrate', () => {
196199

197200
jest.spyOn(Utils, 'toBase64').mockReturnValue(mockId)
198201

199-
const invokePromise = xdmSubstrate.invoke(call, args)
202+
const invokePromise = xdmSubstrate.invoke(call as any, args)
200203

201204
// Simulate receiving a message with wrong id
202205
const event = {
@@ -210,7 +213,7 @@ describe('XDMSubstrate', () => {
210213
isTrusted: true
211214
}
212215

213-
eventHandlers.message(event)
216+
dispatchMessage(event)
214217

215218
// The promise should still be pending
216219
let isResolved = false
@@ -231,7 +234,7 @@ describe('XDMSubstrate', () => {
231234

232235
jest.spyOn(Utils, 'toBase64').mockReturnValue(mockId)
233236

234-
const invokePromise = xdmSubstrate.invoke(call, args)
237+
const invokePromise = xdmSubstrate.invoke(call as any, args)
235238

236239
// Simulate receiving a message with isTrusted false
237240
const event = {
@@ -245,7 +248,7 @@ describe('XDMSubstrate', () => {
245248
isTrusted: false
246249
}
247250

248-
eventHandlers.message(event)
251+
dispatchMessage(event)
249252

250253
// The promise should still be pending
251254
let isResolved = false
@@ -266,7 +269,7 @@ describe('XDMSubstrate', () => {
266269

267270
jest.spyOn(Utils, 'toBase64').mockReturnValue(mockId)
268271

269-
const invokePromise = xdmSubstrate.invoke(call, args)
272+
const invokePromise = xdmSubstrate.invoke(call as any, args)
270273

271274
// Simulate receiving a message with isInvocation true
272275
const event = {
@@ -280,7 +283,7 @@ describe('XDMSubstrate', () => {
280283
isTrusted: true
281284
}
282285

283-
eventHandlers.message(event)
286+
dispatchMessage(event)
284287

285288
// The promise should still be pending
286289
let isResolved = false
@@ -331,7 +334,7 @@ describe('XDMSubstrate', () => {
331334
isTrusted: true
332335
}
333336

334-
eventHandlers.message(event)
337+
dispatchMessage(event)
335338

336339
const res = await invokePromise
337340
expect(res).toEqual(result)
@@ -371,7 +374,7 @@ describe('XDMSubstrate', () => {
371374
isTrusted: true
372375
}
373376

374-
eventHandlers.message(event)
377+
dispatchMessage(event)
375378

376379
await expect(invokePromise).rejects.toThrow(WalletError)
377380
await expect(invokePromise).rejects.toThrow(errorDescription)

0 commit comments

Comments
 (0)