Skip to content

Commit 0fdaef6

Browse files
committed
Create tests for omitExtraData:
- getFieldNames tests are taken from Form.test.jsx and modified - getUsedFormData tests are taken from Form.test.jsx and modified, originally they contained schemas that weren't necessary - some extra tests for omitExtraData are created
1 parent 4053a70 commit 0fdaef6

File tree

3 files changed

+285
-0
lines changed

3 files changed

+285
-0
lines changed

packages/utils/test/schema.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
sanitizeDataForNewSchemaTest,
1313
toIdSchemaTest,
1414
toPathSchemaTest,
15+
omitExtraDataTest,
1516
} from './schema';
1617

1718
const testValidator = getTestValidator({});
@@ -29,3 +30,4 @@ retrieveSchemaTest(testValidator);
2930
sanitizeDataForNewSchemaTest(testValidator);
3031
toIdSchemaTest(testValidator);
3132
toPathSchemaTest(testValidator);
33+
omitExtraDataTest(testValidator);

packages/utils/test/schema/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import retrieveSchemaTest from './retrieveSchemaTest';
1010
import sanitizeDataForNewSchemaTest from './sanitizeDataForNewSchemaTest';
1111
import toIdSchemaTest from './toIdSchemaTest';
1212
import toPathSchemaTest from './toPathSchemaTest';
13+
import omitExtraDataTest from './omitExtraDataTest';
1314

1415
export * from './types';
1516

