-
Notifications
You must be signed in to change notification settings - Fork 388
/
object.test.ts
164 lines (148 loc) · 4.07 KB
/
object.test.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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 {type ObjectDefinition, type StringDefinition} from '../definition'
import {defineField, defineType} from '../types'
describe('object types', () => {
it('should define object schema', () => {
const objectDef = defineType({
type: 'object',
name: 'custom-object',
title: 'Custom',
icon: () => null,
description: 'Description',
initialValue: () => Promise.resolve({title: 'some title'}),
validation: (Rule) => [
Rule.required()
.required()
.custom((value) => (value?.title === 'yolo' ? true : 'Error'))
.warning(),
// @ts-expect-error greaterThan does not exist on objectRule
Rule.greaterThan(5).error(),
],
hidden: () => false,
fieldsets: [
{
name: 'fieldset',
title: 'Fieldset',
description: 'Fieldset description',
hidden: false,
readOnly: false,
options: {
collapsed: true,
collapsible: true,
columns: 2,
//TODO is this actually supported on fieldset?
modal: {type: 'dialog', width: 1},
},
},
],
groups: [{name: 'group', title: 'Group title', icon: () => null, default: true}],
preview: {select: {title: 'title', subtitle: 'title'}},
fields: [],
})
const assignableToObject: ObjectDefinition = objectDef
// @ts-expect-error object is not assignable to string
const notAssignableToString: StringDefinition = objectDef
})
it('should define document fields safely (with some compromises without defineField)', () => {
defineType({
type: 'object',
name: 'custom-object',
fields: [],
})
defineType({
type: 'object',
name: 'custom-object',
fields: [
//@ts-expect-error {} not assignable to FieldDefinition
{},
],
})
})
defineType({
type: 'object',
name: 'custom-object',
fields: [
//@ts-expect-error name is missing
{
type: 'string',
},
],
})
defineType({
type: 'object',
name: 'custom-object',
fields: [
{
type: 'string',
name: 'stringField',
},
],
})
defineType({
type: 'object',
name: 'custom-object',
fields: [
{
type: 'object',
name: 'objectField',
fields: [
{
type: 'object',
//@ts-expect-error errors on unknown prop: message is a bit off in this case, but at least is an error
unknown: '',
},
],
},
],
})
defineType({
type: 'object',
name: 'custom-object',
fields: [
{
type: 'object',
name: 'objectField',
fields: [
{
type: 'string',
name: 'stringField',
title: 'Title',
initialValue: 'string',
// validation is unfortunately broadened to the generic-rule-with-all-functions here
validation: (Rule) => Rule.greaterThan(5),
options: {
unknownOption: 'allowed',
direction: 'horizontal',
unknown: 'without-a-wrapper-unknown-options-are-allowed',
},
},
],
},
defineField({
type: 'object',
name: 'defineFieldObject',
fields: [
defineField({
type: 'string',
name: 'stringField',
title: 'Title',
initialValue: 'string',
//@ts-expect-error now this is not allowed as greaterThan is not in StringRule
validation: (Rule) => Rule.greaterThan(5),
options: {
//@ts-expect-error not allowed in this context
unknownOption: 'not-allowed',
direction: 'horizontal',
},
}),
],
}),
],
})
})
export {}