Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { describe, expect, it, vi } from 'vitest'

import { project } from '@oxide/api-mocks'

import { apiq, useApiMutation } from '..'
import { api, q, useApiMutation } from '..'
import type { DiskCreate } from '../__generated__/Api'
import { overrideOnce } from '../../../test/unit/server'

Expand All @@ -35,16 +35,17 @@ export function Wrapper({ children }: { children: React.ReactNode }) {

const config = { wrapper: Wrapper }

const renderProjectList = () => renderHook(() => useQuery(apiq('projectList', {})), config)
const renderProjectList = () => renderHook(() => useQuery(q(api.projectList, {})), config)

// 503 is a special key in the MSW server that returns a 503
const renderGetProject503 = () =>
renderHook(
() => useQuery(apiq('projectView', { path: { project: 'project-error-503' } })),
() => useQuery(q(api.projectView, { path: { project: 'project-error-503' } })),
config
)

const renderCreateProject = () => renderHook(() => useApiMutation('projectCreate'), config)
const renderCreateProject = () =>
renderHook(() => useApiMutation(api.projectCreate), config)

const createParams = {
body: { name: 'abc', description: '', hello: 'a' },
Expand Down Expand Up @@ -120,7 +121,7 @@ describe('useApiQuery', () => {
function BadApiCall() {
try {
// oxlint-disable-next-line react-hooks/rules-of-hooks
useQuery(apiq('projectView', { path: { project: 'nonexistent' } }))
useQuery(q(api.projectView, { path: { project: 'nonexistent' } }))
} catch (e) {
onError(e)
}
Expand All @@ -143,8 +144,8 @@ describe('useApiQuery', () => {
const { result } = renderHook(
() =>
useQuery(
apiq(
'projectView',
q(
api.projectView,
{ path: { project: 'nonexistent' } },
{ throwOnError: false }
)
Expand Down Expand Up @@ -205,7 +206,7 @@ describe('useApiMutation', () => {
}

it('passes through raw response', async () => {
const { result } = renderHook(() => useApiMutation('diskCreate'), config)
const { result } = renderHook(() => useApiMutation(api.diskCreate), config)

act(() => result.current.mutate(diskCreate404Params))

Expand All @@ -219,7 +220,7 @@ describe('useApiMutation', () => {
})

it('parses error json if possible', async () => {
const { result } = renderHook(() => useApiMutation('diskCreate'), config)
const { result } = renderHook(() => useApiMutation(api.diskCreate), config)

act(() => result.current.mutate(diskCreate404Params))

Expand Down Expand Up @@ -286,3 +287,12 @@ describe('useApiMutation', () => {
})
})
})

// we're relying on the name property of the API method for the queryKey, so we
// need to make sure nothing changes in the generated client to cause the API
// methods to not have a name
it('apiq queryKey', () => {
const params = { path: { silo: 'abc' } }
const queryOptions = q(api.siloView, { path: { silo: 'abc' } })
expect(queryOptions.queryKey).toEqual(['siloView', params])
})
2 changes: 1 addition & 1 deletion app/api/__tests__/safety.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const grepFiles = (s: string) =>
it('mock-api is only referenced in test files', () => {
expect(grepFiles('api-mocks')).toMatchInlineSnapshot(`
[
"app/api/__tests__/hooks.spec.tsx",
"app/api/__tests__/client.spec.tsx",
"mock-api/msw/db.ts",
"test/e2e/instance-create.e2e.ts",
"test/e2e/inventory.e2e.ts",
Expand Down
Loading
Loading