Skip to content

Commit afac2bc

Browse files
committed
unit: more tests for form mutations
1 parent a3c3b1d commit afac2bc

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ describe('FormMutations', () => {
6565
delete contextMutations.effects;
6666
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {}, "hasError": false, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {}, "validations": {}, "xhrProgress": {}, "xhrSchema": {}}
6767
expect(contextMutations).toStrictEqual(expected);
68+
expect(onChange).toHaveBeenCalledTimes(2);
69+
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
6870
});
6971
it('updateXHRData', () => {
70-
const updateMutation = stateMachineService.send('updateFormOnXHRComplete', {
72+
const updateFormOnXHRComplete = stateMachineService.send('updateFormOnXHRComplete', {
7173
formSchema: {
7274
'new': 'info'
7375
},
@@ -77,18 +79,76 @@ describe('FormMutations', () => {
7779
formData: {},
7880
uiData: {}
7981
});
80-
const contextMutations = JSON.parse(JSON.stringify(updateMutation.context));
82+
const contextMutations = JSON.parse(JSON.stringify(updateFormOnXHRComplete.context));
8183
delete contextMutations.effects;
8284
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": {}};
8385
expect(contextMutations).toStrictEqual(expected);
86+
expect(onChange).toHaveBeenCalledTimes(3);
87+
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
8488
});
8589

8690
it('updateXHRData (edge case)', () => {
87-
const updateMutation = stateMachineService.send('updateFormOnXHRComplete', {});
88-
const contextMutations = JSON.parse(JSON.stringify(updateMutation.context));
91+
const updateFormOnXHRComplete = stateMachineService.send('updateFormOnXHRComplete', {});
92+
const contextMutations = JSON.parse(JSON.stringify(updateFormOnXHRComplete.context));
8993
delete contextMutations.effects;
9094
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": {}};
9195
expect(contextMutations).toStrictEqual(expected);
96+
expect(onChange).toHaveBeenCalledTimes(4);
97+
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
98+
});
99+
100+
it('updateErrorData', () => {
101+
const noErrors = stateMachineService.send('noErrors', {
102+
hasError: false,
103+
});
104+
const contextMutations = JSON.parse(JSON.stringify(noErrors.context));
105+
delete contextMutations.effects;
106+
const expected = {"activeStep": 0, "hasError": false, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasXHRError": false, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validations": {}, "xhrProgress": {"undefined": false}, "xhrSchema": {}};
107+
expect(contextMutations).toStrictEqual(expected);
108+
expect(onChange).toHaveBeenCalledTimes(4);
109+
expect(stateMachineService.state.value).toStrictEqual({"formUI": "dirty"});
110+
});
111+
112+
it('updateErrorXHRProgress', () => {
113+
const updateErrorXHRProgress = stateMachineService.send('errorXHRProgress', {
114+
status: true,
115+
statusCode: 500,
116+
formSchema: {
117+
'new': 'info'
118+
},
119+
formSchemaXHR: {
120+
'new': 'info'
121+
},
122+
formData: {},
123+
uiData: {}
124+
});
125+
const contextMutations = JSON.parse(JSON.stringify(updateErrorXHRProgress.context));
126+
delete contextMutations.effects;
127+
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": true, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {"xhr": []}, "validations": {}, "xhrProgress": {"undefined": true}, "xhrSchema": {}};
128+
expect(contextMutations).toStrictEqual(expected);
129+
expect(onChange).toHaveBeenCalledTimes(5);
130+
expect(stateMachineService.state.value).toStrictEqual({"formUI": "invalid"});
131+
});
132+
133+
it('updateErrorXHRProgress (offline)', () => {
134+
const updateErrorXHRProgress = stateMachineService.send('errorXHRProgress', {
135+
status: true,
136+
statusCode: 999,
137+
formSchema: {
138+
'new': 'info'
139+
},
140+
formSchemaXHR: {
141+
'new': 'info'
142+
},
143+
formData: {},
144+
uiData: {}
145+
});
146+
const contextMutations = JSON.parse(JSON.stringify(updateErrorXHRProgress.context));
147+
delete contextMutations.effects;
148+
const expected = {"activeStep": 0, "formData": {"test": "test-2"}, "formSchema": {}, "formSchemaXHR": {"new": "info"}, "hasError": false, "hasXHRError": true, "lastField": "test", "parsedFormSchema": {}, "uiData": {}, "uiSchema": {}, "validation": {"xhr": []}, "validations": {}, "xhrProgress": {"undefined": true}, "xhrSchema": {}};
149+
expect(contextMutations).toStrictEqual(expected);
150+
expect(onChange).toHaveBeenCalledTimes(6);
151+
expect(stateMachineService.state.value).toStrictEqual({"formUI": "invalid"});
92152
});
93153
});
94154
});

0 commit comments

Comments
 (0)