-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2035 from GetStream/develop
v10.8.5
- Loading branch information
Showing
2 changed files
with
74 additions
and
15 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/components/MessageInput/hooks/__tests__/useCooldownTimer.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,58 @@ | ||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useCooldownTimer } from '../useCooldownTimer'; | ||
|
||
import { ChannelStateProvider, ChatProvider } from '../../../../context'; | ||
import { getTestClient } from '../../../../mock-builders'; | ||
|
||
async function renderUseCooldownTimerHook({ channel, chatContext }) { | ||
const client = await getTestClient(); | ||
|
||
const wrapper = ({ children }) => ( | ||
<ChatProvider value={{ client, ...chatContext }}> | ||
<ChannelStateProvider value={{ channel }}>{children}</ChannelStateProvider> | ||
</ChatProvider> | ||
); | ||
return renderHook(useCooldownTimer, { wrapper }); | ||
} | ||
|
||
const cid = 'cid'; | ||
const cooldown = 30; | ||
describe('useCooldownTimer', () => { | ||
it('should set remaining cooldown time to if no channel.cooldown', async () => { | ||
const channel = { cid }; | ||
const chatContext = { latestMessageDatesByChannels: {} }; | ||
const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); | ||
expect(result.current.cooldownRemaining).toBe(0); | ||
}); | ||
it('should set remaining cooldown time to 0 if skip-slow-mode is among own_capabilities', async () => { | ||
const channel = { cid, data: { cooldown, own_capabilities: ['skip-slow-mode'] } }; | ||
const chatContext = { latestMessageDatesByChannels: { cid: new Date() } }; | ||
const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); | ||
expect(result.current.cooldownRemaining).toBe(0); | ||
}); | ||
it('should set remaining cooldown time to 0 if no previous messages sent', async () => { | ||
const channel = { cid, data: { cooldown } }; | ||
const chatContext = { latestMessageDatesByChannels: {} }; | ||
const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); | ||
expect(result.current.cooldownRemaining).toBe(0); | ||
}); | ||
it('should set remaining cooldown time to 0 if previous messages sent earlier than channel.cooldown', async () => { | ||
const channel = { cid, data: { cooldown } }; | ||
const chatContext = { latestMessageDatesByChannels: { cid: new Date('1970-1-1') } }; | ||
const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); | ||
expect(result.current.cooldownRemaining).toBe(0); | ||
}); | ||
it('should set remaining cooldown time to time left from previous messages sent', async () => { | ||
const channel = { cid, data: { cooldown } }; | ||
const lastSentSecondsAgo = 5; | ||
const chatContext = { | ||
latestMessageDatesByChannels: { | ||
cid: new Date(new Date().getTime() - lastSentSecondsAgo * 1000), | ||
}, | ||
}; | ||
const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); | ||
expect(result.current.cooldownRemaining).toBe(cooldown - lastSentSecondsAgo); | ||
}); | ||
}); |
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