Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 0dcb432

Browse files
authored
Merge pull request #151 from hubert-deriv/usedynamicimport_coverage
hubert / usedynamicimport coverage
2 parents 0bc8439 + 77d203d commit 0dcb432

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

src/features/Apiexplorer/__tests__/ApiExplorer.test.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import userEvent from '@testing-library/user-event';
55
import useWS from '@site/src/hooks/useWs';
66
import useAuthContext from '@site/src/hooks/useAuthContext';
77
import useDynamicImportJSON from '@site/src/hooks/useDynamicImportJSON';
8-
import { cleanup, render, screen } from '@testing-library/react';
8+
import { cleanup, render, screen, act } from '@testing-library/react';
99
import { IAuthContext } from '@site/src/contexts/auth/auth.context';
10-
import { act } from 'react-dom/test-utils';
1110

1211
jest.mock('@docusaurus/router', () => ({
1312
useLocation: () => ({
@@ -44,6 +43,8 @@ const mockUseDynamicImportJSON = useDynamicImportJSON as jest.MockedFunction<
4443
() => Partial<ReturnType<typeof useDynamicImportJSON>>
4544
>;
4645

46+
const mockHandleSelectChange = jest.fn();
47+
4748
mockUseDynamicImportJSON.mockImplementation(() => ({
4849
request_info: {
4950
auth_required: 1,
@@ -56,6 +57,7 @@ mockUseDynamicImportJSON.mockImplementation(() => ({
5657
title: 'this is a test title',
5758
},
5859
setSelected: jest.fn(),
60+
handleTextAreaInput: mockHandleSelectChange,
5961
handleSelectChange: jest.fn(),
6062
text_data: {
6163
name: null,
@@ -149,6 +151,13 @@ describe('ApiExplorerFeatures', () => {
149151
await userEvent.click(close_button);
150152
expect(dialog).not.toBeVisible();
151153
});
154+
155+
it('should change the text when writing in the textbox', async () => {
156+
const json_box = screen.getByPlaceholderText('Request JSON');
157+
expect(json_box).toBeVisible();
158+
await userEvent.type(json_box, 'test123');
159+
expect(mockHandleSelectChange).toHaveBeenCalled();
160+
});
152161
});
153162

154163
describe('Logged in', () => {

src/hooks/useDynamicImportJSON/__tests__/useDynamicImport.test.tsx

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import React from 'react';
12
import '@testing-library/jest-dom';
3+
import userEvent from '@testing-library/user-event';
24
import { renderHook } from '@testing-library/react-hooks';
35
import { act } from 'react-dom/test-utils';
46
import useDynamicImportJSON from '..';
7+
import { cleanup, render, screen } from '@testing-library/react';
58

69
jest.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',

src/hooks/useDynamicImportJSON/index.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ const useDynamicImportJSON = () => {
6767
},
6868
[setRequestInfo, setResponseInfo],
6969
);
70-
useEffect(() => {
71-
const hash_value = hash.split('#')[1];
72-
const request_body = playground_requests.find((el) => el.name === hash_value);
73-
const is_not_placeholder = text_data.selected_value === request_body?.name;
74-
if (is_not_placeholder) dynamicImportJSON(text_data.selected_value);
75-
}, [dynamicImportJSON, hash, text_data.selected_value]);
7670

7771
useEffect(() => {
7872
const hash_value = hash.split('#')[1];

0 commit comments

Comments
 (0)