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

Commit 9a0f6ec

Browse files
authored
Merge pull request #240 from hubert-deriv/error_responses_not_showing
2 parents 4488a6d + ee433f5 commit 9a0f6ec

File tree

8 files changed

+89
-38
lines changed

8 files changed

+89
-38
lines changed

src/contexts/auth/auth.context.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type IUserAccounts = AuthorizeResponse['authorize']['account_list'];
1010
export type IUser = Omit<AuthorizeResponse['authorize'], 'account_list'>;
1111

1212
export interface IAuthContext {
13+
is_switching_account: boolean;
1314
is_logged_in: boolean;
1415
is_authorized: boolean;
1516
loginAccounts: IUserLoginAccount[];

src/contexts/auth/auth.provider.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ if (getIsBrowser()) {
2222
const AuthProvider = ({ children }: TAuthProviderProps) => {
2323
const [is_logged_in, setIsLoggedIn] = useState(false);
2424
const [is_authorized, setIsAuthorized] = useState(false);
25+
const [is_switching_account, setisSwitchingAccount] = useState(false);
2526

2627
const [loginAccounts, setLoginAccounts] = useSessionStorage<IUserLoginAccount[]>(
2728
LOGIN_ACCOUNTS_SESSION_STORAGE_KEY,
@@ -46,6 +47,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
4647
if (currentLoginAccount.token) {
4748
const { authorize } = await apiManager.authorize(currentLoginAccount.token);
4849
setIsAuthorized(true);
50+
setisSwitchingAccount(false);
4951
const { account_list, ...user } = authorize;
5052
setUserAccounts(account_list);
5153
setUser(user);
@@ -76,6 +78,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
7678
const updateCurrentLoginAccount = useCallback(
7779
(account: IUserLoginAccount) => {
7880
setIsAuthorized(false);
81+
setisSwitchingAccount(true);
7982
setCurrentLoginAccount(account);
8083
},
8184
[setCurrentLoginAccount],
@@ -91,6 +94,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
9194

9295
const context_object: IAuthContext = useMemo(() => {
9396
return {
97+
is_switching_account,
9498
is_logged_in,
9599
is_authorized,
96100
loginAccounts,
@@ -102,6 +106,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
102106
};
103107
}, [
104108
currentLoginAccount,
109+
is_switching_account,
105110
is_authorized,
106111
is_logged_in,
107112
loginAccounts,

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ const ReactJson = React.lazy(() => import('react-json-view'));
1010

1111
type TJsonData<T extends TSocketEndpointNames> = {
1212
history_reponse: TSocketResponse<T>;
13-
error: unknown;
1413
};
1514

1615
const JsonData = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNames>({
1716
history_reponse,
18-
error,
1917
}: TJsonData<T>) => {
2018
return (
2119
<React.Fragment>
22-
{history_reponse !== null && history_reponse?.echo_req ? (
20+
{history_reponse !== null && history_reponse?.echo_req && (
2321
<div
2422
className={styles.reactJsonContainer}
2523
data-testid={`dt_json_container-${history_reponse.req_id}`}
2624
>
2725
<ReactJson src={history_reponse.echo_req} theme='tube' />
2826
<ReactJson src={history_reponse} theme='tube' />
2927
</div>
30-
) : (
31-
<ReactJson src={{ error }} theme='tube' />
3228
)}
3329
</React.Fragment>
3430
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mockUsePlaygroundContext.mockImplementation(() => ({
2525

2626
describe('JsonView', () => {
2727
it('should be able to render the JsonView', () => {
28-
render(<JsonView error={'test'} />);
28+
render(<JsonView />);
2929
// req_id determines the index number of dt_json_container-(idx);
3030
const response_block = screen.getByTestId('dt_json_container-3');
3131
expect(response_block).toBeInTheDocument();

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import {
99
import styles from './JsonView.module.scss';
1010
import Spinner from '@site/src/components/Spinner';
1111

12-
type TJsonView = {
13-
error: unknown;
14-
};
15-
16-
const JsonView = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNames>({
17-
error,
18-
}: TJsonView) => {
12+
const JsonView = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNames>() => {
1913
const { playground_history } = usePlaygroundContext();
2014
return (
2115
<Suspense fallback={<Spinner />}>
@@ -24,7 +18,7 @@ const JsonView = <T extends TSocketEndpointNames | TSocketSubscribableEndpointNa
2418
const key = response.subscription ? JSON.stringify(response) : response.req_id;
2519
return (
2620
<React.Fragment key={key}>
27-
<JsonData history_reponse={response} error={error} />
21+
<JsonData history_reponse={response} />
2822
</React.Fragment>
2923
);
3024
})}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const PlaygroundSection = <T extends TSocketEndpointNames | TSocketSubscribableE
3131
if (full_response) {
3232
setPlaygroundHistory((prev: TSocketResponse<T>[]) => [...prev, full_response]);
3333
}
34+
if (error) {
35+
setPlaygroundHistory((prev: TSocketResponse<T>[]) => [...prev, error]);
36+
}
3437
};
3538

3639
const toggleScrolling = (e) => {
@@ -51,7 +54,7 @@ const PlaygroundSection = <T extends TSocketEndpointNames | TSocketSubscribableE
5154

5255
useEffect(() => {
5356
updateHistory();
54-
}, [full_response]);
57+
}, [full_response, error]);
5558

5659
if (loader && playground_history.length === 0) return <Spinner />;
5760

@@ -65,7 +68,7 @@ const PlaygroundSection = <T extends TSocketEndpointNames | TSocketSubscribableE
6568
>
6669
{response_state && (
6770
<React.Fragment>
68-
<JsonView error={error} />
71+
<JsonView />
6972
</React.Fragment>
7073
)}
7174
</div>

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

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,69 @@ describe('SubscribeRenderer', () => {
141141
expect(mockSubscribe).toBeCalledWith({ ticks: 'R_50', subscribe: 1 });
142142
});
143143

144+
it('should unsubscribe any subscriptions when switching accounts', () => {
145+
cleanup();
146+
jest.clearAllMocks();
147+
148+
mockUseSubscription.mockImplementation(() => ({
149+
subscribe: mockSubscribe,
150+
unsubscribe: mockUnsubscribe,
151+
error: { code: '' },
152+
full_response: {
153+
tick: 1,
154+
echo_req: { tick: 1 },
155+
},
156+
is_subscribed: false,
157+
}));
158+
159+
mockUseAuthContext.mockImplementation(() => ({
160+
is_logged_in: true,
161+
is_authorized: true,
162+
is_switching_account: true,
163+
currentLoginAccount: {
164+
name: 'someAccount',
165+
token: 'asdf1234',
166+
currency: 'USD',
167+
},
168+
}));
169+
170+
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
171+
expect(mockUnsubscribe).toHaveBeenCalledTimes(1);
172+
});
173+
144174
it('should call unsubscribe when pressing the clear button', async () => {
175+
cleanup();
176+
jest.clearAllMocks();
177+
178+
mockUseSubscription.mockImplementation(() => ({
179+
subscribe: mockSubscribe,
180+
unsubscribe: mockUnsubscribe,
181+
error: { code: '' },
182+
full_response: {
183+
tick: 1,
184+
echo_req: { tick: 1 },
185+
},
186+
is_subscribed: true,
187+
}));
188+
189+
mockUseAuthContext.mockImplementation(() => ({
190+
is_logged_in: true,
191+
is_authorized: true,
192+
is_switching_account: false,
193+
currentLoginAccount: {
194+
name: 'someAccount',
195+
token: 'asdf1234',
196+
currency: 'USD',
197+
},
198+
}));
199+
145200
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
146201
const button = await screen.findByRole('button', { name: 'Clear' });
147202
expect(button).toBeVisible();
148203

149204
await userEvent.click(button);
150205
expect(mockUnsubscribe).toBeCalledTimes(1);
151206
});
152-
153207
it('should call unsubscribe when unmounting the component', async () => {
154208
const { unmount } = render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
155209
unmount();
@@ -159,27 +213,26 @@ describe('SubscribeRenderer', () => {
159213
cleanup();
160214
jest.clearAllMocks();
161215

162-
const setToggleModal = jest.fn();
163-
jest.spyOn(React, 'useState').mockReturnValue([false, setToggleModal]);
164216
mockUseAuthContext.mockImplementation(() => ({
165217
is_logged_in: false,
166-
is_authorized: false,
218+
is_authorized: true,
167219
}));
168220
mockUseSubscription.mockImplementation(() => ({
169221
subscribe: mockSubscribe,
170222
unsubscribe: mockUnsubscribe,
171223
error: { code: 'AuthorizationRequired' },
172224
full_response: {
173-
tick: 1,
174-
echo_req: { tick: 1 },
225+
app_list: 1,
226+
echo_req: { app_list: 1 },
175227
},
176228
}));
177229

178230
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
179-
const button = await screen.findByRole('button', { name: /Send Request/i });
180-
await userEvent.click(button);
231+
const login_dialog = await screen.findByText(
232+
/This API call must be authorised because it requires access to your account information./i,
233+
);
181234
await waitFor(() => {
182-
expect(setToggleModal).toHaveBeenCalled();
235+
expect(login_dialog).toBeVisible();
183236
});
184237
});
185238
});

src/features/Apiexplorer/SubscribeRenderer/index.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
2323
reqData,
2424
auth,
2525
}: IResponseRendererProps<T>) {
26-
const { is_logged_in, is_authorized } = useAuthContext();
26+
const { is_logged_in, is_switching_account } = useAuthContext();
2727
const { disableSendRequest } = useDisableSendRequest();
2828
const { full_response, is_loading, subscribe, unsubscribe, is_subscribed, error } =
2929
useSubscription<T>(name);
@@ -44,9 +44,8 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
4444
}, [is_subscribed]);
4545

4646
useEffect(() => {
47-
const has_active_subscription = full_response !== undefined && !is_authorized;
48-
if (has_active_subscription) unsubscribe();
49-
}, [full_response, is_authorized]);
47+
if (is_switching_account) unsubscribe();
48+
}, [is_switching_account]);
5049

5150
const parseRequestJSON = useCallback(() => {
5251
let request_data: TSocketRequestProps<T> extends never ? undefined : TSocketRequestProps<T>;
@@ -86,18 +85,18 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
8685
Clear
8786
</Button>
8887
</div>
89-
{is_not_valid ? (
88+
{is_not_valid && (
9089
<ValidDialog setIsNotValid={setIsNotValid} setToggleModal={setToggleModal} />
91-
) : !is_logged_in && auth == 1 && toggle_modal ? (
90+
)}
91+
{!is_logged_in && auth == 1 && toggle_modal && (
9292
<LoginDialog setToggleModal={setToggleModal} />
93-
) : (
94-
<PlaygroundSection
95-
loader={is_loading}
96-
response_state={response_state}
97-
full_response={full_response}
98-
error={error}
99-
/>
10093
)}
94+
<PlaygroundSection
95+
loader={is_loading}
96+
response_state={response_state}
97+
full_response={full_response}
98+
error={error}
99+
/>
101100
</div>
102101
);
103102
}

0 commit comments

Comments
 (0)