|
| 1 | +import React from 'react'; |
| 2 | +import { renderHook } from '@testing-library/react-hooks'; |
| 3 | + |
| 4 | +import { useCooldownTimer } from '../useCooldownTimer'; |
| 5 | + |
| 6 | +import { ChannelStateProvider, ChatProvider } from '../../../../context'; |
| 7 | +import { getTestClient } from '../../../../mock-builders'; |
| 8 | + |
| 9 | +async function renderUseCooldownTimerHook({ channel, chatContext }) { |
| 10 | + const client = await getTestClient(); |
| 11 | + |
| 12 | + const wrapper = ({ children }) => ( |
| 13 | + <ChatProvider value={{ client, ...chatContext }}> |
| 14 | + <ChannelStateProvider value={{ channel }}>{children}</ChannelStateProvider> |
| 15 | + </ChatProvider> |
| 16 | + ); |
| 17 | + return renderHook(useCooldownTimer, { wrapper }); |
| 18 | +} |
| 19 | + |
| 20 | +const cid = 'cid'; |
| 21 | +const cooldown = 30; |
| 22 | +describe('useCooldownTimer', () => { |
| 23 | + it('should set remaining cooldown time to if no channel.cooldown', async () => { |
| 24 | + const channel = { cid }; |
| 25 | + const chatContext = { latestMessageDatesByChannels: {} }; |
| 26 | + const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); |
| 27 | + expect(result.current.cooldownRemaining).toBe(0); |
| 28 | + }); |
| 29 | + it('should set remaining cooldown time to 0 if skip-slow-mode is among own_capabilities', async () => { |
| 30 | + const channel = { cid, data: { cooldown, own_capabilities: ['skip-slow-mode'] } }; |
| 31 | + const chatContext = { latestMessageDatesByChannels: { cid: new Date() } }; |
| 32 | + const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); |
| 33 | + expect(result.current.cooldownRemaining).toBe(0); |
| 34 | + }); |
| 35 | + it('should set remaining cooldown time to 0 if no previous messages sent', async () => { |
| 36 | + const channel = { cid, data: { cooldown } }; |
| 37 | + const chatContext = { latestMessageDatesByChannels: {} }; |
| 38 | + const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); |
| 39 | + expect(result.current.cooldownRemaining).toBe(0); |
| 40 | + }); |
| 41 | + it('should set remaining cooldown time to 0 if previous messages sent earlier than channel.cooldown', async () => { |
| 42 | + const channel = { cid, data: { cooldown } }; |
| 43 | + const chatContext = { latestMessageDatesByChannels: { cid: new Date('1970-1-1') } }; |
| 44 | + const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); |
| 45 | + expect(result.current.cooldownRemaining).toBe(0); |
| 46 | + }); |
| 47 | + it('should set remaining cooldown time to time left from previous messages sent', async () => { |
| 48 | + const channel = { cid, data: { cooldown } }; |
| 49 | + const lastSentSecondsAgo = 5; |
| 50 | + const chatContext = { |
| 51 | + latestMessageDatesByChannels: { |
| 52 | + cid: new Date(new Date().getTime() - lastSentSecondsAgo * 1000), |
| 53 | + }, |
| 54 | + }; |
| 55 | + const { result } = await renderUseCooldownTimerHook({ channel, chatContext }); |
| 56 | + expect(result.current.cooldownRemaining).toBe(cooldown - lastSentSecondsAgo); |
| 57 | + }); |
| 58 | +}); |
0 commit comments