-
Notifications
You must be signed in to change notification settings - Fork 389
/
reference.test.ts
92 lines (84 loc) · 2.68 KB
/
reference.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
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 BooleanDefinition, type ReferenceDefinition} from '../definition'
import {defineType} from '../types'
describe('reference types', () => {
describe('defineType', () => {
it('should define reference schema', () => {
const referenceDef = defineType({
type: 'reference',
name: 'custom-reference',
title: 'Custom PTE',
icon: () => null,
description: 'Description',
initialValue: () => Promise.resolve({_ref: 'yolo'}),
validation: (Rule) => [
Rule.required()
.required()
.custom((value) => (value?._ref?.toLowerCase() ? 'Error' : true))
.warning(),
// @ts-expect-error greaterThan does not exist on referenceRule
Rule.greaterThan(5).error(),
],
hidden: () => false,
readOnly: () => false,
weak: true,
to: [{type: 'crewMember'}],
options: {
disableNew: false,
filter: ({document, parent, parentPath}) =>
Promise.resolve({
filter: '*[field==$param]',
params: {
param: document._type,
},
}),
},
})
const assignableToreference: ReferenceDefinition = referenceDef
// @ts-expect-error reference is not assignable to boolean
const notAssignableToBoolean: BooleanDefinition = referenceDef
})
it('should allow reference without filter in options', () => {
defineType({
type: 'reference',
name: 'custom-reference',
title: 'Custom PTE',
to: [{type: 'crewMember'}],
options: {
disableNew: false,
},
})
})
it('should not allow filterParams when filter is function', () => {
defineType({
type: 'reference',
name: 'custom-reference',
title: 'Custom PTE',
to: [{type: 'crewMember'}],
options: {
//@ts-expect-error function is not assignable to string (when filterParams is provided, filter must be string)
filter: () => ({}),
filterParams: {not: 'allowed'},
},
})
})
it('should allow filterParams when filter is string', () => {
defineType({
type: 'reference',
name: 'custom-reference',
title: 'Custom PTE',
to: [{type: 'crewMember'}],
options: {
filter: '*',
filterParams: {is: 'allowed'},
},
})
})
})
})
export {}