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

Commit b340722

Browse files
authored
Merge pull request #201 from sanjam-deriv/balanceerror
2 parents 1a647b4 + 2b2fda8 commit b340722

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

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

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22
import userEvent from '@testing-library/user-event';
3-
import { cleanup, render, screen } from '@testing-library/react';
3+
import { cleanup, render, screen, waitFor } from '@testing-library/react';
44
import SubscribeRenderer from '..';
55
import useAuthContext from '@site/src/hooks/useAuthContext';
66
import useSubscription from '@site/src/hooks/useSubscription';
77
import useDynamicImportJSON from '@site/src/hooks/useDynamicImportJSON';
88
import { IAuthContext } from '@site/src/contexts/auth/auth.context';
9+
import LoginDialog from '../../LoginDialog';
910

1011
jest.mock('@site/src/hooks/useAuthContext');
1112

@@ -78,39 +79,82 @@ describe('SubscribeRenderer', () => {
7879
expect(button).toBeVisible();
7980
});
8081

82+
it('should throw an error if incorrect json is being parsed', async () => {
83+
const consoleOutput = [];
84+
const mockedError = (output) => consoleOutput.push(output);
85+
console.error = mockedError;
86+
87+
render(<SubscribeRenderer name='ticks' auth={1} reqData={'asdawefaewf3232'} />);
88+
const button = await screen.findByRole('button', { name: /Send Request/i });
89+
await userEvent.click(button);
90+
91+
expect(consoleOutput[0]).toEqual(
92+
'Could not parse the JSON data while trying to send the request: ',
93+
);
94+
});
95+
8196
it('should call subscribe and unsubscribe when pressing the send request button', async () => {
97+
jest.spyOn(React, 'useRef').mockReturnValue({
98+
current: {
99+
unsubscribe: mockUnsubscribe,
100+
},
101+
});
82102
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
83103
const button = await screen.findByRole('button', { name: /Send Request/i });
84104
expect(button).toBeVisible();
85105

86106
await userEvent.click(button);
87-
88107
expect(mockUnsubscribe).toBeCalledTimes(1);
89108
expect(mockSubscribe).toBeCalledTimes(1);
90109
expect(mockSubscribe).toBeCalledWith({ ticks: 'R_50', subscribe: 1 });
91110
});
92111

93112
it('should call unsubscribe when pressing the clear button', async () => {
113+
jest.spyOn(React, 'useRef').mockReturnValue({
114+
current: {
115+
unsubscribe: mockUnsubscribe,
116+
},
117+
});
94118
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
95119
const button = await screen.findByRole('button', { name: 'Clear' });
96120
expect(button).toBeVisible();
97121

98122
await userEvent.click(button);
99-
100123
expect(mockUnsubscribe).toBeCalledTimes(1);
101124
});
102125

103-
it('should throw an error if incorrect json is being parsed', async () => {
104-
const consoleOutput = [];
105-
const mockedError = (output) => consoleOutput.push(output);
106-
console.error = mockedError;
126+
it('should call unsubscribe when unmounting the component', async () => {
127+
jest.spyOn(React, 'useRef').mockReturnValue({
128+
current: {
129+
unsubscribe: mockUnsubscribe,
130+
},
131+
});
132+
const { unmount } = render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
133+
unmount();
134+
expect(mockUnsubscribe).toBeCalledTimes(1);
135+
});
136+
it('should call login dialog when the error code is not authourized', async () => {
137+
const setToggleModal = jest.fn();
138+
jest.spyOn(React, 'useState').mockReturnValue([false, setToggleModal]);
139+
mockUseAuthContext.mockImplementation(() => ({
140+
is_logged_in: false,
141+
is_authorized: false,
142+
}));
143+
mockUseSubscription.mockImplementation(() => ({
144+
subscribe: mockSubscribe,
145+
unsubscribe: mockUnsubscribe,
146+
error: { code: 'AuthorizationRequired' },
147+
full_response: {
148+
tick: 1,
149+
echo_req: { tick: 1 },
150+
},
151+
}));
107152

108-
render(<SubscribeRenderer name='ticks' auth={1} reqData={'asdawefaewf3232'} />);
153+
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
109154
const button = await screen.findByRole('button', { name: /Send Request/i });
110155
await userEvent.click(button);
111-
112-
expect(consoleOutput[0]).toEqual(
113-
'Could not parse the JSON data while trying to send the request: ',
114-
);
156+
await waitFor(() => {
157+
expect(setToggleModal).toHaveBeenCalled();
158+
});
115159
});
116160
});

