@remoteoss/json-schema-form 0.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/index.cjs +1207 -0
- package/dist/index.js +1176 -0
- package/dist/standalone.js +12580 -0
- package/package.json +79 -0
- package/src/tests/createHeadlessForm.customValidations.test.jsx +799 -0
- package/src/tests/createHeadlessForm.test.js +3343 -0
- package/src/tests/helpers.custom.js +223 -0
- package/src/tests/helpers.js +1983 -0
- package/src/tests/internals.helpers.test.js +175 -0
- package/src/tests/utils.test.jsx +32 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import * as Yup from 'yup';
|
|
2
|
+
|
|
3
|
+
import { yupToFormErrors } from '../helpers';
|
|
4
|
+
import { getFieldDescription, pickXKey } from '../internals/helpers';
|
|
5
|
+
|
|
6
|
+
describe('getFieldDescription()', () => {
|
|
7
|
+
it('returns no description', () => {
|
|
8
|
+
const descriptionField = getFieldDescription();
|
|
9
|
+
expect(descriptionField).toEqual({});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('returns the description from the node', () => {
|
|
13
|
+
const node = { description: 'a description' };
|
|
14
|
+
const customProperties = {};
|
|
15
|
+
const descriptionField = getFieldDescription(node, customProperties);
|
|
16
|
+
expect(descriptionField).toEqual({ description: 'a description' });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('with customProperties', () => {
|
|
20
|
+
it('given no match, returns no description', () => {
|
|
21
|
+
const node = {};
|
|
22
|
+
const customProperties = { a_property: 'a_property' };
|
|
23
|
+
const descriptionField = getFieldDescription(node, customProperties);
|
|
24
|
+
|
|
25
|
+
expect(descriptionField).toEqual({});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('returns the description from customProperties', () => {
|
|
29
|
+
const node = { description: 'a description' };
|
|
30
|
+
const customProperties = { description: 'a custom description' };
|
|
31
|
+
|
|
32
|
+
const descriptionField = getFieldDescription(node, customProperties);
|
|
33
|
+
|
|
34
|
+
expect(descriptionField).toEqual({ description: 'a custom description' });
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('with x-jsf-presentation attribute', () => {
|
|
39
|
+
it('returns x-jsf-presentation given no base description', () => {
|
|
40
|
+
const node = {
|
|
41
|
+
'x-jsf-presentation': { description: 'a presentation description' },
|
|
42
|
+
};
|
|
43
|
+
const customProperties = {};
|
|
44
|
+
const descriptionField = getFieldDescription(node, customProperties);
|
|
45
|
+
|
|
46
|
+
expect(descriptionField).toEqual({
|
|
47
|
+
presentation: { description: 'a presentation description' },
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('returns presentation overriding the base description', () => {
|
|
52
|
+
const node = {
|
|
53
|
+
description: 'a description',
|
|
54
|
+
'x-jsf-presentation': { description: 'a presentation description' },
|
|
55
|
+
};
|
|
56
|
+
const customProperties = {};
|
|
57
|
+
const descriptionField = getFieldDescription(node, customProperties);
|
|
58
|
+
|
|
59
|
+
expect(descriptionField).toEqual({
|
|
60
|
+
description: 'a description',
|
|
61
|
+
presentation: { description: 'a presentation description' },
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('returns the custom description, overriding the base and presentation description', () => {
|
|
66
|
+
const node = {
|
|
67
|
+
description: 'a description',
|
|
68
|
+
'x-jsf-presentation': { description: 'a presentation description' },
|
|
69
|
+
};
|
|
70
|
+
const customProperties = { description: 'a custom description' };
|
|
71
|
+
const descriptionField = getFieldDescription(node, customProperties);
|
|
72
|
+
|
|
73
|
+
expect(descriptionField).toEqual({
|
|
74
|
+
description: 'a custom description',
|
|
75
|
+
presentation: { description: 'a custom description' },
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('yupToFormErrors()', () => {
|
|
82
|
+
it('returns an object given an YupError', () => {
|
|
83
|
+
const yupOpts = { abortEarly: false };
|
|
84
|
+
const YupSchema = Yup.object({
|
|
85
|
+
age: Yup.number().min(18, 'Too young'),
|
|
86
|
+
name: Yup.object({
|
|
87
|
+
first: Yup.string().required(),
|
|
88
|
+
middle: Yup.string(),
|
|
89
|
+
last: Yup.string().required('Required field.'),
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
YupSchema.validateSync(
|
|
95
|
+
{
|
|
96
|
+
age: 10,
|
|
97
|
+
name: { first: 'Junior' },
|
|
98
|
+
},
|
|
99
|
+
yupOpts
|
|
100
|
+
);
|
|
101
|
+
} catch (yupError) {
|
|
102
|
+
expect(yupToFormErrors(yupError)).toEqual({
|
|
103
|
+
age: 'Too young',
|
|
104
|
+
name: {
|
|
105
|
+
last: 'Required field.',
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('returns nill given nill', () => {
|
|
112
|
+
expect(yupToFormErrors(undefined)).toEqual(undefined);
|
|
113
|
+
expect(yupToFormErrors(null)).toEqual(null);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('pickXKey()', () => {
|
|
118
|
+
it('returns the x-jsx attribute', () => {
|
|
119
|
+
const schema = {
|
|
120
|
+
max_length: 255,
|
|
121
|
+
'x-jsf-presentation': {
|
|
122
|
+
inputType: 'text',
|
|
123
|
+
},
|
|
124
|
+
title: 'Address',
|
|
125
|
+
type: 'string',
|
|
126
|
+
};
|
|
127
|
+
const xKey = pickXKey(schema, 'presentation');
|
|
128
|
+
|
|
129
|
+
expect(xKey).toEqual({
|
|
130
|
+
inputType: 'text',
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('returns the deprecated attribute', () => {
|
|
135
|
+
const schema = {
|
|
136
|
+
max_length: 255,
|
|
137
|
+
presentation: {
|
|
138
|
+
inputType: 'text (deprecated)',
|
|
139
|
+
},
|
|
140
|
+
title: 'Address',
|
|
141
|
+
type: 'string',
|
|
142
|
+
};
|
|
143
|
+
const xKey = pickXKey(schema, 'presentation');
|
|
144
|
+
|
|
145
|
+
expect(xKey).toEqual({
|
|
146
|
+
inputType: 'text (deprecated)',
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('return undefined given a key that is not being deprecated', () => {
|
|
151
|
+
const schema = {
|
|
152
|
+
properties: {
|
|
153
|
+
age: { type: 'number' },
|
|
154
|
+
address: { type: 'string' },
|
|
155
|
+
},
|
|
156
|
+
order: ['age', 'address'],
|
|
157
|
+
};
|
|
158
|
+
const xKey = pickXKey(schema, 'order');
|
|
159
|
+
|
|
160
|
+
// it's undefined because "x-jsf-order" does not exist,
|
|
161
|
+
// and "order" is not one of the deprecated custom keywords.
|
|
162
|
+
expect(xKey).toBeUndefined();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('returns undefined if the key is not found within an object', () => {
|
|
166
|
+
const schema = {
|
|
167
|
+
max_length: 255,
|
|
168
|
+
title: 'Address',
|
|
169
|
+
type: 'string',
|
|
170
|
+
};
|
|
171
|
+
const xKey = pickXKey(schema, 'presentation');
|
|
172
|
+
|
|
173
|
+
expect(xKey).toBeUndefined();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { convertDiskSizeFromTo } from '../utils';
|
|
2
|
+
|
|
3
|
+
describe('utils', () => {
|
|
4
|
+
it('should convert bytes to KB', () => {
|
|
5
|
+
const convert = convertDiskSizeFromTo('Bytes', 'KB');
|
|
6
|
+
expect(convert(1024)).toBe(1);
|
|
7
|
+
});
|
|
8
|
+
it('should convert bytes to MB', () => {
|
|
9
|
+
const convert = convertDiskSizeFromTo('Bytes', 'MB');
|
|
10
|
+
expect(convert(1024 * 1024)).toBe(1);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should convert KB to MB', () => {
|
|
14
|
+
const convert = convertDiskSizeFromTo('KB', 'MB');
|
|
15
|
+
expect(convert(1024)).toBe(1);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should convert KB to Bytes', () => {
|
|
19
|
+
const convert = convertDiskSizeFromTo('KB', 'Bytes');
|
|
20
|
+
expect(convert(1)).toBe(1024);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should convert MB to KB', () => {
|
|
24
|
+
const convert = convertDiskSizeFromTo('MB', 'KB');
|
|
25
|
+
expect(convert(1)).toBe(1024);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should convert MB to KB', () => {
|
|
29
|
+
const convert = convertDiskSizeFromTo('MB', 'Bytes');
|
|
30
|
+
expect(convert(1)).toBe(1048576);
|
|
31
|
+
});
|
|
32
|
+
});
|