Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

schema-view.test.ts

52 lines1.4 KB

1import { describe, expect, it } from 'vitest'
2​
3import { schemaToRows } from '#/lib/schema-view.ts'
4​
5describe('schemaToRows', () => {
6 it('links a property type to its $def and lists that definition', () => {
7 const table = schemaToRows({
8 type: 'object',
9 properties: {
10 item: { $ref: '#/$defs/CreateContentGenerationContentItem' },
11 },
12 required: ['item'],
13 $defs: {
14 CreateContentGenerationContentItem: {
15 type: 'object',
16 properties: {
17 type: { type: 'string' },
18 },
19 required: ['type'],
20 },
21 },
22 })
23​
24 expect(table.rows[0]?.typeLabel).toBe('CreateContentGenerationContentItem')
25 expect(table.rows[0]?.typeRefs).toEqual([
26 'CreateContentGenerationContentItem',
27 ])
28 expect(table.definitions.map((def) => def.name)).toEqual([
29 'CreateContentGenerationContentItem',
30 ])
31 expect(table.definitions[0]?.children.map((row) => row.name)).toEqual([
32 'type',
33 ])
34 })
35​
36 it('prefers the longer $def name when one name contains another', () => {
37 const table = schemaToRows({
38 type: 'object',
39 properties: {
40 item: { $ref: '#/$defs/ContentItem' },
41 },
42 $defs: {
43 Content: { type: 'string' },
44 ContentItem: {
45 type: 'object',
46 properties: { text: { type: 'string' } },
47 },
48 },
49 })
50 expect(table.rows[0]?.typeRefs).toEqual(['ContentItem'])
51 })
52})