Files
faunosteo/packages/supabase/tests/anatomical-sheets.test.ts
T

76 lines
2.2 KiB
TypeScript

import { publishSheetVersion } from '../src'
import type { FaunosteoClient } from '../src'
/**
* Stub of the two client calls publishSheetVersion makes. The real RPC is
* covered by supabase/tests/005_anatomical_sheets.sql; here the point is the
* order of the gate: no RPC call unless the document is publishable.
*/
function stubClient(content: unknown) {
const rpcCalls: unknown[] = []
const client = {
from: () => ({
select: () => ({
eq: () => ({
single: async () => ({ data: { content }, error: null }),
}),
}),
}),
rpc: async (name: string, args: unknown) => {
rpcCalls.push([name, args])
return { data: 'published-id', error: null }
},
}
return { client: client as unknown as FaunosteoClient, rpcCalls }
}
const publishable = {
schema_version: 2,
species_id: 'equine',
name: { fr: 'Test', en: 'Test' },
description: {},
scope_disclaimer: {},
attribution: { authors: [], license: null, notes: null },
vertebral_config: { regions: [] },
references: [],
categories: [],
views: [
{
id: 'view-main',
kind: 'main',
name: { fr: 'Vue', en: 'View' },
svg_asset_id: '11111111-1111-4111-8111-111111111111',
view_box: null,
},
],
annotations: [],
finding_zones: [],
}
describe('publishSheetVersion', () => {
it('calls the RPC once the document passes shape and integrity checks', async () => {
const { client, rpcCalls } = stubClient(publishable)
await expect(publishSheetVersion(client, 'v1')).resolves.toBe(
'published-id',
)
expect(rpcCalls).toEqual([['publish_sheet_version', { version_id: 'v1' }]])
})
it('refuses a document with integrity errors before any RPC', async () => {
const { client, rpcCalls } = stubClient({
...publishable,
views: [{ ...publishable.views[0], svg_asset_id: null }],
})
await expect(publishSheetVersion(client, 'v1')).rejects.toThrow(
/view\.asset\.missing/,
)
expect(rpcCalls).toEqual([])
})
it('refuses a document that does not match the schema', async () => {
const { client, rpcCalls } = stubClient({ schema_version: 1 })
await expect(publishSheetVersion(client, 'v1')).rejects.toThrow()
expect(rpcCalls).toEqual([])
})
})