11import renderer from 'react-test-renderer' ;
2- import { render , waitFor } from '@testing-library/react' ;
3- import userEvent from '@testing-library/user-event ' ;
4- import IFrame from '../core/src ' ;
2+ import { render , waitFor , renderHook , act } from '@testing-library/react' ;
3+ import IFrame , { useFrame , FrameContext } from '../core/src ' ;
4+ import React from 'react ' ;
55
66it ( 'renders <IFrame /> test case' , ( ) => {
77 const component = renderer . create (
@@ -25,4 +25,61 @@ it('renders <IFrame /> test case', async () => {
2525 await waitFor ( ( ) => {
2626 expect ( container . innerHTML ) . toEqual ( `<iframe srcdoc="<!DOCTYPE html><html><head></head><body></body></html>"></iframe>` ) ;
2727 } )
28+ } ) ;
29+
30+ it ( 'renders useFrame test case' , async ( ) => {
31+ const { result } = renderHook ( ( ) => useFrame ( ) ) ;
32+ act ( ( ) => {
33+ expect ( Object . keys ( result . current ) ) . toEqual ( [ "document" , "window" ] ) ;
34+ } ) ;
35+ } ) ;
36+
37+
38+ it ( 'renders ref test case' , async ( ) => {
39+ const ref = React . createRef < HTMLIFrameElement > ( ) ;
40+ const { container } = render (
41+ < IFrame ref = { ref } >
42+ < h1 > Hello World!</ h1 >
43+ </ IFrame >
44+ ) ;
45+ await waitFor ( ( ) => {
46+ expect ( ref . current instanceof HTMLIFrameElement ) . toBeTruthy ( ) ;
47+ expect ( container . innerHTML ) . toEqual ( `<iframe srcdoc="<!DOCTYPE html><html><head></head><body></body></html>"></iframe>` ) ;
48+ } )
49+ } ) ;
50+
51+
52+ it ( 'renders <IFrame src="https://wangchujiang.com/" /> test case' , async ( ) => {
53+ const { container } = render (
54+ < IFrame src = "https://wangchujiang.com/" >
55+ < h1 > Hello World!</ h1 >
56+ </ IFrame >
57+ ) ;
58+ await waitFor ( ( ) => {
59+ expect ( container . innerHTML ) . toEqual ( `<iframe src="https://wangchujiang.com/"></iframe>` ) ;
60+ } )
61+ } ) ;
62+
63+
64+ it ( 'renders <IFrame head="..." /> test case' , async ( ) => {
65+ const head = (
66+ < style > { `body { background: red; }` } </ style >
67+ ) ;
68+ const ref = React . createRef < HTMLIFrameElement > ( ) ;
69+ const { container } = render (
70+ < IFrame ref = { ref } head = { head } >
71+ < FrameContext . Consumer >
72+ { ( { document, window } ) => {
73+ return (
74+ < div >
75+ < div > Hello World!</ div >
76+ </ div >
77+ )
78+ } }
79+ </ FrameContext . Consumer >
80+ </ IFrame >
81+ ) ;
82+ await waitFor ( ( ) => {
83+ expect ( container . innerHTML ) . toEqual ( `<iframe srcdoc="<!DOCTYPE html><html><head></head><body></body></html>"></iframe>` ) ;
84+ } ) ;
2885} ) ;
0 commit comments