This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #65 from ably-labs/add-useConnectionState-and-useC…
…hannelState feat: add `useChannelStateListener` and `useConnectionStateListener` hooks
- Loading branch information
Showing
11 changed files
with
565 additions
and
6 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 |
---|---|---|
|
@@ -102,3 +102,5 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
.DS_Store |
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
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
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
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
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,133 @@ | ||
import React from 'react'; | ||
import { it, beforeEach, describe, expect } from 'vitest'; | ||
import { useChannelStateListener } from './useChannelStateListener'; | ||
import { useState } from 'react'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { FakeAblySdk, FakeAblyChannels } from '../fakes/ably'; | ||
import { Types } from 'ably'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { AblyProvider } from '../AblyProvider'; | ||
|
||
function renderInCtxProvider( | ||
client: FakeAblySdk, | ||
children: React.ReactNode | React.ReactNode[] | ||
) { | ||
return render( | ||
<AblyProvider client={client as unknown as Types.RealtimePromise}> | ||
{children} | ||
</AblyProvider> | ||
); | ||
} | ||
|
||
describe('useChannelStateListener', () => { | ||
let channels: FakeAblyChannels; | ||
let ablyClient: FakeAblySdk; | ||
|
||
beforeEach(() => { | ||
channels = new FakeAblyChannels(['blah']); | ||
ablyClient = new FakeAblySdk().connectTo(channels); | ||
}); | ||
|
||
it('can register a channel state listener for all state changes', async () => { | ||
renderInCtxProvider( | ||
ablyClient, | ||
<UseChannelStateListenerComponent></UseChannelStateListenerComponent> | ||
); | ||
|
||
act(() => { | ||
ablyClient.channels | ||
.get('blah') | ||
.emit('attached', { current: 'attached' }); | ||
}); | ||
|
||
expect(screen.getAllByRole('channelState')[0].innerHTML).toEqual( | ||
'attached' | ||
); | ||
}); | ||
|
||
it('can register a channel state listener for named state changes', async () => { | ||
renderInCtxProvider( | ||
ablyClient, | ||
<UseChannelStateListenerComponentNamedEvents | ||
event={'failed'} | ||
></UseChannelStateListenerComponentNamedEvents> | ||
); | ||
|
||
act(() => { | ||
ablyClient.channels | ||
.get('blah') | ||
.emit('attached', { current: 'attached' }); | ||
}); | ||
|
||
expect(screen.getAllByRole('channelState')[0].innerHTML).toEqual( | ||
'initialized' | ||
); | ||
|
||
act(() => { | ||
ablyClient.channels | ||
.get('blah') | ||
.emit('failed', { current: 'failed' }); | ||
}); | ||
|
||
expect(screen.getAllByRole('channelState')[0].innerHTML).toEqual( | ||
'failed' | ||
); | ||
}); | ||
|
||
it('can register a channel state listener for an array of named state changes', async () => { | ||
renderInCtxProvider( | ||
ablyClient, | ||
<UseChannelStateListenerComponentNamedEvents | ||
event={['failed', 'suspended']} | ||
></UseChannelStateListenerComponentNamedEvents> | ||
); | ||
|
||
act(() => { | ||
ablyClient.channels | ||
.get('blah') | ||
.emit('attached', { current: 'attached' }); | ||
}); | ||
|
||
expect(screen.getAllByRole('channelState')[0].innerHTML).toEqual( | ||
'initialized' | ||
); | ||
|
||
act(() => { | ||
ablyClient.channels | ||
.get('blah') | ||
.emit('suspended', { current: 'suspended' }); | ||
}); | ||
|
||
expect(screen.getAllByRole('channelState')[0].innerHTML).toEqual( | ||
'suspended' | ||
); | ||
}); | ||
}); | ||
|
||
const UseChannelStateListenerComponent = () => { | ||
const [channelState, setChannelState] = | ||
useState<Types.ChannelState>('initialized'); | ||
|
||
useChannelStateListener('blah', (stateChange) => { | ||
setChannelState(stateChange.current); | ||
}); | ||
|
||
return <p role="channelState">{channelState}</p>; | ||
}; | ||
|
||
interface UseChannelStateListenerComponentNamedEventsProps { | ||
event: Types.ChannelState | Types.ChannelState[]; | ||
} | ||
|
||
const UseChannelStateListenerComponentNamedEvents = ({ | ||
event, | ||
}: UseChannelStateListenerComponentNamedEventsProps) => { | ||
const [channelState, setChannelState] = | ||
useState<Types.ChannelState>('initialized'); | ||
|
||
useChannelStateListener('blah', event, (stateChange) => { | ||
setChannelState(stateChange.current); | ||
}); | ||
|
||
return <p role="channelState">{channelState}</p>; | ||
}; |
Oops, something went wrong.