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

Commit 8ecc698

Browse files
committed
chore: test cases
1 parent f634ed4 commit 8ecc698

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,6 @@ describe('SubscribeRenderer', () => {
7878
expect(button).toBeVisible();
7979
});
8080

81-
it('should call subscribe and unsubscribe when pressing the send request button', async () => {
82-
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
83-
const button = await screen.findByRole('button', { name: /Send Request/i });
84-
expect(button).toBeVisible();
85-
86-
await userEvent.click(button);
87-
expect(mockSubscribe).toBeCalledTimes(1);
88-
expect(mockSubscribe).toBeCalledWith({ ticks: 'R_50', subscribe: 1 });
89-
});
90-
91-
it('should call unsubscribe when pressing the clear button', async () => {
92-
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
93-
const button = await screen.findByRole('button', { name: 'Clear' });
94-
expect(button).toBeVisible();
95-
96-
await userEvent.click(button);
97-
});
98-
9981
it('should throw an error if incorrect json is being parsed', async () => {
10082
const consoleOutput = [];
10183
const mockedError = (output) => consoleOutput.push(output);
@@ -110,3 +92,28 @@ describe('SubscribeRenderer', () => {
11092
);
11193
});
11294
});
95+
96+
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+
});
102+
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
103+
const button = await screen.findByRole('button', { name: /Send Request/i });
104+
expect(button).toBeVisible();
105+
106+
await userEvent.click(button);
107+
expect(mockUnsubscribe).toBeCalledTimes(1);
108+
expect(mockSubscribe).toBeCalledTimes(1);
109+
expect(mockSubscribe).toBeCalledWith({ ticks: 'R_50', subscribe: 1 });
110+
});
111+
112+
it('should call unsubscribe when pressing the clear button', async () => {
113+
render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />);
114+
const button = await screen.findByRole('button', { name: 'Clear' });
115+
expect(button).toBeVisible();
116+
117+
await userEvent.click(button);
118+
expect(mockUnsubscribe).toBeCalledTimes(1);
119+
});

src/features/Apiexplorer/SubscribeRenderer/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
3030
const [toggle_modal, setToggleModal] = useState(false);
3131
const [is_not_valid, setIsNotValid] = useState(false);
3232

33-
const subscribe_ref: any = useRef();
33+
const subscribe_ref: React.MutableRefObject<{ unsubscribe: () => void }> = useRef();
3434

3535
useEffect(() => {
3636
if (error && error.code === 'AuthorizationRequired') {
@@ -42,7 +42,7 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
4242
};
4343
}, [error]);
4444

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

4848
try {
@@ -54,13 +54,13 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
5454
}
5555

5656
return request_data;
57-
};
57+
}, [reqData]);
5858

5959
const handleClick = useCallback(() => {
6060
if (subscribe_ref.current) subscribe_ref.current.unsubscribe();
6161
subscribe_ref.current = subscribe(parseRequestJSON());
6262
setResponseState(true);
63-
}, [subscribe, unsubscribe]);
63+
}, [parseRequestJSON, subscribe]);
6464

6565
const handleClear = () => {
6666
subscribe_ref.current?.unsubscribe?.();

0 commit comments

Comments
 (0)