1+ import React from 'react' ;
12import '@testing-library/jest-dom' ;
3+ import userEvent from '@testing-library/user-event' ;
24import { renderHook } from '@testing-library/react-hooks' ;
35import { act } from 'react-dom/test-utils' ;
46import useDynamicImportJSON from '..' ;
7+ import { cleanup , render , screen } from '@testing-library/react' ;
58
69jest . mock ( '@docusaurus/router' , ( ) => ( {
710 useLocation : ( ) => ( {
@@ -20,6 +23,7 @@ describe('useDynamicImportJSON', () => {
2023
2124 afterEach ( ( ) => {
2225 jest . clearAllMocks ( ) ;
26+ cleanup ( ) ;
2327 } ) ;
2428
2529 it ( 'should populate text data with the correct values' , ( ) => {
@@ -32,31 +36,74 @@ describe('useDynamicImportJSON', () => {
3236 } ) ;
3337 } ) ;
3438
39+ it ( 'should be able to call handleTextAreaInput when typing in a textarea' , async ( ) => {
40+ const spyHandleInputFunction = jest . spyOn ( result . current , 'handleTextAreaInput' ) ;
41+
42+ render ( < textarea placeholder = 'testtextarea' onChange = { result . current . handleTextAreaInput } /> ) ;
43+
44+ const textarea = screen . getByPlaceholderText ( 'testtextarea' ) ;
45+ expect ( textarea ) . toBeVisible ( ) ;
46+
47+ await userEvent . type ( textarea , 'test123' ) ;
48+ expect ( spyHandleInputFunction ) . toHaveBeenCalled ( ) ;
49+ } ) ;
50+
3551 it ( 'should have the correct hash value in the URL on selection of an api call' , ( ) => {
3652 const location = require ( '@docusaurus/router' ) . useLocation ( ) ;
3753 const url = location . hash ;
3854 expect ( url ) . toMatch ( 'active_symbols' ) ;
3955 } ) ;
4056
41- it ( 'should check for change in hash value and update text data accordingly' , ( ) => {
42- act ( ( ) => {
43- jest . mock ( '@docusaurus/router' , ( ) => ( {
44- useLocation : ( ) => ( {
45- pathname : '/api-explorer#active_symbols' ,
46- hash : '#active_symbol' ,
47- } ) ,
48- useHistory : ( ) => ( {
49- push : jest . fn ( ) ,
50- } ) ,
51- } ) ) ;
52- const mockEvent = {
53- currentTarget : {
54- value : 'active_symbols' ,
57+ it ( 'should check for change in hash value and update text data accordingly' , async ( ) => {
58+ jest . mock ( '@site/src/utils/playground_requests' , ( ) => ( {
59+ playground_requests : [
60+ {
61+ name : 'active_symbols' ,
62+ title : 'Active Symbols' ,
63+ body : {
64+ active_symbols : 'brief' ,
65+ product_type : 'basic' ,
66+ } ,
5567 } ,
56- preventDefault : jest . fn ( ) ,
57- } ;
68+ ] ,
69+ } ) ) ;
70+
71+ jest . mock ( '@docusaurus/router' , ( ) => ( {
72+ useLocation : ( ) => ( {
73+ pathname : '/api-explorer#active_symbols' ,
74+ hash : '#active_symbol' ,
75+ } ) ,
76+ useHistory : ( ) => ( {
77+ push : jest . fn ( ) ,
78+ } ) ,
79+ } ) ) ;
80+
81+ const mockEvent = {
82+ currentTarget : {
83+ value : 'active_symbols' ,
84+ } ,
85+ preventDefault : jest . fn ( ) ,
86+ } ;
87+
88+ const spyHandleSelectChange = jest . spyOn ( result . current , 'handleSelectChange' ) ;
89+
90+ const mockHandleSelectChange = ( ) =>
5891 result . current . handleSelectChange ( mockEvent , 'active_symbols' ) ;
59- } ) ;
92+
93+ render (
94+ < div >
95+ < button className = 'simulated_option' onClick = { ( ) => mockHandleSelectChange ( ) } >
96+ Active Symbols
97+ </ button >
98+ </ div > ,
99+ ) ;
100+
101+ const option = screen . getByRole ( 'button' , { name : 'Active Symbols' } ) ;
102+
103+ await userEvent . click ( option ) ;
104+
105+ expect ( spyHandleSelectChange ) . toHaveBeenCalled ( ) ;
106+
60107 expect ( result . current . text_data ) . toEqual ( {
61108 request : '{\n "active_symbols": "brief",\n "product_type": "basic"\n}' ,
62109 selected_value : 'Active Symbols' ,
0 commit comments