Skip to content

Commit 879ad22

Browse files
committed
unit: more tests for form mutations
1 parent 5c18b81 commit 879ad22

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

src/helpers/state-machine/form/mutations/__tests__/form-state-mutations.spec.ts

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,28 @@ describe('FormMutations', () => {
3030
const onChange = jest.fn();
3131
const onError = jest.fn();
3232
const params = {
33-
uiSchema: {},
33+
uiSchema: {
34+
"ui:page": {
35+
"ui:layout": "tabs",
36+
"props": {
37+
"ui:schemaErrors": true
38+
},
39+
"style": {
40+
"boxShadow": "none"
41+
},
42+
"tabs": {
43+
"style": {
44+
"width": "29vw",
45+
"marginTop": 10
46+
}
47+
},
48+
"tab": {
49+
"style": {
50+
"minWidth": 81
51+
}
52+
}
53+
}
54+
},
3455
xhrSchema: {
3556
"ui:errors": {
3657
"offline": {
@@ -70,11 +91,12 @@ describe('FormMutations', () => {
7091
});
7192
const contextMutations = JSON.parse(JSON.stringify(updateMutation.context));
7293
delete contextMutations.effects;
73-
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {}, "validations": {}, "xhrProgress": {}, "xhrSchema": params.xhrSchema}
94+
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": params.uiSchema, "validation": {}, "validations": {}, "xhrProgress": {}, "xhrSchema": params.xhrSchema}
7495
expect(contextMutations).toStrictEqual(expected);
7596
expect(onChange).toHaveBeenCalledTimes(2);
7697
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
7798
});
99+
78100
it('updateXHRData', () => {
79101
const updateFormOnXHRComplete = stateMachineService.send('updateFormOnXHRComplete', {
80102
formSchema: {
@@ -88,7 +110,7 @@ describe('FormMutations', () => {
88110
});
89111
const contextMutations = JSON.parse(JSON.stringify(updateFormOnXHRComplete.context));
90112
delete contextMutations.effects;
91-
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": params.xhrSchema};
113+
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": params.uiSchema, "validation": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": params.xhrSchema};
92114
expect(contextMutations).toStrictEqual(expected);
93115
expect(onChange).toHaveBeenCalledTimes(3);
94116
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
@@ -98,9 +120,21 @@ describe('FormMutations', () => {
98120
const updateFormOnXHRComplete = stateMachineService.send('updateFormOnXHRComplete', {});
99121
const contextMutations = JSON.parse(JSON.stringify(updateFormOnXHRComplete.context));
100122
delete contextMutations.effects;
101-
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": params.xhrSchema};
123+
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": params.uiSchema, "validation": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": params.xhrSchema};
124+
expect(contextMutations).toStrictEqual(expected);
125+
expect(onChange).toHaveBeenCalled();
126+
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
127+
});
128+
129+
it('updateTabIndex', () => {
130+
const updateMutation = stateMachineService.send('updateTabIndex', {
131+
tabIndex: 1,
132+
});
133+
const contextMutations = JSON.parse(JSON.stringify(updateMutation.context));
134+
delete contextMutations.effects;
135+
const expected = {"activeStep": 1, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {"ui:page": {"props": {"ui:schemaErrors": true}, "style": {"boxShadow": "none"}, "tab": {"style": {"minWidth": 81}}, "tabs": {"props": {"tabIndex": 1}, "style": {"marginTop": 10, "width": "29vw"}}, "ui:layout": "tabs"}}, "validation": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": {"ui:errors": {"offline": {"message": "Please try again once you are online.", "title": "You are Offline !"}}}}
102136
expect(contextMutations).toStrictEqual(expected);
103-
expect(onChange).toHaveBeenCalledTimes(4);
137+
expect(onChange).toHaveBeenCalled();
104138
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
105139
});
106140

@@ -113,9 +147,9 @@ describe('FormMutations', () => {
113147
});
114148
const contextMutations = JSON.parse(JSON.stringify(updateMutation.context));
115149
delete contextMutations.effects;
116-
const expected = {"activeStep": 0, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": params.xhrSchema};
150+
const expected = {"activeStep": 1, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {"ui:page": {"props": {"ui:schemaErrors": true}, "style": {"boxShadow": "none"}, "tab": {"style": {"minWidth": 81}}, "tabs": {"props": {"tabIndex": 1}, "style": {"marginTop": 10, "width": "29vw"}}, "ui:layout": "tabs"}}, "validation": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": {"ui:errors": {"offline": {"message": "Please try again once you are online.", "title": "You are Offline !"}}}};
117151
expect(contextMutations).toStrictEqual(expected);
118-
expect(onChange).toHaveBeenCalledTimes(5);
152+
expect(onChange).toHaveBeenCalled();
119153
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
120154
expect(updateArrayFN).toHaveBeenCalledTimes(1);
121155
});
@@ -126,9 +160,9 @@ describe('FormMutations', () => {
126160
});
127161
const contextMutations = JSON.parse(JSON.stringify(noErrors.context));
128162
delete contextMutations.effects;
129-
const expected = {"activeStep": 0, "hasError": false, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": params.xhrSchema};
163+
const expected = {"activeStep": 1, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {"ui:page": {"props": {"ui:schemaErrors": true}, "style": {"boxShadow": "none"}, "tab": {"style": {"minWidth": 81}}, "tabs": {"props": {"tabIndex": 1}, "style": {"marginTop": 10, "width": "29vw"}}, "ui:layout": "tabs"}}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": {"ui:errors": {"offline": {"message": "Please try again once you are online.", "title": "You are Offline !"}}}};
130164
expect(contextMutations).toStrictEqual(expected);
131-
expect(onChange).toHaveBeenCalledTimes(5);
165+
expect(onChange).toHaveBeenCalled();
132166
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
133167
});
134168

@@ -147,9 +181,9 @@ describe('FormMutations', () => {
147181
});
148182
const contextMutations = JSON.parse(JSON.stringify(updateErrorXHRProgress.context));
149183
delete contextMutations.effects;
150-
const expected = {"activeStep": 0, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": true, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {"xhr": []}, "validations": {}, "xhrProgress": {"undefined": true}, "xhrSchema": params.xhrSchema};
184+
const expected = {"activeStep": 1, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": true, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {"ui:page": {"props": {"ui:schemaErrors": true}, "style": {"boxShadow": "none"}, "tab": {"style": {"minWidth": 81}}, "tabs": {"props": {"tabIndex": 1}, "style": {"marginTop": 10, "width": "29vw"}}, "ui:layout": "tabs"}}, "validation": {"xhr": []}, "validations": {}, "xhrProgress": {"undefined": true}, "xhrSchema": {"ui:errors": {"offline": {"message": "Please try again once you are online.", "title": "You are Offline !"}}}};
151185
expect(contextMutations).toStrictEqual(expected);
152-
expect(onChange).toHaveBeenCalledTimes(6);
186+
expect(onChange).toHaveBeenCalled();
153187
expect(stateMachineService.state.value).toStrictEqual({"formUI": "invalid"});
154188
});
155189

@@ -168,9 +202,9 @@ describe('FormMutations', () => {
168202
});
169203
const contextMutations = JSON.parse(JSON.stringify(updateErrorXHRProgress.context));
170204
delete contextMutations.effects;
171-
const expected = {"activeStep": 0, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": true, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {"xhr": [{"message": "Please try again once you are online.", "rule": "offline", "title": "You are Offline !"}]}, "validations": {}, "xhrProgress": {"undefined": true}, "xhrSchema": {"ui:errors": {"offline": {"message": "Please try again once you are online.", "title": "You are Offline !"}}}};
205+
const expected = {"activeStep": 1, "formData": {}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": true, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {"ui:page": {"props": {"ui:schemaErrors": true}, "style": {"boxShadow": "none"}, "tab": {"style": {"minWidth": 81}}, "tabs": {"props": {"tabIndex": 1}, "style": {"marginTop": 10, "width": "29vw"}}, "ui:layout": "tabs"}}, "validation": {"xhr": [{"message": "Please try again once you are online.", "rule": "offline", "title": "You are Offline !"}]}, "validations": {}, "xhrProgress": {"undefined": true}, "xhrSchema": {"ui:errors": {"offline": {"message": "Please try again once you are online.", "title": "You are Offline !"}}}};
172206
expect(contextMutations).toStrictEqual(expected);
173-
expect(onChange).toHaveBeenCalledTimes(7);
207+
expect(onChange).toHaveBeenCalled();
174208
expect(stateMachineService.state.value).toStrictEqual({"formUI": "invalid"});
175209
});
176210
});

0 commit comments

Comments
 (0)