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

Commit 7cbebfb

Browse files
Hubert KosterHubert Koster
authored andcommitted
chore: fixing errors
1 parent a49f83a commit 7cbebfb

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ const JsonData = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNa
1919
name,
2020
error,
2121
}: TJsonData<T>) => {
22-
const getResponse = useCallback((key: string) => full_response[key], [full_response]);
23-
const key = getResponse('msg_type') ?? name;
22+
const getResponse = useCallback(
23+
(key: string) => {
24+
const response = full_response;
25+
const selected_key = response[key] ?? name;
26+
if (response !== null) return selected_key;
27+
},
28+
[full_response],
29+
);
30+
31+
const key = getResponse('msg_type');
2432
const echo_req_json = {
2533
echo_req: getResponse('echo_req'),
2634
msg_type: getResponse('msg_type'),

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,19 @@ describe('SubscribeRenderer', () => {
100100
expect(mockUnsubscribe).toBeCalledTimes(1);
101101
});
102102

103-
it('should throw an error if incorrect json is being parsed', () => {
103+
it('should throw an error if incorrect json is being parsed', async () => {
104104
const consoleOutput = [];
105105
const mockedError = (output) => consoleOutput.push(output);
106106
console.error = mockedError;
107107

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

110-
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+
);
111117
});
112118
});

src/features/Apiexplorer/SubscribeRenderer/index.tsx

Lines changed: 16 additions & 9 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';
@@ -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

src/hooks/useSubscription/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const useSubscription = <T extends TSocketSubscribableEndpointNames>(name: T) =>
1818
(response: TSocketResponse<T>) => {
1919
const key = response['msg_type'] ?? name;
2020
setData(response[key] as TSocketResponseData<T>);
21-
setFullResponse(full_response);
21+
setFullResponse(response);
2222
setIsLoading(false);
2323
},
2424
[name],

0 commit comments

Comments
 (0)