From aa49c450c1c13d3f7132904648d92e0b88fac4bd Mon Sep 17 00:00:00 2001 From: theborakompanioni Date: Tue, 28 Nov 2023 00:09:48 +0100 Subject: [PATCH] test(websocket): enable websocket indicator test in App.test.tsx --- src/components/App.test.tsx | 25 ++++++++++++++++++------- src/context/WebsocketContext.tsx | 6 ++---- src/setupTests.ts | 7 ++----- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/components/App.test.tsx b/src/components/App.test.tsx index 6876435fa..7333f46ec 100644 --- a/src/components/App.test.tsx +++ b/src/components/App.test.tsx @@ -58,20 +58,31 @@ describe('', () => { expect(screen.getByText('footer.warning_alert_button_ok')).toBeInTheDocument() }) - /*it('should display a websocket connection indicator', async () => { + it('should display websocket connection indicator as CONNECTED', async () => { global.__DEV__.addToAppSettings({ showOnboarding: false }) await act(async () => { render() }) - expect(screen.getByTestId('connection-indicator-icon').classList.contains('text-secondary')).toBe(true) - expect(screen.getByTestId('connection-indicator-icon').classList.contains('text-success')).toBe(false) + await global.__DEV__.JM_WEBSOCKET_SERVER_MOCK.connected - await act(async () => { - await global.__DEV__.JM_WEBSOCKET_SERVER_MOCK.connected - }) expect(screen.getByTestId('connection-indicator-icon').classList.contains('text-success')).toBe(true) expect(screen.getByTestId('connection-indicator-icon').classList.contains('text-secondary')).toBe(false) - })*/ + }) + + it('should display websocket connection indicator AS DISCONNECTED', async () => { + global.__DEV__.addToAppSettings({ showOnboarding: false }) + + await act(async () => { + render() + }) + + await act(async () => { + global.__DEV__.JM_WEBSOCKET_SERVER_MOCK.close() + }) + + expect(screen.getByTestId('connection-indicator-icon').classList.contains('text-success')).toBe(false) + expect(screen.getByTestId('connection-indicator-icon').classList.contains('text-secondary')).toBe(true) + }) }) diff --git a/src/context/WebsocketContext.tsx b/src/context/WebsocketContext.tsx index cd50a00a9..71bb85b7b 100644 --- a/src/context/WebsocketContext.tsx +++ b/src/context/WebsocketContext.tsx @@ -47,8 +47,6 @@ const createWebSocket = () => { return websocket } -const initialWebsocket = createWebSocket() - export interface WebsocketContextEntry { websocket: WebSocket websocketState: number @@ -62,8 +60,8 @@ const WebsocketContext = createContext(undefi * See Websocket docs: https://github.com/JoinMarket-Org/joinmarket-clientserver/blob/v0.9.5/docs/JSON-RPC-API-using-jmwalletd.md#websocket */ const WebsocketProvider = ({ children }: PropsWithChildren<{}>) => { - const [websocket, setWebsocket] = useState(initialWebsocket) - const [websocketState, setWebsocketState] = useState(initialWebsocket.readyState) + const [websocket, setWebsocket] = useState(createWebSocket()) + const [websocketState, setWebsocketState] = useState(websocket.readyState) const [isWebsocketHealthy, setIsWebsocketHealthy] = useState(false) const setConnectionErrorCount = useState(0)[1] const currentWallet = useCurrentWallet() diff --git a/src/setupTests.ts b/src/setupTests.ts index c5d08cf48..c81f836ac 100644 --- a/src/setupTests.ts +++ b/src/setupTests.ts @@ -23,11 +23,8 @@ global.__DEV__.addToAppSettings = () => { ) } ;(function setupWebsocketServerMock() { - global.__DEV__.JM_WEBSOCKET_SERVER_MOCK = new WebSocketServer('ws://localhost/jmws', { jsonProtocol: true }) - - afterEach(() => { - // gracefully close all open connections and reset the environment between test runs - WebSocketServer.clean() + beforeAll(() => { + global.__DEV__.JM_WEBSOCKET_SERVER_MOCK = new WebSocketServer('ws://localhost/jmws', { jsonProtocol: true }) }) afterAll(() => {