Skip to content

Commit b25ecf1

Browse files
author
Ruben van Leeuwen
committed
2076: getFormFieldValuesFromFieldsOrLabels test stub. Move all mock functions to same file
1 parent ba66c4b commit b25ecf1

File tree

3 files changed

+248
-143
lines changed

3 files changed

+248
-143
lines changed

frontend/packages/pydantic-forms/src/core/helper.spec.ts

Lines changed: 174 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,63 @@ import {
22
enumToOption,
33
flattenSchemaCombinators,
44
getErrorDetailsFromResponse,
5+
getFormValuesFromFieldOrLabels,
56
isNullable,
67
optionsToOption,
78
} from '@/core/helper';
89
import {
910
PydanticFormApiResponse,
11+
PydanticFormField,
1012
PydanticFormFieldFormat,
1113
PydanticFormFieldType,
1214
PydanticFormPropertySchemaParsed,
15+
PydanticFormSchema,
1316
} from '@/types';
1417

18+
export const getMockPydanticFormField = (
19+
props: Partial<PydanticFormField>,
20+
): PydanticFormField => {
21+
return {
22+
id: 'dummy',
23+
type: PydanticFormFieldType.STRING,
24+
format: PydanticFormFieldFormat.LONG,
25+
title: 'Dummy Field',
26+
default: undefined,
27+
description: undefined,
28+
arrayItem: undefined,
29+
properties: {},
30+
required: false,
31+
options: [],
32+
columns: 6,
33+
schema: {
34+
type: PydanticFormFieldType.STRING,
35+
format: PydanticFormFieldFormat.DEFAULT,
36+
},
37+
validations: {},
38+
attributes: {},
39+
...props,
40+
};
41+
};
42+
43+
const getMockPydanticFormSchema = (
44+
overrides: Partial<PydanticFormSchema> = {},
45+
): PydanticFormSchema => {
46+
return {
47+
title: 'Mock Form Schema',
48+
type: PydanticFormFieldType.OBJECT,
49+
description: 'This is a mock schema description',
50+
properties: {},
51+
...overrides,
52+
};
53+
};
54+
55+
const getMockFormPropertySchemaParsed = (
56+
overrides: Partial<PydanticFormPropertySchemaParsed> = {},
57+
): PydanticFormPropertySchemaParsed => ({
58+
format: PydanticFormFieldFormat.DEFAULT,
59+
...overrides,
60+
});
61+
1562
describe('getErrorDetailsFromResponse', () => {
1663
it('should correctly format the error details from API response', () => {
1764
const mockApiErrorResponse: PydanticFormApiResponse = {
@@ -190,13 +237,6 @@ describe('optionsToOption', () => {
190237
});
191238

192239
describe('flattenSchemaCombinators', () => {
193-
const getMockFormPropertySchemaParsed = (
194-
overrides: Partial<PydanticFormPropertySchemaParsed> = {},
195-
): PydanticFormPropertySchemaParsed => ({
196-
format: PydanticFormFieldFormat.DEFAULT,
197-
...overrides,
198-
});
199-
200240
it('Doesnt change the schema if no combinator properties exist', () => {
201241
const inputSchema = getMockFormPropertySchemaParsed();
202242
const outputSchema = flattenSchemaCombinators(inputSchema);
@@ -510,49 +550,138 @@ describe('flattenSchemaCombinators', () => {
510550

511551
consoleWarnSpy.mockRestore();
512552
});
553+
});
513554

514-
describe('isNullable', () => {
515-
it('returns true if the schema has a null type in anyOf', () => {
516-
const inputSchema = getMockFormPropertySchemaParsed({
517-
type: PydanticFormFieldType.STRING,
518-
format: PydanticFormFieldFormat.DEFAULT,
519-
anyOf: [
520-
{
521-
type: PydanticFormFieldType.STRING,
522-
},
523-
{
524-
type: PydanticFormFieldType.NULL,
525-
},
526-
],
527-
});
528-
const result: boolean = isNullable(inputSchema);
529-
expect(result).toBe(true);
555+
describe('isNullable', () => {
556+
it('returns true if the schema has a null type in anyOf', () => {
557+
const inputSchema = getMockFormPropertySchemaParsed({
558+
type: PydanticFormFieldType.STRING,
559+
format: PydanticFormFieldFormat.DEFAULT,
560+
anyOf: [
561+
{
562+
type: PydanticFormFieldType.STRING,
563+
},
564+
{
565+
type: PydanticFormFieldType.NULL,
566+
},
567+
],
530568
});
569+
const result: boolean = isNullable(inputSchema);
570+
expect(result).toBe(true);
571+
});
531572

532-
it('returns true if the schema has a null type in oneOf', () => {
533-
const inputSchema = getMockFormPropertySchemaParsed({
534-
type: PydanticFormFieldType.STRING,
535-
format: PydanticFormFieldFormat.DEFAULT,
536-
oneOf: [
537-
{
538-
type: PydanticFormFieldType.STRING,
539-
},
540-
{
541-
type: PydanticFormFieldType.NULL,
542-
},
543-
],
544-
});
545-
const result: boolean = isNullable(inputSchema);
546-
expect(result).toBe(true);
573+
it('returns true if the schema has a null type in oneOf', () => {
574+
const inputSchema = getMockFormPropertySchemaParsed({
575+
type: PydanticFormFieldType.STRING,
576+
format: PydanticFormFieldFormat.DEFAULT,
577+
oneOf: [
578+
{
579+
type: PydanticFormFieldType.STRING,
580+
},
581+
{
582+
type: PydanticFormFieldType.NULL,
583+
},
584+
],
547585
});
586+
const result: boolean = isNullable(inputSchema);
587+
expect(result).toBe(true);
588+
});
548589

549-
it('returns false if the schema does not have a null type', () => {
550-
const inputSchema = getMockFormPropertySchemaParsed({
551-
type: PydanticFormFieldType.STRING,
552-
format: PydanticFormFieldFormat.DEFAULT,
553-
});
554-
const result: boolean = isNullable(inputSchema);
555-
expect(result).toBe(false);
590+
it('returns false if the schema does not have a null type', () => {
591+
const inputSchema = getMockFormPropertySchemaParsed({
592+
type: PydanticFormFieldType.STRING,
593+
format: PydanticFormFieldFormat.DEFAULT,
594+
});
595+
const result: boolean = isNullable(inputSchema);
596+
expect(result).toBe(false);
597+
});
598+
});
599+
600+
describe('getFormValuesFromFieldOrLabels', () => {
601+
it.only('returns an empty object if no schema is provided', () => {
602+
const result = getFormValuesFromFieldOrLabels(undefined, {});
603+
expect(result).toEqual({});
604+
});
605+
606+
it.only('Returns empty object when schema has no properties', () => {
607+
const schema = getMockPydanticFormSchema();
608+
expect(getFormValuesFromFieldOrLabels(schema, {})).toEqual({});
609+
});
610+
611+
it.only('Returns fieldNames with default values', () => {
612+
const schema = getMockPydanticFormSchema({
613+
properties: {
614+
test: getMockPydanticFormField({
615+
default: 'default value',
616+
id: 'test',
617+
}),
618+
test2: getMockPydanticFormField({
619+
default: 'default value 2',
620+
id: 'test2',
621+
}),
622+
},
623+
});
624+
expect(getFormValuesFromFieldOrLabels(schema, {})).toEqual({
625+
test: 'default value',
626+
test2: 'default value 2',
556627
});
557628
});
629+
630+
it.only('Returns label instead of default value if present', () => {
631+
const schema = getMockPydanticFormSchema({
632+
properties: {
633+
test: getMockPydanticFormField({
634+
default: 'default value',
635+
id: 'test',
636+
}),
637+
test2: getMockPydanticFormField({
638+
default: 'default value 2',
639+
id: 'test2',
640+
}),
641+
},
642+
});
643+
expect(
644+
getFormValuesFromFieldOrLabels(schema, {
645+
test2: 'label for test2',
646+
}),
647+
).toEqual({
648+
test: 'default value',
649+
test2: 'label for test2',
650+
});
651+
});
652+
653+
it.only('Returns nested default values in object fields', () => {
654+
const schema = getMockPydanticFormSchema({
655+
properties: {
656+
test: getMockPydanticFormField({
657+
default: 'default value',
658+
id: 'test',
659+
}),
660+
test2: getMockPydanticFormField({
661+
default: 'default value 2',
662+
id: 'test2',
663+
properties: {
664+
nestedField: getMockPydanticFormField({
665+
default: 'nested default value',
666+
id: 'nestedField',
667+
}),
668+
},
669+
}),
670+
},
671+
});
672+
expect(getFormValuesFromFieldOrLabels(schema)).toEqual({
673+
test: 'default value',
674+
test2: {
675+
nestedField: 'nested default value',
676+
},
677+
});
678+
});
679+
680+
it('Returns nested default values in array fields', () => {
681+
expect(true).toEqual(false);
682+
});
683+
684+
it('Works with object field in array field in object field', () => {
685+
expect(true).toEqual(false);
686+
});
558687
});

0 commit comments

Comments
 (0)