1
1
import React from 'react' ;
2
2
import { getConfig } from '@edx/frontend-platform' ;
3
3
import { act , renderHook } from '@testing-library/react' ;
4
- import { useKeyedState } from '@edx/react-unit-test-utils' ;
5
4
import { logError } from '@edx/frontend-platform/logging' ;
6
- import { iframeMessageTypes , iframeStateKeys } from '../../../constants' ;
7
- import { useIframeBehavior } from '../useIframeBehavior' ;
5
+ import { iframeMessageTypes } from '../../../constants' ;
6
+ import { useIframeBehavior , iframeBehaviorState } from '../useIframeBehavior' ;
8
7
import { useLoadBearingHook } from '../useLoadBearingHook' ;
9
8
10
9
jest . useFakeTimers ( ) ;
11
10
12
- jest . mock ( '@edx/react-unit-test-utils' , ( ) => ( {
13
- useKeyedState : jest . fn ( ) ,
14
- } ) ) ;
15
-
16
11
jest . mock ( '@edx/frontend-platform/logging' , ( ) => ( {
17
12
logError : jest . fn ( ) ,
18
13
} ) ) ;
@@ -27,20 +22,10 @@ describe('useIframeBehavior', () => {
27
22
const iframeRef = { current : { contentWindow : null } as HTMLIFrameElement } ;
28
23
29
24
beforeEach ( ( ) => {
30
- ( useKeyedState as jest . Mock ) . mockImplementation ( ( key , initialValue ) => {
31
- switch ( key ) {
32
- case iframeStateKeys . iframeHeight :
33
- return [ 0 , setIframeHeight ] ;
34
- case iframeStateKeys . hasLoaded :
35
- return [ false , setHasLoaded ] ;
36
- case iframeStateKeys . showError :
37
- return [ false , setShowError ] ;
38
- case iframeStateKeys . windowTopOffset :
39
- return [ null , setWindowTopOffset ] ;
40
- default :
41
- return [ initialValue , jest . fn ( ) ] ;
42
- }
43
- } ) ;
25
+ jest . spyOn ( iframeBehaviorState , 'iframeHeight' ) . mockImplementation ( ( ) => [ 0 , setIframeHeight ] ) ;
26
+ jest . spyOn ( iframeBehaviorState , 'hasLoaded' ) . mockImplementation ( ( ) => [ false , setHasLoaded ] ) ;
27
+ jest . spyOn ( iframeBehaviorState , 'showError' ) . mockImplementation ( ( ) => [ false , setShowError ] ) ;
28
+ jest . spyOn ( iframeBehaviorState , 'windowTopOffset' ) . mockImplementation ( ( ) => [ null , setWindowTopOffset ] ) ;
44
29
45
30
window . scrollTo = jest . fn ( ( x : number | ScrollToOptions , y ?: number ) : void => {
46
31
const scrollY = typeof x === 'number' ? y : ( x as ScrollToOptions ) . top || 0 ;
@@ -58,14 +43,7 @@ describe('useIframeBehavior', () => {
58
43
59
44
it ( 'scrolls to previous position on video fullscreen exit' , ( ) => {
60
45
const mockWindowTopOffset = 100 ;
61
-
62
- ( useKeyedState as jest . Mock ) . mockImplementation ( ( key ) => {
63
- if ( key === iframeStateKeys . windowTopOffset ) {
64
- return [ mockWindowTopOffset , setWindowTopOffset ] ;
65
- }
66
- return [ null , jest . fn ( ) ] ;
67
- } ) ;
68
-
46
+ jest . spyOn ( iframeBehaviorState , 'windowTopOffset' ) . mockImplementation ( ( ) => [ mockWindowTopOffset , setWindowTopOffset ] ) ;
69
47
renderHook ( ( ) => useIframeBehavior ( { id, iframeUrl, iframeRef } ) ) ;
70
48
71
49
const message = {
0 commit comments