-
Notifications
You must be signed in to change notification settings - Fork 388
/
generic.test.tsx
137 lines (120 loc) · 3.12 KB
/
generic.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
* use of ts-expect-error serves the same purpose - TypeScript is the testrunner here
*/
import {defineArrayMember, defineField, defineType} from '../types'
describe('common type test', () => {
it('should capture literal string type', () => {
const stringDef = defineType({
type: 'string',
name: 'custom-string',
})
let type: 'string' = stringDef.type
let name: 'custom-string' = stringDef.name
const stringField = defineField({
type: 'string',
name: 'custom-string',
})
type = stringField.type
name = stringField.name
const stringArrayOf = defineArrayMember({
type: 'string',
name: 'custom-string',
})
const arrayOfType: 'string' = stringArrayOf.type
const arrayOfName: 'custom-string' | undefined = stringArrayOf.name
})
it('should support using all the define functions within each-other', () => {
defineType({
type: 'object',
name: 'custom-object',
fields: [
defineField({
type: 'array',
name: 'arrayField',
title: 'Things',
of: [
defineArrayMember({
type: 'object',
name: 'type-name-in-array',
fields: [defineField({type: 'string', name: 'title', title: 'Title'})],
}),
],
}),
],
})
})
it('should support optional strictness', () => {
defineType({
type: 'string',
name: 'custom-string',
//@ts-expect-error we are in strict mode, so this is not allowed
doc18n: true,
})
defineType(
{
type: 'string',
name: 'custom-string',
//@ts-expect-error we are in strict mode, so this is not allowed
doc18n: true,
},
{strict: true},
)
defineType(
{
type: 'string',
name: 'custom-string',
// strict mode disabled so unknown props are types as any
doc18n: true,
},
{strict: false},
)
defineType(
{
type: 'string',
name: 'custom-string',
options: {
custom: true,
},
},
{strict: false},
)
})
it('should fail for missing fields', () => {
defineType({
//@ts-expect-error did you mean to write type?
type1: 'string',
})
//@ts-expect-error name is missing
defineType({
type: 'string',
})
defineType({
type: 'string',
name: 'custom-string',
//@ts-expect-error disallow unknown props
unknownProp: false,
})
})
it('should allow ReactElement for description', () => {
defineType({
type: 'text',
name: 'text',
description: 'Some text',
})
defineType({
type: 'text',
name: 'text',
description: <strong>Some like it bold</strong>,
})
defineType({
type: 'text',
name: 'text',
//@ts-expect-error ReactElement but not ReactNode
description: () => <strong>Render function not supported </strong>,
})
})
})
export {}