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

Commit fabd60e

Browse files
Hubert KosterHubert Koster
authored andcommitted
chore: adding test coverage
1 parent c807289 commit fabd60e

File tree

7 files changed

+87
-11
lines changed

7 files changed

+87
-11
lines changed

src/features/Apiexplorer/RequestJSONBox/__tests__/RequestJsonBox.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,39 @@ import '@testing-library/jest-dom';
33
import { cleanup, render, screen } from '@testing-library/react';
44
import { TSocketEndpointNames } from '@site/src/configs/websocket/types';
55
import useAuthContext from '@site/src/hooks/useAuthContext';
6+
import useSubscription from '@site/src/hooks/useSubscription';
7+
import useWS from '@site/src/hooks/useWs';
68
import { IAuthContext } from '@site/src/contexts/auth/auth.context';
79
import userEvent from '@testing-library/user-event';
810
import RequestJSONBox from '..';
911

12+
const fakeHookObject = {
13+
clear: jest.fn(),
14+
send: jest.fn(),
15+
full_response: {
16+
tick: 1,
17+
echo_req: { tick: 1 },
18+
},
19+
};
20+
1021
jest.mock('@site/src/hooks/useAuthContext');
1122

1223
const mockUseAuthContext = useAuthContext as jest.MockedFunction<() => Partial<IAuthContext>>;
1324