@@ -26,4 +27,5 @@ export {
2627
sanitizeDataForNewSchemaTest,
2728
toIdSchemaTest,
2829
toPathSchemaTest,
30+
omitExtraDataTest,
2931
};
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
import { TestValidatorType } from './types';
2+
import { getFieldNames } from '../../src/schema/omitExtraData';
3+
import { omitExtraData, PathSchema, RJSFSchema } from '../../lib';
4+
import { getUsedFormData } from '../../lib/schema/omitExtraData';
5+
6+
export default function omitExtraDataTest(testValidator: TestValidatorType) {
7+
describe('omitExtraData()', () => {
8+
describe('getFieldNames()', () => {
9+
it('should return an empty array for a single input form', () => {
10+
const formData = 'foo';
11+
const pathSchema = {
12+
$name: '',
13+
};
14+
15+
expect(getFieldNames(pathSchema as PathSchema<any>, formData)).toEqual([]);
16+
});
17+
18+
it('should get field names from pathSchema', () => {
19+
const formData = {
20+
extra: {
21+
foo: 'bar',
22+
},
23+
level1: {
24+
level2: 'test',
25+
anotherThing: {
26+
anotherThingNested: 'abc',
27+
extra: 'asdf',
28+
anotherThingNested2: 0,
29+
},
30+
stringArray: ['scobochka'],
31+
},
32+
level1a: 1.23,
33+
};
34+
35+
const pathSchema = {
36+
$name: '',
37+
level1: {
38+
$name: 'level1',
39+
level2: { $name: 'level1.level2' },
40+
anotherThing: {
41+
$name: 'level1.anotherThing',
42+
anotherThingNested: {
43+
$name: 'level1.anotherThing.anotherThingNested',
44+
},
45+
anotherThingNested2: {
46+
$name: 'level1.anotherThing.anotherThingNested2',
47+
},
48+
},
49+
stringArray: {
50+
$name: 'level1.stringArray',
51+
},
52+
},
53+
level1a: {
54+
$name: 'level1a',
55+
},
56+
};
57+
58+
const fieldNames = getFieldNames(pathSchema as unknown as PathSchema<any>, formData);
59+
expect(fieldNames.sort()).toEqual(
60+
[
61+
['level1', 'anotherThing', 'anotherThingNested'],
62+
['level1', 'anotherThing', 'anotherThingNested2'],
63+
['level1', 'level2'],
64+
['level1', 'stringArray'],
65+
['level1a'],
66+
].sort()
67+
);
68+
});
69+
70+
it('should get field marked as additionalProperties', () => {
71+
const formData = {
72+
extra: {
73+
foo: 'bar',
74+
},
75+
level1: {
76+
level2: 'test',
77+
extra: 'foo',
78+
mixedMap: {
79+
namedField: 'foo',
80+
key1: 'val1',
81+
},
82+
},
83+
level1a: 1.23,
84+
};
85+
86+
const pathSchema = {
87+
$name: '',
88+
level1: {
89+
$name: 'level1',
90+
level2: { $name: 'level1.level2' },
91+
mixedMap: {
92+
$name: 'level1.mixedMap',
93+
__rjsf_additionalProperties: true,
94+
namedField: { $name: 'level1.mixedMap.namedField' }, // this name should not be returned, as the root object paths should be returned for objects marked with additionalProperties
95+
},
96+
},
97+
level1a: {
98+
$name: 'level1a',
99+
},
100+
};
101+
102+
const fieldNames = getFieldNames(pathSchema as unknown as PathSchema, formData);
103+
expect(fieldNames.sort()).toEqual([['level1', 'level2'], 'level1.mixedMap', ['level1a']].sort());
104+
});
105+
106+
it('should get field names from pathSchema with array', () => {
107+
const formData = {
108+
address_list: [
109+
{
110+
street_address: '21, Jump Street',
111+
city: 'Babel',
112+
state: 'Neverland',
113+
},
114+
{
115+
street_address: '1234 Schema Rd.',
116+
city: 'New York',
117+
state: 'Arizona',
118+
},
119+
],
120+
};
121+
122+
const pathSchema = {
123+
$name: '',
124+
address_list: {
125+
0: {
126+
$name: 'address_list.0',
127+
city: {
128+
$name: 'address_list.0.city',
129+
},
130+
state: {
131+
$name: 'address_list.0.state',
132+
},
133+
street_address: {
134+
$name: 'address_list.0.street_address',
135+
},
136+
},
137+
1: {
138+
$name: 'address_list.1',
139+
city: {
140+
$name: 'address_list.1.city',
141+
},
142+
state: {
143+
$name: 'address_list.1.state',
144+
},
145+
street_address: {
146+
$name: 'address_list.1.street_address',
147+
},
148+
},
149+
},
150+
};
151+
152+
const fieldNames = getFieldNames(pathSchema as unknown as PathSchema, formData);
153+
expect(fieldNames.sort()).toEqual(
154+
[
155+
['address_list', '0', 'city'],
156+
['address_list', '0', 'state'],
157+
['address_list', '0', 'street_address'],
158+
['address_list', '1', 'city'],
159+
['address_list', '1', 'state'],
160+
['address_list', '1', 'street_address'],
161+
].sort()
162+
);
163+
});
164+
});
165+
166+
describe('getUsedFormData()', () => {
167+
it('should just return the single input form value', () => {
168+
const formData = 'foo';
169+
170+
expect(getUsedFormData(formData, [])).toEqual('foo');
171+
});
172+
173+
it('should return the root level array', () => {
174+
const formData: [] = [];
175+
176+
expect(getUsedFormData(formData, [])).toEqual([]);
177+
});
178+
179+
it('should call getUsedFormData with data from fields in event', () => {
180+
const formData = {
181+
foo: 'bar',
182+
};
183+
184+
expect(getUsedFormData(formData, [['foo']])).toEqual({ foo: 'bar' });
185+
});
186+
187+
it('unused form values should be omitted', () => {
188+
const formData = {
189+
foo: 'bar',
190+
baz: 'buzz',
191+
list: [
192+
{ title: 'title0', details: 'details0' },
193+
{ title: 'title1', details: 'details1' },
194+
],
195+
};
196+
197+
expect(getUsedFormData(formData, [['foo'], ['list', '0', 'title'], ['list', '1', 'details']])).toEqual({
198+
foo: 'bar',
199+
list: [{ title: 'title0' }, { details: 'details1' }],
200+
});
201+
});
202+
});
203+
204+
it('should omit fields not defined in the schema', () => {
205+
const schema: RJSFSchema = {
206+
type: 'object',
207+
properties: {
208+
foo: { type: 'string' },
209+
},
210+
};
211+
const formData = {
212+
foo: 'bar',
213+
extraField: 'should be omitted',
214+
};
215+
216+
expect(omitExtraData(testValidator, schema, schema, formData)).toEqual({ foo: 'bar' });
217+
});
218+
219+
it('should include nested object fields defined in the schema', () => {
220+
const schema: RJSFSchema = {
221+
type: 'object',
222+
properties: {
223+
nested: {
224+
type: 'object',
225+
properties: {
226+
foo: { type: 'string' },
227+
},
228+
},
229+
},
230+
};
231+
const formData = {
232+
nested: {
233+
foo: 'bar',
234+
extraField: 'should be omitted',
235+
},
236+
};
237+
238+
expect(omitExtraData(testValidator, schema, schema, formData)).toEqual({ nested: { foo: 'bar' } });
239+
});
240+
241+
it('should handle arrays according to the schema', () => {
242+
const schema: RJSFSchema = {
243+
type: 'object',
244+
properties: {
245+
list: {
246+
type: 'array',
247+
items: {
248+
type: 'object',
249+
properties: {
250+
foo: { type: 'string' },
251+
},
252+
},
253+
},
254+
},
255+
};
256+
const formData = {
257+
list: [{ foo: 'bar', extraField: 'should be omitted' }, { foo: 'baz' }],
258+
};
259+
260+
expect(omitExtraData(testValidator, schema, schema, formData)).toEqual({
261+
list: [{ foo: 'bar' }, { foo: 'baz' }],
262+
});
263+
});
264+
265+
it('should not omit additional properties if the schema allows them', () => {
266+
const schema: RJSFSchema = {
267+
type: 'object',
268+
properties: {
269+
foo: { type: 'string' },
270+
},
271+
additionalProperties: true,
272+
};
273+
const formData = {
274+
foo: 'bar',
275+
extraField: 'should not be omitted',
276+
};
277+
278+
expect(omitExtraData(testValidator, schema, schema, formData)).toEqual(formData);
279+
});
280+
});
281+
}

0 commit comments

Comments
 (0)