src/features/Apiexplorer/SubscribeRenderer/index.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useCallback, useEffect } from 'react';
1+
import React, { useState, useCallback, useEffect, useRef } from 'react';
22
import {
33
TSocketSubscribableEndpointNames,
44
TSocketRequestProps,
@@ -25,18 +25,24 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
2525
}: IResponseRendererProps<T>) {
2626
const { is_logged_in } = useAuthContext();
2727
const { disableSendRequest } = useDisableSendRequest();
28-
const { full_response, is_loading, subscribe, unsubscribe, error } = useSubscription<T>(name);
28+
const { full_response, is_loading, subscribe, error } = useSubscription<T>(name);
2929
const [response_state, setResponseState] = useState(false);
3030
const [toggle_modal, setToggleModal] = useState(false);
3131
const [is_not_valid, setIsNotValid] = useState(false);
3232

33+
const subscribe_ref: React.MutableRefObject<{ unsubscribe: () => void }> = useRef();
34+
3335
useEffect(() => {
3436
if (error && error.code === 'AuthorizationRequired') {
3537
setToggleModal(true);
3638
}
39+
40+
return () => {
41+
if (subscribe_ref.current) subscribe_ref.current.unsubscribe();
42+
};
3743
}, [error]);
3844

39-
const parseRequestJSON = () => {
45+
const parseRequestJSON = useCallback(() => {
4046
let request_data: TSocketRequestProps<T> extends never ? undefined : TSocketRequestProps<T>;
4147

4248
try {
@@ -48,16 +54,16 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
4854
}
4955

5056
return request_data;
51-
};
57+
}, [reqData]);
5258

5359
const handleClick = useCallback(() => {
54-
unsubscribe();
55-
subscribe(parseRequestJSON());
60+
if (subscribe_ref.current) subscribe_ref.current.unsubscribe();
61+
subscribe_ref.current = subscribe(parseRequestJSON());
5662
setResponseState(true);
57-
}, [reqData, subscribe, unsubscribe]);
63+
}, [parseRequestJSON, subscribe]);
5864

5965
const handleClear = () => {
60-
unsubscribe();
66+
subscribe_ref.current.unsubscribe();
6167
setResponseState(false);
6268
};
6369

@@ -88,4 +94,4 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
8894
);
8995
}
9096

91-
export default SubscribeRenderer;
97+
export default React.memo(SubscribeRenderer);

src/hooks/useSubscription/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ const useSubscription = <T extends TSocketSubscribableEndpointNames>(name: T) =>
3939
(data: Parameters<typeof apiManager.augmentedSubscribe<T>>[1]) => {
4040
setIsLoading(true);
4141
setSubscribed(true);
42-
setSubscriber(apiManager.augmentedSubscribe(name, data).subscribe(onData, onError));
42+
const subscriber_ref = apiManager.augmentedSubscribe(name, data).subscribe(onData, onError);
43+
setSubscriber(subscriber_ref);
44+
return subscriber_ref;
4345
},
4446
[name, onData, onError],
4547
);
@@ -49,7 +51,15 @@ const useSubscription = <T extends TSocketSubscribableEndpointNames>(name: T) =>
4951
setSubscribed(false);
5052
}, [subscriber]);
5153

52-
return { subscribe, unsubscribe, is_loading, is_subscribed, error, data, full_response };
54+
return {
55+
subscribe,
56+
unsubscribe,
57+
is_loading,
58+
is_subscribed,
59+
error,
60+
data,
61+
full_response,
62+
};
5363
};
5464

5565
export default useSubscription;

0 commit comments

Comments
 (0)