|
1 | 1 | import React from 'react'; |
2 | 2 | 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'; |
4 | 4 | import SubscribeRenderer from '..'; |
5 | 5 | import useAuthContext from '@site/src/hooks/useAuthContext'; |
6 | 6 | import useSubscription from '@site/src/hooks/useSubscription'; |
7 | 7 | import useDynamicImportJSON from '@site/src/hooks/useDynamicImportJSON'; |
8 | 8 | import { IAuthContext } from '@site/src/contexts/auth/auth.context'; |
| 9 | +import LoginDialog from '../../LoginDialog'; |
9 | 10 |
|
10 | 11 | jest.mock('@site/src/hooks/useAuthContext'); |
11 | 12 |
|
@@ -78,39 +79,82 @@ describe('SubscribeRenderer', () => { |
78 | 79 | expect(button).toBeVisible(); |
79 | 80 | }); |
80 | 81 |
|
| 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 | + |
81 | 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 | + }); |
82 | 102 | render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />); |
83 | 103 | const button = await screen.findByRole('button', { name: /Send Request/i }); |
84 | 104 | expect(button).toBeVisible(); |
85 | 105 |
|
86 | 106 | await userEvent.click(button); |
87 | | - |
88 | 107 | expect(mockUnsubscribe).toBeCalledTimes(1); |
89 | 108 | expect(mockSubscribe).toBeCalledTimes(1); |
90 | 109 | expect(mockSubscribe).toBeCalledWith({ ticks: 'R_50', subscribe: 1 }); |
91 | 110 | }); |
92 | 111 |
|
93 | 112 | it('should call unsubscribe when pressing the clear button', async () => { |
| 113 | + jest.spyOn(React, 'useRef').mockReturnValue({ |
| 114 | + current: { |
| 115 | + unsubscribe: mockUnsubscribe, |
| 116 | + }, |
| 117 | + }); |
94 | 118 | render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />); |
95 | 119 | const button = await screen.findByRole('button', { name: 'Clear' }); |
96 | 120 | expect(button).toBeVisible(); |
97 | 121 |
|
98 | 122 | await userEvent.click(button); |
99 | | - |
100 | 123 | expect(mockUnsubscribe).toBeCalledTimes(1); |
101 | 124 | }); |
102 | 125 |
|
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 | + })); |
107 | 152 |
|
108 | | - render(<SubscribeRenderer name='ticks' auth={1} reqData={'asdawefaewf3232'} />); |
| 153 | + render(<SubscribeRenderer name='ticks' auth={1} reqData={request_data} />); |
109 | 154 | const button = await screen.findByRole('button', { name: /Send Request/i }); |
110 | 155 | 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 | + }); |
115 | 159 | }); |
116 | 160 | }); |
0 commit comments