-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor ConnectionStatus from MessageList
- Loading branch information
Showing
3 changed files
with
103 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// @ts-check | ||
import React, { useContext, useState, useEffect } from 'react'; | ||
|
||
import { ChatContext, TranslationContext } from '../../context'; | ||
import CustomNotification from './CustomNotification'; | ||
|
||
/** | ||
* ConnectionStatus - Indicator that there is a connection failure | ||
* @type {React.FC<{}>} | ||
*/ | ||
const ConnectionStatus = () => { | ||
const { client } = useContext(ChatContext); | ||
const { t } = useContext(TranslationContext); | ||
const [online, setOnline] = useState(true); | ||
|
||
useEffect(() => { | ||
/** @param {import('stream-chat').Event<string>} e */ | ||
const connectionChanged = (e) => { | ||
if (e.online !== online) setOnline(/** @type {boolean} */ (e.online)); | ||
}; | ||
|
||
client.on('connection.changed', connectionChanged); | ||
return () => client.off('connection.changed', connectionChanged); | ||
}, [client, online]); | ||
|
||
return ( | ||
<CustomNotification active={!online} type="error"> | ||
{t('Connection failure, reconnecting now...')} | ||
</CustomNotification> | ||
); | ||
}; | ||
|
||
export default React.memo(ConnectionStatus); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/components/MessageList/__tests__/ConnectionStatus.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from 'react'; | ||
import testRenderer from 'react-test-renderer'; | ||
import { cleanup, render, waitFor, act } from '@testing-library/react'; | ||
import { dispatchConnectionChangedEvent, getTestClient } from 'mock-builders'; | ||
import '@testing-library/jest-dom'; | ||
|
||
import ConnectionStatus from '../ConnectionStatus'; | ||
import { Chat } from '../../Chat'; | ||
|
||
const customNotificationId = 'custom-notification'; | ||
describe('<ChatContext /> component', () => { | ||
let chatClient; | ||
beforeEach(() => { | ||
chatClient = getTestClient(); | ||
}); | ||
|
||
afterEach(cleanup); | ||
|
||
it('should render nothing by default', () => { | ||
const tree = testRenderer.create( | ||
<Chat client={chatClient}> | ||
<ConnectionStatus /> | ||
</Chat>, | ||
); | ||
|
||
expect(tree.toJSON()).toMatchInlineSnapshot(`null`); | ||
}); | ||
|
||
it('should render and hide the status based on online state', async () => { | ||
const { queryByTestId } = render( | ||
<Chat client={chatClient}> | ||
<ConnectionStatus /> | ||
</Chat>, | ||
); | ||
|
||
// default to online | ||
expect(queryByTestId(customNotificationId)).not.toBeInTheDocument(); | ||
|
||
// offline | ||
act(() => dispatchConnectionChangedEvent(chatClient, false)); | ||
await waitFor(() => { | ||
expect(queryByTestId(customNotificationId)).toBeInTheDocument(); | ||
}); | ||
|
||
// online again | ||
act(() => dispatchConnectionChangedEvent(chatClient, true)); | ||
await waitFor(() => { | ||
expect(queryByTestId(customNotificationId)).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should render a proper message when client is offline', async () => { | ||
const { queryByTestId } = render( | ||
<Chat client={chatClient}> | ||
<ConnectionStatus /> | ||
</Chat>, | ||
); | ||
|
||
// offline | ||
act(() => dispatchConnectionChangedEvent(chatClient, false)); | ||
await waitFor(() => { | ||
expect(queryByTestId(customNotificationId)).toBeInTheDocument(); | ||
expect(queryByTestId(customNotificationId)).toHaveTextContent( | ||
'Connection failure, reconnecting now...', | ||
); | ||
}); | ||
}); | ||
}); |