25+
jest.mock('@site/src/hooks/useSubscription');
26+
27+
const mockUseSubscription = useSubscription as jest.MockedFunction<
28+
() => Partial<ReturnType<typeof useSubscription>>
29+
>;
30+
31+
mockUseSubscription.mockImplementation(() => fakeHookObject);
32+
33+
jest.mock('@site/src/hooks/useWs');
34+
35+
const mockuseWS = useWS as jest.MockedFunction<() => Partial<ReturnType<typeof useWS>>>;
36+
37+
mockuseWS.mockImplementation(() => fakeHookObject);
38+
1439
describe('RequestResponseRenderer', () => {
1540
const mockProps = {
1641
handleChange: jest.fn(),

src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/__tests__/PlaygroundSection.test.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,49 @@ import React from 'react';
22
import PlaygroundSection from '..';
33
import { render, screen } from '@testing-library/react';
44

5+
const fake_full_response = {
6+
msg_type: 'test',
7+
};
8+
59
describe('PlaygroundSection', () => {
610
it('should render the loader', async () => {
7-
render(<PlaygroundSection loader response_state={false} data={null} error={null} />);
11+
render(
12+
<PlaygroundSection
13+
loader
14+
response_state={false}
15+
full_response={fake_full_response}
16+
error={null}
17+
name=''
18+
/>,
19+
);
820
const loader = await screen.findByTestId('circles-loading');
921
expect(loader).toBeVisible();
1022
});
1123

1224
it('should render the PlaygroundConsole', async () => {
13-
render(<PlaygroundSection loader={false} response_state={false} data={null} error={null} />);
25+
render(
26+
<PlaygroundSection
27+
loader={false}
28+
response_state={false}
29+
full_response={fake_full_response}
30+
error={null}
31+
name=''
32+
/>,
33+
);
1434
const playground_section = await screen.findByTestId('dt_playground_section');
1535
expect(playground_section).toBeVisible();
1636
});
1737

1838
it('should render the ReactJson', async () => {
19-
render(<PlaygroundSection loader={false} response_state data={null} error={null} />);
39+
render(
40+
<PlaygroundSection
41+
loader={false}
42+
response_state
43+
full_response={fake_full_response}
44+
error={null}
45+
name=''
46+
/>,
47+
);
2048

2149
const playground_section = await screen.findByTestId('dt_playground_section');
2250
expect(playground_section).toBeVisible();

src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/index.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ const ReactJson = React.lazy(() => import('react-json-view'));
1212
type TPlaygroundSection<T extends TSocketEndpointNames> = {
1313
loader: boolean;
1414
response_state: boolean;
15-
data: TSocketResponseData<T>;
15+
full_response: TSocketResponseData<T>;
1616
error: unknown;
17+
name: string;
1718
};
1819

1920
const PlaygroundSection = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNames>({
2021
loader,
2122
response_state,
22-
data,
23+
full_response,
2324
error,
25+
name,
2426
}: TPlaygroundSection<T>) => {
2527
if (loader) return <Loader />;
2628

29+
const key = full_response['msg_type'] ?? name;
30+
const echo_req_json = {
31+
echo_req: full_response['echo_req'],
32+
msg_type: full_response['msg_type'],
33+
req_id: full_response['req_id'],
34+
};
35+
const main_object_json = { [key]: full_response[key] };
36+
2737
return (
2838
<div
2939
id='playground-console'
@@ -34,8 +44,11 @@ const PlaygroundSection = <T extends TSocketEndpointNames | TSocketSubscribableE
3444
<React.Fragment>
3545
<Suspense fallback={<Loader />}>
3646
<div data-testid='dt_json_view'>
37-
{data !== null ? (
38-
<ReactJson src={{ data }} theme='tube' />
47+
{full_response !== null ? (
48+
<div className={styles.reactJsonContainer}>
49+
<ReactJson src={echo_req_json} theme='tube' />
50+
<ReactJson src={main_object_json} theme='tube' />
51+
</div>
3952
) : (
4053
<ReactJson src={{ error }} theme='tube' />
4154
)}

src/features/Apiexplorer/RequestResponseRenderer/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function RequestResponseRenderer<T extends TSocketEndpointNames>({
2121
}: IResponseRendererProps<T>) {
2222
const { is_logged_in } = useAuthContext();
2323
const { disableSendRequest } = useDisableSendRequest();
24-
const { data, is_loading, send, clear, error } = useWS<T>(name);
24+
const { full_response, is_loading, send, clear, error } = useWS<T>(name);
2525
const [toggle_modal, setToggleModal] = useState(false);
2626
const [response_state, setResponseState] = useState(false);
2727

@@ -66,8 +66,9 @@ function RequestResponseRenderer<T extends TSocketEndpointNames>({
6666
<PlaygroundSection
6767
loader={is_loading}
6868
response_state={response_state}
69-
data={data}
69+
full_response={full_response}
7070
error={error}
71+
name={name}
7172
/>
7273
)}
7374
</div>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ mockUseSubscription.mockImplementation(() => ({
5555
subscribe: mockSubscribe,
5656
unsubscribe: mockUnsubscribe,
5757
error: 'random error',
58+
full_response: {
59+
tick: 1,
60+
echo_req: { tick: 1 },
61+
},
5862
}));
5963

6064
const request_data = `{

src/features/Apiexplorer/SubscribeRenderer/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
2121
}: IResponseRendererProps<T>) {
2222
const { is_logged_in } = useAuthContext();
2323
const { disableSendRequest } = useDisableSendRequest();
24-
const { data, is_loading, subscribe, unsubscribe, error } = useSubscription<T>(name);
24+
const { full_response, is_loading, subscribe, unsubscribe, error } = useSubscription<T>(name);
2525
const [response_state, setResponseState] = useState(false);
2626
const [toggle_modal, setToggleModal] = useState(false);
2727

@@ -66,8 +66,9 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
6666
<PlaygroundSection
6767
loader={is_loading}
6868
response_state={response_state}
69-
data={data}
69+
full_response={full_response}
7070
error={error}
71+
name={name}
7172
/>
7273
)}
7374
</div>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const mockClear = jest.fn();
3232
mockuseWS.mockImplementation(() => ({
3333
clear: mockClear,
3434
send: jest.fn(),
35+
full_response: {
36+
tick: 1,
37+
echo_req: { tick: 1 },
38+
},
3539
}));
3640

3741
jest.mock('@site/src/hooks/useDynamicImportJSON');

0 commit comments

Comments
 (0)