From 282e1f47146cd04e0b9750db84aa36bd92a3a32d Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 23 Aug 2023 16:12:50 +0200 Subject: [PATCH 1/4] chore: fixing no error messages --- src/contexts/auth/auth.context.tsx | 1 + src/contexts/auth/auth.provider.tsx | 5 ++ .../JsonView/JsonData/index.tsx | 6 +- .../JsonView/__tests__/JsonView.test.tsx | 2 +- .../PlaygroundSection/JsonView/index.tsx | 10 +--- .../PlaygroundSection/index.tsx | 7 ++- .../__tests__/SubscribeRenderer.test.tsx | 55 +++++++++++++++++++ .../Apiexplorer/SubscribeRenderer/index.tsx | 25 ++++----- 8 files changed, 82 insertions(+), 29 deletions(-) diff --git a/src/contexts/auth/auth.context.tsx b/src/contexts/auth/auth.context.tsx index 768ff48e5..960ff3795 100644 --- a/src/contexts/auth/auth.context.tsx +++ b/src/contexts/auth/auth.context.tsx @@ -10,6 +10,7 @@ export type IUserAccounts = AuthorizeResponse['authorize']['account_list']; export type IUser = Omit; export interface IAuthContext { + is_switching_account: boolean; is_logged_in: boolean; is_authorized: boolean; loginAccounts: IUserLoginAccount[]; diff --git a/src/contexts/auth/auth.provider.tsx b/src/contexts/auth/auth.provider.tsx index 33e7c2413..d36d091e3 100644 --- a/src/contexts/auth/auth.provider.tsx +++ b/src/contexts/auth/auth.provider.tsx @@ -22,6 +22,7 @@ if (getIsBrowser()) { const AuthProvider = ({ children }: TAuthProviderProps) => { const [is_logged_in, setIsLoggedIn] = useState(false); const [is_authorized, setIsAuthorized] = useState(false); + const [is_switching_account, setisSwitchingAccount] = useState(false); const [loginAccounts, setLoginAccounts] = useSessionStorage( LOGIN_ACCOUNTS_SESSION_STORAGE_KEY, @@ -46,6 +47,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => { if (currentLoginAccount.token) { const { authorize } = await apiManager.authorize(currentLoginAccount.token); setIsAuthorized(true); + setisSwitchingAccount(false); const { account_list, ...user } = authorize; setUserAccounts(account_list); setUser(user); @@ -76,6 +78,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => { const updateCurrentLoginAccount = useCallback( (account: IUserLoginAccount) => { setIsAuthorized(false); + setisSwitchingAccount(true); setCurrentLoginAccount(account); }, [setCurrentLoginAccount], @@ -91,6 +94,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => { const context_object: IAuthContext = useMemo(() => { return { + is_switching_account, is_logged_in, is_authorized, loginAccounts, @@ -102,6 +106,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => { }; }, [ currentLoginAccount, + is_switching_account, is_authorized, is_logged_in, loginAccounts, diff --git a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/JsonData/index.tsx b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/JsonData/index.tsx index 4e4fa769c..ae6848d64 100644 --- a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/JsonData/index.tsx +++ b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/JsonData/index.tsx @@ -10,16 +10,14 @@ const ReactJson = React.lazy(() => import('react-json-view')); type TJsonData = { history_reponse: TSocketResponse; - error: unknown; }; const JsonData = ({ history_reponse, - error, }: TJsonData) => { return ( - {history_reponse !== null && history_reponse?.echo_req ? ( + {history_reponse !== null && history_reponse?.echo_req && (
- ) : ( - )}
); diff --git a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/__tests__/JsonView.test.tsx b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/__tests__/JsonView.test.tsx index 92258deeb..35b3da0cd 100644 --- a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/__tests__/JsonView.test.tsx +++ b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/__tests__/JsonView.test.tsx @@ -25,7 +25,7 @@ mockUsePlaygroundContext.mockImplementation(() => ({ describe('JsonView', () => { it('should be able to render the JsonView', () => { - render(); + render(); // req_id determines the index number of dt_json_container-(idx); const response_block = screen.getByTestId('dt_json_container-3'); expect(response_block).toBeInTheDocument(); diff --git a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/index.tsx b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/index.tsx index 89a4c2bd9..3aa831904 100644 --- a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/index.tsx +++ b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/JsonView/index.tsx @@ -9,13 +9,7 @@ import { import styles from './JsonView.module.scss'; import Spinner from '@site/src/components/Spinner'; -type TJsonView = { - error: unknown; -}; - -const JsonView = ({ - error, -}: TJsonView) => { +const JsonView = () => { const { playground_history } = usePlaygroundContext(); return ( }> @@ -24,7 +18,7 @@ const JsonView = - + ); })} diff --git a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/index.tsx b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/index.tsx index cf828c8c5..efe8839de 100644 --- a/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/index.tsx +++ b/src/features/Apiexplorer/RequestResponseRenderer/PlaygroundSection/index.tsx @@ -31,6 +31,9 @@ const PlaygroundSection = []) => [...prev, full_response]); } + if (error) { + setPlaygroundHistory((prev: TSocketResponse[]) => [...prev, error]); + } }; const toggleScrolling = (e) => { @@ -51,7 +54,7 @@ const PlaygroundSection = { updateHistory(); - }, [full_response]); + }, [full_response, error]); if (loader && playground_history.length === 0) return ; @@ -65,7 +68,7 @@ const PlaygroundSection = {response_state && ( - + )} diff --git a/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx b/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx index 9f1844f7e..09923a7fa 100644 --- a/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx +++ b/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx @@ -141,7 +141,62 @@ describe('SubscribeRenderer', () => { expect(mockSubscribe).toBeCalledWith({ ticks: 'R_50', subscribe: 1 }); }); + it('should unsubscribe any subscriptions when switching accounts', () => { + cleanup(); + jest.clearAllMocks(); + + mockUseSubscription.mockImplementation(() => ({ + subscribe: mockSubscribe, + unsubscribe: mockUnsubscribe, + error: { code: '' }, + full_response: { + tick: 1, + echo_req: { tick: 1 }, + }, + is_subscribed: false, + })); + + mockUseAuthContext.mockImplementation(() => ({ + is_logged_in: true, + is_authorized: true, + is_switching_account: true, + currentLoginAccount: { + name: 'someAccount', + token: 'asdf1234', + currency: 'USD', + }, + })); + + render(); + expect(mockUnsubscribe).toHaveBeenCalledTimes(1); + }); + it('should call unsubscribe when pressing the clear button', async () => { + cleanup(); + jest.clearAllMocks(); + + mockUseSubscription.mockImplementation(() => ({ + subscribe: mockSubscribe, + unsubscribe: mockUnsubscribe, + error: { code: '' }, + full_response: { + tick: 1, + echo_req: { tick: 1 }, + }, + is_subscribed: true, + })); + + mockUseAuthContext.mockImplementation(() => ({ + is_logged_in: true, + is_authorized: true, + is_switching_account: false, + currentLoginAccount: { + name: 'someAccount', + token: 'asdf1234', + currency: 'USD', + }, + })); + render(); const button = await screen.findByRole('button', { name: 'Clear' }); expect(button).toBeVisible(); diff --git a/src/features/Apiexplorer/SubscribeRenderer/index.tsx b/src/features/Apiexplorer/SubscribeRenderer/index.tsx index d8d6f97b4..912a90f50 100644 --- a/src/features/Apiexplorer/SubscribeRenderer/index.tsx +++ b/src/features/Apiexplorer/SubscribeRenderer/index.tsx @@ -23,7 +23,7 @@ function SubscribeRenderer({ reqData, auth, }: IResponseRendererProps) { - const { is_logged_in, is_authorized } = useAuthContext(); + const { is_logged_in, is_switching_account } = useAuthContext(); const { disableSendRequest } = useDisableSendRequest(); const { full_response, is_loading, subscribe, unsubscribe, is_subscribed, error } = useSubscription(name); @@ -44,9 +44,8 @@ function SubscribeRenderer({ }, [is_subscribed]); useEffect(() => { - const has_active_subscription = full_response !== undefined && !is_authorized; - if (has_active_subscription) unsubscribe(); - }, [full_response, is_authorized]); + if (is_switching_account) unsubscribe(); + }, [is_switching_account]); const parseRequestJSON = useCallback(() => { let request_data: TSocketRequestProps extends never ? undefined : TSocketRequestProps; @@ -86,18 +85,18 @@ function SubscribeRenderer({ Clear - {is_not_valid ? ( + {is_not_valid && ( - ) : !is_logged_in && auth == 1 && toggle_modal ? ( + )} + {!is_logged_in && auth == 1 && toggle_modal && ( - ) : ( - )} + ); } From 8e356ac6ae363ac1cc57caf55570dae5bbe71aef Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 23 Aug 2023 17:36:06 +0200 Subject: [PATCH 2/4] chore: rerun tests --- src/features/dashboard/register-app/index.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/features/dashboard/register-app/index.tsx b/src/features/dashboard/register-app/index.tsx index c1205a489..e301314a2 100644 --- a/src/features/dashboard/register-app/index.tsx +++ b/src/features/dashboard/register-app/index.tsx @@ -7,6 +7,8 @@ import { RegisterAppDialogSuccess } from '../components/Dialogs/RegisterAppDialo import { IRegisterAppForm } from '../types'; import useAuthContext from '@site/src/hooks/useAuthContext'; +//test + const AppRegistration = () => { const { send: registerApp, error, clear, data } = useWS('app_register'); const { currentLoginAccount } = useAuthContext(); From a904a90164716d443492f095c2f08565a35358ef Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 23 Aug 2023 17:37:29 +0200 Subject: [PATCH 3/4] chore: rerun tests --- src/features/dashboard/register-app/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/features/dashboard/register-app/index.tsx b/src/features/dashboard/register-app/index.tsx index e301314a2..c1205a489 100644 --- a/src/features/dashboard/register-app/index.tsx +++ b/src/features/dashboard/register-app/index.tsx @@ -7,8 +7,6 @@ import { RegisterAppDialogSuccess } from '../components/Dialogs/RegisterAppDialo import { IRegisterAppForm } from '../types'; import useAuthContext from '@site/src/hooks/useAuthContext'; -//test - const AppRegistration = () => { const { send: registerApp, error, clear, data } = useWS('app_register'); const { currentLoginAccount } = useAuthContext(); From ee433f5d05a7eb9fa046555cd48e596c92ca0afe Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 23 Aug 2023 18:01:42 +0200 Subject: [PATCH 4/4] chore: fixing test --- .../__tests__/SubscribeRenderer.test.tsx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx b/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx index 09923a7fa..dbd0da668 100644 --- a/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx +++ b/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx @@ -204,7 +204,6 @@ describe('SubscribeRenderer', () => { await userEvent.click(button); expect(mockUnsubscribe).toBeCalledTimes(1); }); - it('should call unsubscribe when unmounting the component', async () => { const { unmount } = render(); unmount(); @@ -214,27 +213,26 @@ describe('SubscribeRenderer', () => { cleanup(); jest.clearAllMocks(); - const setToggleModal = jest.fn(); - jest.spyOn(React, 'useState').mockReturnValue([false, setToggleModal]); mockUseAuthContext.mockImplementation(() => ({ is_logged_in: false, - is_authorized: false, + is_authorized: true, })); mockUseSubscription.mockImplementation(() => ({ subscribe: mockSubscribe, unsubscribe: mockUnsubscribe, error: { code: 'AuthorizationRequired' }, full_response: { - tick: 1, - echo_req: { tick: 1 }, + app_list: 1, + echo_req: { app_list: 1 }, }, })); render(); - const button = await screen.findByRole('button', { name: /Send Request/i }); - await userEvent.click(button); + const login_dialog = await screen.findByText( + /This API call must be authorised because it requires access to your account information./i, + ); await waitFor(() => { - expect(setToggleModal).toHaveBeenCalled(); + expect(login_dialog).toBeVisible(); }); }); });