Skip to content

Commit

Permalink
add tests for useParallel
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick Terrill committed May 30, 2024
1 parent 64bfad8 commit 51198d0
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions packages/react/src/__tests__/useParallel-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react'
import { renderHook, act, waitFor, waitForNextUpdate } from '@testing-library/react'
import { useParallel, ParallelProvider } from '../index'

const TestAccount = { id: 'test' }

const ParallelMock = {
getProfile: (cb, _eb) => cb(TestAccount),
getLoginStatus: (cb) => cb({ status: 'connected' }),
subscribe: () => null,
unsubscribe: () => null,
_appendLoadContext: () => null,
login: () => 'login mock',
logout: () => 'logout mock',
}

const ParallelMockWithLoginError = {
...ParallelMock,
getLoginStatus: (cb) => cb({ error: 'test error' }),
}

describe('The useParallel hook', () => {
test('returns the resolved Parallel client from the context', async () => {
const wrapper = ({ children }) => (
<ParallelProvider parallel={Promise.resolve(ParallelMock)}>{children}</ParallelProvider>
)
const { result } = renderHook(() => useParallel(), { wrapper })

// The Provider takes a Promise for the Parallel client, so we wait until its loaded
await waitFor(() => expect(result.current).toMatchObject({ isLoaded: true }))

// make sure the hook returns the expected shape
expect(result.current).toMatchObject({
isLoaded: true,
parallel: ParallelMock,
error: undefined,
loginStatus: { status: 'connected' },
getProfile: expect.any(Function),
login: ParallelMock.login,
logout: ParallelMock.logout,
})
})

test('wraps a Promise around the getProfile callback', async () => {
const wrapper = ({ children }) => (
<ParallelProvider parallel={Promise.resolve(ParallelMock)}>{children}</ParallelProvider>
)
const { result } = renderHook(() => useParallel(), { wrapper })

// wait to load
await waitFor(() => expect(result.current).toMatchObject({ isLoaded: true }))
act(() => {
result.current.getProfile().then((profile) => {
expect(profile).toMatchObject(TestAccount)
})
})
})

test('indicates a login error', async () => {
const wrapper = ({ children }) => (
<ParallelProvider parallel={Promise.resolve(ParallelMockWithLoginError)}>{children}</ParallelProvider>
)
const { result } = renderHook(() => useParallel(), { wrapper })

// wait to load with an error status
await waitFor(() => expect(result.current).toMatchObject({ error: 'test error' }))
expect(result.current).toMatchObject({
isLoaded: true,
error: 'test error',
loginStatus: { error: 'test error' },
})
})
})

0 comments on commit 51198d0

Please sign in to comment.