1- import { TimeoutError } from '@deephaven/utils' ;
2- import { makeMessage , requestParentResponse } from './MessageUtils' ;
1+ import {
2+ makeMessage ,
3+ makeResponse ,
4+ Message ,
5+ requestParentResponse ,
6+ } from './MessageUtils' ;
37
4- // describe ('Throws exception if called on a window without parent', async () => {
5- // expect(await requestParentResponse<string> ('request', 'response' )).toThrow();
6- // });
7-
8- jest . useFakeTimers ( ) ;
8+ it ( 'Throws an exception if called on a window without parent' , async ( ) => {
9+ await expect ( requestParentResponse ( 'request' ) ) . rejects . toThrow (
10+ 'window.opener is null, unable to send request.'
11+ ) ;
12+ } ) ;
913
1014describe ( 'requestParentResponse' , ( ) => {
11- const mockPostMessage = jest . fn ( ) ;
1215 let addListenerSpy : jest . SpyInstance ;
1316 let removeListenerSpy : jest . SpyInstance ;
1417 let listenerCallback ;
18+ let messageId ;
19+ const mockPostMessage = jest . fn ( ( data : Message < unknown > ) => {
20+ messageId = data . id ;
21+ } ) ;
1522 const originalWindowOpener = window . opener ;
1623 beforeEach ( ( ) => {
1724 addListenerSpy = jest
@@ -27,12 +34,13 @@ describe('requestParentResponse', () => {
2734 removeListenerSpy . mockRestore ( ) ;
2835 mockPostMessage . mockClear ( ) ;
2936 window . opener = originalWindowOpener ;
37+ messageId = undefined ;
3038 } ) ;
3139
3240 it ( 'Posts message to parent and subscribes to response' , async ( ) => {
33- requestParentResponse < string > ( 'request' , 'response ') ;
41+ requestParentResponse ( 'request' ) ;
3442 expect ( mockPostMessage ) . toHaveBeenCalledWith (
35- expect . objectContaining ( { message : 'request' } ) ,
43+ expect . objectContaining ( makeMessage ( 'request' , messageId ) ) ,
3644 '*'
3745 ) ;
3846 expect ( addListenerSpy ) . toHaveBeenCalledWith (
@@ -43,31 +51,32 @@ describe('requestParentResponse', () => {
4351
4452 it ( 'Resolves with the payload from the parent window response and unsubscribes' , async ( ) => {
4553 const PAYLOAD = 'PAYLOAD' ;
46- const promise = requestParentResponse < string > ( 'request' , 'response ') ;
54+ const promise = requestParentResponse ( 'request' ) ;
4755 listenerCallback ( {
48- data : makeMessage < string > ( 'response' , PAYLOAD ) ,
56+ data : makeResponse ( messageId , PAYLOAD ) ,
4957 } ) ;
5058 const result = await promise ;
5159 expect ( result ) . toBe ( PAYLOAD ) ;
5260 expect ( removeListenerSpy ) . toHaveBeenCalledWith ( 'message' , listenerCallback ) ;
5361 } ) ;
5462
55- it ( 'Rejects on time out' , async ( ) => {
56- const promise = requestParentResponse < string > ( 'request' , 'response' ) ;
57- expect ( removeListenerSpy ) . not . toHaveBeenCalled ( ) ;
63+ it ( 'Ignores unrelated response, rejects on timeout' , async ( ) => {
64+ jest . useFakeTimers ( ) ;
65+ const promise = requestParentResponse ( 'request' ) ;
66+ listenerCallback ( {
67+ data : makeMessage ( 'wrong-id' ) ,
68+ } ) ;
5869 jest . runOnlyPendingTimers ( ) ;
59- expect ( removeListenerSpy ) . toHaveBeenCalled ( ) ;
60- await expect ( promise ) . rejects . toThrow ( TimeoutError ) ;
70+ await expect ( promise ) . rejects . toThrow ( 'Request timed out' ) ;
71+ jest . useRealTimers ( ) ;
6172 } ) ;
6273
63- // it('Ignores unrelated messages', async () => {
64- // window.opener = { postMessage: mockPostMessage };
65- // const PAYLOAD = 'PAYLOAD';
66- // const promise = requestParentResponse<string>('request', 'response');
67- // listenerCallback({
68- // data: makeMessage<string>('response', `TEST_${messageId}`, PAYLOAD),
69- // });
70- // const result = await promise;
71- // expect(result).toBe(PAYLOAD);
72- // });
74+ it ( 'Times out if no response' , async ( ) => {
75+ jest . useFakeTimers ( ) ;
76+ const promise = requestParentResponse ( 'request' ) ;
77+ jest . runOnlyPendingTimers ( ) ;
78+ expect ( removeListenerSpy ) . toHaveBeenCalled ( ) ;
79+ await expect ( promise ) . rejects . toThrow ( 'Request timed out' ) ;
80+ jest . useRealTimers ( ) ;
81+ } ) ;
7382} ) ;
0 commit comments