From 4ffe50c07ea7e5e7039cd01b963634f2d2a85a8d Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Wed, 29 Nov 2023 17:30:46 +0400 Subject: [PATCH 01/12] chore: updated the WS connection --- src/hooks/useWs/index.tsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/hooks/useWs/index.tsx b/src/hooks/useWs/index.tsx index b8ac97aae..84e9b5635 100644 --- a/src/hooks/useWs/index.tsx +++ b/src/hooks/useWs/index.tsx @@ -6,7 +6,7 @@ import { } from '@site/src/configs/websocket/types'; import { useCallback, useState } from 'react'; -const useWS = (name: T) => { +const useWS = (name?: T) => { const [is_loading, setIsLoading] = useState(false); const [error, setError] = useState(); const [data, setData] = useState>(); @@ -19,10 +19,20 @@ const useWS = (name: T) => { }, []); const send = useCallback( - async (data?: Parameters>[1]) => { + async (data?: Parameters>[]) => { + let payload: any = data; + console.log(); + + if (name) { + payload = { ...payload, name: { [name]: 1 } }; + } else { + payload = { ...payload }; + } + setIsLoading(true); + try { - const response = await apiManager.augmentedSend(name, data); + const response = await apiManager.augmentedSend(payload); const key = response['msg_type'] ?? name; setData(response[key] as TSocketResponseData); setFullResponse(response); From 1c33bf8cff3bf6324771d88d7fdd5afd52958c1e Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Wed, 29 Nov 2023 17:31:38 +0400 Subject: [PATCH 02/12] chore: updated the WS connection --- src/configs/websocket/index.ts | 8 +- .../Apiexplorer/RequestJSONBox/index.tsx | 10 - .../__tests__/useDynamicImport.test.tsx | 243 ++++++++---------- src/hooks/useSubscription/index.tsx | 10 +- src/hooks/useWs/__tests__/useWs.test.tsx | 4 +- 5 files changed, 125 insertions(+), 150 deletions(-) diff --git a/src/configs/websocket/index.ts b/src/configs/websocket/index.ts index a6b49de63..da4287048 100644 --- a/src/configs/websocket/index.ts +++ b/src/configs/websocket/index.ts @@ -40,19 +40,15 @@ export class ApiManager { } public augmentedSend( - name: T, request?: TSocketRequestProps extends never ? undefined : TSocketRequestProps, ): Promise> { - return this.derivApi.send({ [name]: 1, ...request }) as Promise>; + return this.derivApi.send(request) as Promise>; } public augmentedSubscribe( - name: T, request?: TSocketRequestProps extends never ? undefined : TSocketRequestProps, ): Observable> { - return this.derivApi.subscribe({ [name]: 1, subscribe: 1, ...request }) as Observable< - TSocketResponse - >; + return this.derivApi.subscribe({ subscribe: 1, request }) as Observable>; } public authorize(token: string) { diff --git a/src/features/Apiexplorer/RequestJSONBox/index.tsx b/src/features/Apiexplorer/RequestJSONBox/index.tsx index 88b409c87..495ac3aa5 100644 --- a/src/features/Apiexplorer/RequestJSONBox/index.tsx +++ b/src/features/Apiexplorer/RequestJSONBox/index.tsx @@ -22,15 +22,6 @@ function RequestJSONBox({ const is_subscribe = useMemo(() => { return request_example?.includes('subscribe'); }, [request_example]); - const [isdisabled, setIsDisabled] = useState(false); - - useEffect(() => { - if (name === null) { - setIsDisabled(true); - } else { - setIsDisabled(false); - } - }, [name]); return (
@@ -44,7 +35,6 @@ function RequestJSONBox({ placeholder={'Request JSON'} onChange={handleChange} value={request_example} - disabled={isdisabled} > {is_subscribe ? ( ({ +import React from 'react'; +import '@testing-library/jest-dom'; +import userEvent from '@testing-library/user-event'; +import { renderHook } from '@testing-library/react-hooks'; +import { act } from 'react-dom/test-utils'; +import useDynamicImportJSON from '..'; +import { cleanup, render, screen } from '@testing-library/react'; + +jest.mock('@docusaurus/router', () => ({ useLocation: () => ({ - pathname: "/api-explorer#active_symbols", - hash: "#active_symbols", + pathname: '/api-explorer#active_symbols', + hash: '#active_symbols', }), useHistory: () => ({ push: jest.fn(), }), })); -jest.mock("@site/src/hooks/useAuthContext"); +jest.mock('@site/src/hooks/useAuthContext'); -describe("useDynamicImportJSON", () => { +describe('useDynamicImportJSON', () => { const { result } = renderHook(() => useDynamicImportJSON()); afterEach(() => { @@ -26,61 +26,52 @@ describe("useDynamicImportJSON", () => { cleanup(); }); - it("should populate text data with the correct values", () => { + it('should populate text data with the correct values', () => { act(() => { expect(result.current.text_data).toEqual({ - request: - '{\n "active_symbols": "brief",\n "product_type": "basic"\n}', - selected_value: "Active Symbols", - name: "active_symbols", + request: '{\n "active_symbols": "brief",\n "product_type": "basic"\n}', + selected_value: 'Active Symbols', + name: 'active_symbols', }); }); }); - it("should be able to call handleTextAreaInput when typing in a textarea", async () => { - const spyHandleInputFunction = jest.spyOn( - result.current, - "handleTextAreaInput" - ); + it('should be able to call handleTextAreaInput when typing in a textarea', async () => { + const spyHandleInputFunction = jest.spyOn(result.current, 'handleTextAreaInput'); - render( -