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

Commit eead7d5

Browse files
authored
Merge pull request #134 from hubert-deriv/response_object_incomplete3
Hubert / Response object incomplete
2 parents de8cd75 + e786631 commit eead7d5

File tree

12 files changed

+140
-33
lines changed

12 files changed

+140
-33
lines changed

src/features/Apiexplorer/LoginDialog/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useCallback } from 'react';
2-
import { Modal } from '@deriv/ui';
2+
import { Modal, Button } from '@deriv/ui';
33
import useLoginUrl from '@site/src/hooks/useLoginUrl';
4-
import { Button } from '@deriv/ui';
54
import styles from './LoginDialog.module.scss';
65

76
type TLoginDialog = {

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(),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@use 'src/styles/utility' as *;
2+
3+
.reactJsonContainer {
4+
display: flex;
5+
flex-direction: column;
6+
gap: rem(2);
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
import styles from './JsonData.module.scss';
3+
import {
4+
TSocketEndpointNames,
5+
TSocketSubscribableEndpointNames,
6+
TSocketResponse,
7+
} from '@site/src/configs/websocket/types';
8+
9+
const ReactJson = React.lazy(() => import('react-json-view'));
10+
11+
type TJsonData<T extends TSocketEndpointNames> = {
12+
full_response: TSocketResponse<T>;
13+
error: unknown;
14+
name: string;
15+
};
16+
17+
const JsonData = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNames>({
18+
full_response,
19+
error,
20+
}: TJsonData<T>) => {
21+
return (
22+
<React.Fragment>
23+
{full_response !== null ? (
24+
<div className={styles.reactJsonContainer}>
25+
<ReactJson src={full_response.echo_req} theme='tube' />
26+
<ReactJson src={full_response} theme='tube' />
27+
</div>
28+
) : (
29+
<ReactJson src={{ error }} theme='tube' />
30+
)}
31+
</React.Fragment>
32+
);
33+
};
34+
35+
export default JsonData;

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,31 @@ import { render, screen } from '@testing-library/react';
44

55
describe('PlaygroundSection', () => {
66
it('should render the loader', async () => {
7-
render(<PlaygroundSection loader response_state={false} data={null} error={null} />);
7+
render(
8+
<PlaygroundSection loader response_state={false} full_response={null} error={null} name='' />,
9+
);
810
const loader = await screen.findByTestId('circles-loading');
911
expect(loader).toBeVisible();
1012
});
1113

1214
it('should render the PlaygroundConsole', async () => {
13-
render(<PlaygroundSection loader={false} response_state={false} data={null} error={null} />);
15+
render(
16+
<PlaygroundSection
17+
loader={false}
18+
response_state={false}
19+
full_response={null}
20+
error={null}
21+
name=''
22+
/>,
23+
);
1424
const playground_section = await screen.findByTestId('dt_playground_section');
1525
expect(playground_section).toBeVisible();
1626
});
1727

1828
it('should render the ReactJson', async () => {
19-
render(<PlaygroundSection loader={false} response_state data={null} error={null} />);
29+
render(
30+
<PlaygroundSection loader={false} response_state full_response={null} error={null} name='' />,
31+
);
2032

2133
const playground_section = await screen.findByTestId('dt_playground_section');
2234
expect(playground_section).toBeVisible();

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import React, { Suspense } from 'react';
22
import {
33
TSocketEndpointNames,
4-
TSocketResponseData,
54
TSocketSubscribableEndpointNames,
5+
TSocketResponse,
66
} from '@site/src/configs/websocket/types';
7+
import JsonData from './JsonData';
78
import Loader from '@site/src/components/Loader';
89
import styles from './PlaygroundSection.module.scss';
910

10-
const ReactJson = React.lazy(() => import('react-json-view'));
11-
1211
type TPlaygroundSection<T extends TSocketEndpointNames> = {
1312
loader: boolean;
1413
response_state: boolean;
15-
data: TSocketResponseData<T>;
14+
full_response: TSocketResponse<T>;
1615
error: unknown;
16+
name: string;
1717
};
1818

1919
const PlaygroundSection = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNames>({
2020
loader,
2121
response_state,
22-
data,
22+
full_response,
2323
error,
24+
name,
2425
}: TPlaygroundSection<T>) => {
2526
if (loader) return <Loader />;
2627

@@ -34,11 +35,7 @@ const PlaygroundSection = <T extends TSocketEndpointNames | TSocketSubscribableE
3435
<React.Fragment>
3536
<Suspense fallback={<Loader />}>
3637
<div data-testid='dt_json_view'>
37-
{data !== null ? (
38-
<ReactJson src={{ data }} theme='tube' />
39-
) : (
40-
<ReactJson src={{ error }} theme='tube' />
41-
)}
38+
<JsonData full_response={full_response} name={name} error={error} />
4239
</div>
4340
</Suspense>
4441
</React.Fragment>

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: 12 additions & 2 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 = `{
@@ -96,13 +100,19 @@ describe('SubscribeRenderer', () => {
96100
expect(mockUnsubscribe).toBeCalledTimes(1);
97101
});
98102

99-
it('should throw an error if incorrect json is being parsed', () => {
103+
it('should throw an error if incorrect json is being parsed', async () => {
100104
const consoleOutput = [];
101105
const mockedError = (output) => consoleOutput.push(output);
102106
console.error = mockedError;
103107

104108
render(<SubscribeRenderer name='ticks' auth={1} reqData={'asdawefaewf3232'} />);
105109

106-
expect(consoleOutput[0]).toEqual('something went wrong when parsing the json data: ');
110+
const send_request_button = screen.getByRole('button', { name: 'Send Request' });
111+
112+
await userEvent.click(send_request_button);
113+
114+
expect(consoleOutput[0]).toEqual(
115+
'Could not parse the JSON data while trying to send the request: ',
116+
);
107117
});
108118
});

src/features/Apiexplorer/SubscribeRenderer/index.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React, { useState, useCallback, useEffect } from 'react';
2-
import { TSocketSubscribableEndpointNames } from '@site/src/configs/websocket/types';
2+
import {
3+
TSocketSubscribableEndpointNames,
4+
TSocketRequestProps,
5+
} from '@site/src/configs/websocket/types';
36
import { Button } from '@deriv/ui';
47
import styles from '../RequestJSONBox/RequestJSONBox.module.scss';
58
import useAuthContext from '@site/src/hooks/useAuthContext';
@@ -21,7 +24,7 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
2124
}: IResponseRendererProps<T>) {
2225
const { is_logged_in } = useAuthContext();
2326
const { disableSendRequest } = useDisableSendRequest();
24-
const { data, is_loading, subscribe, unsubscribe, error } = useSubscription<T>(name);
27+
const { full_response, is_loading, subscribe, unsubscribe, error } = useSubscription<T>(name);
2528
const [response_state, setResponseState] = useState(false);
2629
const [toggle_modal, setToggleModal] = useState(false);
2730

@@ -31,17 +34,21 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
3134
}
3235
}, [error]);
3336

34-
let json_data;
35-
try {
36-
json_data = JSON.parse(reqData);
37-
} catch (error) {
38-
json_data = '';
39-
console.error('something went wrong when parsing the json data: ', error);
40-
}
37+
const parseRequestJSON = () => {
38+
let json_data: TSocketRequestProps<T> extends never ? undefined : TSocketRequestProps<T>;
39+
40+
try {
41+
json_data = JSON.parse(reqData);
42+
} catch (error) {
43+
console.error('Could not parse the JSON data while trying to send the request: ', error);
44+
}
45+
46+
return json_data;
47+
};
4148

4249
const handleClick = useCallback(() => {
4350
unsubscribe();
44-
subscribe(json_data);
51+
subscribe(parseRequestJSON());
4552
setResponseState(true);
4653
}, [reqData, subscribe, unsubscribe]);
4754

@@ -66,8 +73,9 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
6673
<PlaygroundSection
6774
loader={is_loading}
6875
response_state={response_state}
69-
data={data}
76+
full_response={full_response}
7077
error={error}
78+
name={name}
7179
/>
7280
)}
7381
</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');

src/hooks/useSubscription/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ const useSubscription = <T extends TSocketSubscribableEndpointNames>(name: T) =>
1111
const [is_subscribed, setSubscribed] = useState(false);
1212
const [error, setError] = useState<unknown>();
1313
const [data, setData] = useState<TSocketResponseData<T>>();
14+
const [full_response, setFullResponse] = useState<TSocketResponse<T>>();
1415
const [subscriber, setSubscriber] = useState<{ unsubscribe?: VoidFunction }>();
1516

1617
const onData = useCallback(
1718
(response: TSocketResponse<T>) => {
1819
const key = response['msg_type'] ?? name;
1920
setData(response[key] as TSocketResponseData<T>);
21+
setFullResponse(response);
2022
setIsLoading(false);
2123
},
2224
[name],
@@ -41,7 +43,7 @@ const useSubscription = <T extends TSocketSubscribableEndpointNames>(name: T) =>
4143
setSubscribed(false);
4244
}, [subscriber]);
4345

44-
return { subscribe, unsubscribe, is_loading, is_subscribed, error, data };
46+
return { subscribe, unsubscribe, is_loading, is_subscribed, error, data, full_response };
4547
};
4648

4749
export default useSubscription;

src/hooks/useWs/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import apiManager from '@site/src/configs/websocket';
2-
import { TSocketEndpointNames, TSocketResponseData } from '@site/src/configs/websocket/types';
2+
import {
3+
TSocketEndpointNames,
4+
TSocketResponse,
5+
TSocketResponseData,
6+
} from '@site/src/configs/websocket/types';
37
import { useCallback, useState } from 'react';
48

59
const useWS = <T extends TSocketEndpointNames>(name: T) => {
610
const [is_loading, setIsLoading] = useState(false);
711
const [error, setError] = useState<unknown>();
812
const [data, setData] = useState<TSocketResponseData<T>>();
13+
const [full_response, setFullResponse] = useState<TSocketResponse<T>>();
914

1015
const clear = useCallback(() => {
1116
setError(null);
1217
setData(null);
18+
setFullResponse(null);
1319
}, []);
1420

1521
const send = useCallback(
@@ -19,6 +25,7 @@ const useWS = <T extends TSocketEndpointNames>(name: T) => {
1925
const response = await apiManager.augmentedSend(name, data);
2026
const key = response['msg_type'] ?? name;
2127
setData(response[key] as TSocketResponseData<T>);
28+
setFullResponse(response);
2229
} catch (e) {
2330
setError(e);
2431
} finally {
@@ -28,7 +35,7 @@ const useWS = <T extends TSocketEndpointNames>(name: T) => {
2835
[name],
2936
);
3037

31-
return { send, is_loading, error, data, clear };
38+
return { send, full_response, is_loading, error, data, clear };
3239
};
3340

3441
export default useWS;

0 commit comments

Comments
 (0)