Skip to content

Commit af5e68b

Browse files
committed
[core] Add tests for const pre-filling
1 parent a3a4d45 commit af5e68b

File tree

1 file changed

+166
-4
lines changed

1 file changed

+166
-4
lines changed

packages/form/src/core/default-state.test.ts

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,63 @@ describe("getDefaultFormState2()", () => {
5252
afterAll(() => {
5353
consoleWarnSpy.mockRestore();
5454
});
55+
it("test an object const value merge with formData", () => {
56+
const schema: Schema = {
57+
type: "object",
58+
properties: {
59+
localConst: {
60+
type: "string",
61+
const: "local",
62+
},
63+
RootConst: {
64+
type: "object",
65+
properties: {
66+
attr1: {
67+
type: "number",
68+
},
69+
attr2: {
70+
type: "boolean",
71+
},
72+
},
73+
const: {
74+
attr1: 1,
75+
attr2: true,
76+
},
77+
},
78+
RootAndLocalConst: {
79+
type: "string",
80+
const: "FromLocal",
81+
},
82+
fromFormData: {
83+
type: "string",
84+
},
85+
},
86+
const: {
87+
RootAndLocalConst: "FromRoot",
88+
},
89+
};
90+
expect(
91+
getDefaultFormState2(
92+
testValidator,
93+
defaultMerger,
94+
schema,
95+
{
96+
fromFormData: "fromFormData",
97+
},
98+
schema,
99+
false,
100+
{ emptyObjectFields: "skipDefaults" }
101+
)
102+
).toEqual({
103+
localConst: "local",
104+
RootConst: {
105+
attr1: 1,
106+
attr2: true,
107+
},
108+
RootAndLocalConst: "FromLocal",
109+
fromFormData: "fromFormData",
110+
});
111+
});
55112
it("getInnerSchemaForArrayItem() item of type boolean returns empty schema", () => {
56113
expect(
57114
getInnerSchemaForArrayItem(
@@ -89,6 +146,25 @@ describe("getDefaultFormState2()", () => {
89146
foo: 42,
90147
});
91148
});
149+
it("test computeDefaults that is passed a schema with a const property", () => {
150+
const schema: Schema = {
151+
type: "object",
152+
properties: {
153+
test: {
154+
type: "string",
155+
const: "test",
156+
},
157+
},
158+
};
159+
expect(
160+
computeDefaults3(testValidator, defaultMerger, schema, {
161+
...defaults,
162+
rootSchema: schema,
163+
})
164+
).toEqual({
165+
test: "test",
166+
});
167+
});
92168
it("test an object with an optional property that has a nested required property", () => {
93169
const schema: Schema = {
94170
type: "object",
@@ -613,6 +689,68 @@ describe("getDefaultFormState2()", () => {
613689
requiredProperty: "foo",
614690
});
615691
});
692+
it("test an object const value populate as field defaults", () => {
693+
const schema: Schema = {
694+
type: "object",
695+
properties: {
696+
localConst: {
697+
type: "string",
698+
const: "local",
699+
},
700+
RootConst: {
701+
type: "object",
702+
properties: {
703+
attr1: {
704+
type: "number",
705+
},
706+
attr2: {
707+
type: "boolean",
708+
},
709+
},
710+
const: {
711+
attr1: 1,
712+
attr2: true,
713+
},
714+
},
715+
fromFormData: {
716+
type: "string",
717+
default: "notUsed",
718+
},
719+
RootAndLocalConst: {
720+
type: "string",
721+
const: "FromLocal",
722+
},
723+
},
724+
const: {
725+
RootAndLocalConst: "FromRoot",
726+
},
727+
};
728+
expect(
729+
getObjectDefaults(
730+
testValidator,
731+
defaultMerger,
732+
schema,
733+
{
734+
...defaults,
735+
rootSchema: schema,
736+
experimental_defaultFormStateBehavior: {
737+
emptyObjectFields: "skipDefaults",
738+
},
739+
rawFormData: {
740+
fromFormData: "fromFormData",
741+
},
742+
},
743+
undefined
744+
)
745+
).toEqual({
746+
localConst: "local",
747+
RootConst: {
748+
attr1: 1,
749+
attr2: true,
750+
},
751+
RootAndLocalConst: "FromLocal",
752+
});
753+
});
616754
it("test an object with an additionalProperties", () => {
617755
const schema: Schema = {
618756
type: "object",
@@ -838,6 +976,30 @@ describe("getDefaultFormState2()", () => {
838976
)
839977
).toEqual(["Raphael", "Michaelangelo", "Unknown", "Unknown"]);
840978
});
979+
it("test an array const value populate as defaults", () => {
980+
const schema: Schema = {
981+
type: "array",
982+
minItems: 4,
983+
const: ["ConstFromRoot", "ConstFromRoot"],
984+
items: {
985+
type: "string",
986+
const: "Constant",
987+
},
988+
};
989+
expect(
990+
getArrayDefaults(
991+
testValidator,
992+
defaultMerger,
993+
schema,
994+
{
995+
...defaults,
996+
rootSchema: schema,
997+
includeUndefinedValues: "excludeObjectChildren",
998+
},
999+
["ConstFromRoot", "ConstFromRoot"]
1000+
)
1001+
).toEqual(["ConstFromRoot", "ConstFromRoot", "Constant", "Constant"]);
1002+
});
8411003
it("test an array with no defaults", () => {
8421004
const schema: Schema = {
8431005
type: "array",
@@ -859,8 +1021,8 @@ describe("getDefaultFormState2()", () => {
8591021
},
8601022
undefined
8611023
)
862-
// NOTE: Looks like in original code jest ignores the length and thinks
863-
// that [] === [undefined, undefined, undefined, undefined]
1024+
// NOTE: Looks like in original code jest ignores the length and thinks
1025+
// that [] === [undefined, undefined, undefined, undefined]
8641026
).toEqual([undefined, undefined, undefined, undefined]);
8651027
});
8661028
it("test computeDefaults handles an invalid property schema", () => {
@@ -1377,8 +1539,8 @@ describe("getDefaultFormState2()", () => {
13771539
},
13781540
undefined
13791541
)
1380-
// NOTE: Looks like in original code jest ignores the length and thinks
1381-
// that [] === [undefined, undefined, undefined, undefined]
1542+
// NOTE: Looks like in original code jest ignores the length and thinks
1543+
// that [] === [undefined, undefined, undefined, undefined]
13821544
).toEqual([undefined, undefined, undefined, undefined]);
13831545
});
13841546
it("test computeDefaults handles an invalid array schema", () => {

0 commit comments

Comments
 (0)