77} from '../../../__mocks__' ;
88import { initializeMocks , makeWrapper } from '../../../testUtils' ;
99import { getClipboardUrl } from '../../data/api' ;
10- import useClipboard from './useClipboard' ;
10+ import useClipboard , { _testingOverrideBroadcastChannel } from './useClipboard' ;
1111
1212initializeMocks ( ) ;
1313
@@ -16,13 +16,14 @@ let mockShowToast: jest.Mock;
1616
1717const unitId = 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@vertical_0270f6de40fc' ;
1818const xblockId = 'block-v1:edX+DemoX+Demo_Course+type@html+block@030e35c4756a4ddc8d40b95fbbfff4d4' ;
19+
20+ let broadcastMockListener : ( x : unknown ) => void | undefined ;
1921const clipboardBroadcastChannelMock = {
20- postMessage : jest . fn ( ) ,
21- close : jest . fn ( ) ,
22- onmessage : jest . fn ( ) ,
22+ postMessage : ( message : unknown ) => { broadcastMockListener ( message ) ; } ,
23+ addEventListener : ( _eventName : string , handler : typeof broadcastMockListener ) => { broadcastMockListener = handler ; } ,
24+ removeEventListener : jest . fn ( ) ,
2325} ;
24-
25- ( global as any ) . BroadcastChannel = jest . fn ( ( ) => clipboardBroadcastChannelMock ) ;
26+ _testingOverrideBroadcastChannel ( clipboardBroadcastChannelMock as any ) ;
2627
2728describe ( 'useClipboard' , ( ) => {
2829 beforeEach ( async ( ) => {
@@ -88,14 +89,14 @@ describe('useClipboard', () => {
8889 describe ( 'broadcast channel message handling' , ( ) => {
8990 it ( 'updates states correctly on receiving a broadcast message' , async ( ) => {
9091 const { result, rerender } = renderHook ( ( ) => useClipboard ( true ) , { wrapper : makeWrapper ( ) } ) ;
91- clipboardBroadcastChannelMock . onmessage ( { data : clipboardUnit } ) ;
92+ clipboardBroadcastChannelMock . postMessage ( { data : clipboardUnit } ) ;
9293
9394 rerender ( ) ;
9495
9596 expect ( result . current . showPasteUnit ) . toBe ( true ) ;
9697 expect ( result . current . showPasteXBlock ) . toBe ( false ) ;
9798
98- clipboardBroadcastChannelMock . onmessage ( { data : clipboardXBlock } ) ;
99+ clipboardBroadcastChannelMock . postMessage ( { data : clipboardXBlock } ) ;
99100 rerender ( ) ;
100101
101102 expect ( result . current . showPasteUnit ) . toBe ( false ) ;
0 commit comments