-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): initial tests for hooks (#40)
- Loading branch information
Showing
9 changed files
with
254 additions
and
34 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
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 |
---|---|---|
@@ -1,40 +1,179 @@ | ||
import { MapeoClientApi } from '@comapeo/ipc' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { renderHook } from '@testing-library/react' | ||
import { assert, assertType, test } from 'vitest' | ||
import { act, renderHook, waitFor } from '@testing-library/react' | ||
import { assert, describe, test } from 'vitest' | ||
|
||
import { useClientApi } from '../../src/hooks/client.js' | ||
import { | ||
useClientApi, | ||
useIsArchiveDevice, | ||
useOwnDeviceInfo, | ||
useSetIsArchiveDevice, | ||
useSetOwnDeviceInfo, | ||
} from '../../src/index.js' | ||
import { setupCoreIpc } from '../helpers/ipc.js' | ||
import { createClientApiWrapper } from '../helpers/react.js' | ||
|
||
test('useClientApi() throws when ClientApiProvider is not set up', () => { | ||
const queryClient = new QueryClient() | ||
|
||
assert.throws(() => { | ||
renderHook(() => useClientApi(), { | ||
wrapper: ({ children }) => { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
) | ||
}, | ||
describe('useClientApi()', () => { | ||
test('throws when ClientApiProvider is not set up', () => { | ||
const queryClient = new QueryClient() | ||
|
||
assert.throws(() => { | ||
renderHook(() => useClientApi(), { | ||
wrapper: ({ children }) => { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
) | ||
}, | ||
}) | ||
}, 'No client API set') | ||
}) | ||
|
||
test('returns client api instance when ClientApiProvider is set up properly', (t) => { | ||
const { client, cleanup } = setupCoreIpc() | ||
|
||
t.onTestFinished(() => { | ||
return cleanup() | ||
}) | ||
}, 'No client API set') | ||
}) | ||
|
||
test('useClientApi() returns client api instance when ClientApiProvider is set up properly', (t) => { | ||
const { client, cleanup } = setupCoreIpc() | ||
const { result } = renderHook(() => useClientApi(), { | ||
wrapper: createClientApiWrapper({ clientApi: client }), | ||
}) | ||
|
||
t.onTestFinished(() => { | ||
return cleanup() | ||
assert.isDefined(result.current, 'client is set up properly') | ||
}) | ||
}) | ||
|
||
describe('device info', () => { | ||
test('basic read and write', async (t) => { | ||
const { client, cleanup } = setupCoreIpc() | ||
|
||
t.onTestFinished(() => { | ||
return cleanup() | ||
}) | ||
|
||
const { result } = renderHook(() => useClientApi(), { | ||
wrapper: createClientApiWrapper({ clientApi: client }), | ||
const queryClient = new QueryClient() | ||
|
||
const wrapper = createClientApiWrapper({ clientApi: client, queryClient }) | ||
|
||
const readHook = renderHook(() => useOwnDeviceInfo(), { wrapper }) | ||
const writeHook = renderHook(() => useSetOwnDeviceInfo(), { wrapper }) | ||
|
||
// Since the read hook is Suspense-based, we need to simulate waiting for the data to initially resolve | ||
await waitFor(() => { | ||
assert.isNotNull(readHook.result.current.data) | ||
}) | ||
|
||
const expectedDeviceId = await client.deviceId() | ||
|
||
// 1. Initial state | ||
assert.deepStrictEqual( | ||
readHook.result.current, | ||
{ | ||
data: { | ||
deviceId: expectedDeviceId, | ||
deviceType: 'device_type_unspecified', | ||
}, | ||
isRefetching: false, | ||
error: null, | ||
}, | ||
'read hook has expected initial state', | ||
) | ||
assert.deepStrictEqual( | ||
writeHook.result.current.status, | ||
'idle', | ||
'write hook has expected initial status', | ||
) | ||
|
||
// 2. Simulate a user interaction | ||
act(() => { | ||
writeHook.result.current.mutate({ | ||
name: 'my device', | ||
deviceType: 'tablet', | ||
}) | ||
}) | ||
|
||
// 3. Write hook lifecycle | ||
// TODO: Ideally check for status === 'pending' before this | ||
await waitFor(() => { | ||
assert.strictEqual(writeHook.result.current.status, 'success') | ||
}) | ||
|
||
// 4. Read hook lifecycle | ||
// TODO: Ideally check for isRefetching === true before this | ||
await waitFor(() => { | ||
assert.strictEqual(readHook.result.current.isRefetching, false) | ||
}) | ||
|
||
assert.deepStrictEqual( | ||
readHook.result.current, | ||
{ | ||
isRefetching: false, | ||
error: null, | ||
data: { | ||
deviceId: expectedDeviceId, | ||
name: 'my device', | ||
deviceType: 'tablet', | ||
}, | ||
}, | ||
'read hook has expected updated state', | ||
) | ||
}) | ||
}) | ||
|
||
assertType<MapeoClientApi>(result.current) | ||
describe('is archive device', () => { | ||
test('basic read and write', async (t) => { | ||
const { client, cleanup } = setupCoreIpc() | ||
|
||
assert.isDefined(result.current, 'client is set up properly') | ||
t.onTestFinished(() => { | ||
return cleanup() | ||
}) | ||
|
||
const queryClient = new QueryClient() | ||
|
||
const wrapper = createClientApiWrapper({ clientApi: client, queryClient }) | ||
|
||
const readHook = renderHook(() => useIsArchiveDevice(), { wrapper }) | ||
const writeHook = renderHook(() => useSetIsArchiveDevice(), { wrapper }) | ||
|
||
// Since the hook is Suspense-based, we need to simulate waiting for the data to initially resolve | ||
await waitFor(() => { | ||
assert.isNotNull(readHook.result.current.data) | ||
}) | ||
|
||
// 1. Initial state | ||
assert.deepStrictEqual( | ||
readHook.result.current, | ||
{ | ||
data: true, | ||
error: null, | ||
isRefetching: false, | ||
}, | ||
'read hook has expected initial state', | ||
) | ||
assert.deepStrictEqual( | ||
writeHook.result.current.status, | ||
'idle', | ||
'write hook has expected initial status', | ||
) | ||
|
||
// 2. Simulate a user interaction | ||
act(() => { | ||
writeHook.result.current.mutate({ isArchiveDevice: false }) | ||
}) | ||
|
||
// 3. Write hook lifecycle | ||
// TODO: Ideally check for status === 'pending' before this | ||
await waitFor(() => { | ||
assert.strictEqual(writeHook.result.current.status, 'success') | ||
}) | ||
|
||
// 4. Read hook lifecycle | ||
// TODO: Ideally check for isRefetching === true before this | ||
await waitFor(() => { | ||
assert.strictEqual(readHook.result.current.isRefetching, false) | ||
}) | ||
|
||
assert.strictEqual(readHook.result.current.data, false, 'data has updated') | ||
}) | ||
}) |
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,57 @@ | ||
import { QueryClient } from '@tanstack/react-query' | ||
import { renderHook, waitFor } from '@testing-library/react' | ||
import { assert, test } from 'vitest' | ||
|
||
import { useMapStyleUrl } from '../../src/index.js' | ||
import { setupCoreIpc } from '../helpers/ipc.js' | ||
import { createClientApiWrapper } from '../helpers/react.js' | ||
|
||
test('basic read works', async (t) => { | ||
const { client, cleanup, fastifyController } = setupCoreIpc() | ||
|
||
fastifyController.start() | ||
|
||
t.onTestFinished(() => { | ||
return cleanup() | ||
}) | ||
|
||
const queryClient = new QueryClient() | ||
|
||
const wrapper = createClientApiWrapper({ clientApi: client, queryClient }) | ||
|
||
const mapStyleUrlHook = renderHook< | ||
ReturnType<typeof useMapStyleUrl>, | ||
Parameters<typeof useMapStyleUrl>[0] | ||
>(({ refreshToken } = {}) => useMapStyleUrl({ refreshToken }), { | ||
wrapper, | ||
}) | ||
|
||
await waitFor(() => { | ||
assert(mapStyleUrlHook.result.current.data !== null) | ||
}) | ||
|
||
const url1 = new URL(mapStyleUrlHook.result.current.data) | ||
|
||
assert(url1, 'map style url hook returns valid URL') | ||
|
||
mapStyleUrlHook.rerender({ refreshToken: 'abc_123' }) | ||
|
||
// TODO: Ideally check for isRefetching === true before this | ||
await waitFor(() => { | ||
assert(mapStyleUrlHook.result.current.isRefetching === false) | ||
}) | ||
|
||
const url2 = new URL(mapStyleUrlHook.result.current.data) | ||
|
||
assert.notStrictEqual( | ||
url2.href, | ||
url1.href, | ||
'map style url hook updates after changing refresh token option', | ||
) | ||
|
||
assert.strictEqual( | ||
url2.searchParams.get('refresh_token'), | ||
'abc_123', | ||
'map style url has search param containing refresh token', | ||
) | ||
}) |
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,13 @@ | ||
import { notifyManager } from '@tanstack/react-query' | ||
import { act, cleanup } from '@testing-library/react' | ||
import { afterEach } from 'vitest' | ||
|
||
afterEach(() => { | ||
// https://testing-library.com/docs/react-testing-library/api#cleanup | ||
cleanup() | ||
}) | ||
|
||
// Wrap notifications with act to make sure React knows about React Query updates | ||
notifyManager.setNotifyFunction((fn) => { | ||
act(fn) | ||
}) |
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