mayoiga 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -54,7 +54,8 @@ function createFormStore(initialState, schema) {
54
54
  },
55
55
  pushFormErrors: (validator) => (prev) => {
56
56
  const pushedErrors = validator(prev.value);
57
- return { ...prev, errors: { ...prev.errors, ...pushedErrors } };
57
+ const allErrors = { ...prev.errors, ...pushedErrors };
58
+ return { ...prev, isValid: Object.keys(allErrors).length === 0, errors: allErrors };
58
59
  },
59
60
  reduceFormErrors: (reducer) => (prev) => {
60
61
  return { ...prev, errors: reducer(prev.errors) };
@@ -89,3 +89,25 @@ test('User is able to re-initialize the form state', async () => {
89
89
  TestFormHook.actions.initializeForm({}, { cleanup: true });
90
90
  expect(TestFormHook.api.getState().isDirty).toBeFalsy();
91
91
  });
92
+ test('User is able to push server side validation', async () => {
93
+ const TestFormHook = createTestHook();
94
+ const Top = () => {
95
+ return (React.createElement("div", null,
96
+ React.createElement(AgeInputComponent, { controller: TestFormHook.controller })));
97
+ };
98
+ render(React.createElement(Top, null));
99
+ const ageInput = screen.getByTestId('age-input');
100
+ await userEvent.type(ageInput, '9999');
101
+ TestFormHook.actions.pushFormErrors((_s) => ({ age: 'server side error' }));
102
+ const state1 = TestFormHook.api.getState();
103
+ expect(state1.isDirty).toBeTruthy();
104
+ expect(state1.isValid).toBeFalsy();
105
+ expect(state1.errors.age).toEqual('server side error');
106
+ await userEvent.type(ageInput, 'hgoehoge');
107
+ // confirm empty field won't overwrite any messages
108
+ TestFormHook.actions.pushFormErrors((_s) => ({}));
109
+ const state2 = TestFormHook.api.getState();
110
+ expect(state2.isDirty).toBeTruthy();
111
+ expect(state2.isValid).toBeFalsy();
112
+ expect(state2.errors.age).toEqual('Invalid input');
113
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mayoiga",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Type safe hook based form library for React",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -142,3 +142,35 @@ test('User is able to re-initialize the form state', async () => {
142
142
  TestFormHook.actions.initializeForm({}, {cleanup: true});
143
143
  expect(TestFormHook.api.getState().isDirty).toBeFalsy();
144
144
  });
145
+
146
+ test('User is able to push server side validation', async () => {
147
+ const TestFormHook = createTestHook();
148
+
149
+ const Top = () => {
150
+ return (
151
+ <div>
152
+ <AgeInputComponent controller={TestFormHook.controller} />
153
+ </div>
154
+ );
155
+ };
156
+
157
+ render(<Top />);
158
+
159
+ const ageInput: HTMLInputElement = screen.getByTestId('age-input');
160
+ await userEvent.type(ageInput, '9999');
161
+
162
+ TestFormHook.actions.pushFormErrors((_s) => ({age: 'server side error'}));
163
+ const state1 = TestFormHook.api.getState();
164
+ expect(state1.isDirty).toBeTruthy();
165
+ expect(state1.isValid).toBeFalsy();
166
+ expect(state1.errors.age).toEqual('server side error');
167
+
168
+ await userEvent.type(ageInput, 'hgoehoge');
169
+
170
+ // confirm empty field won't overwrite any messages
171
+ TestFormHook.actions.pushFormErrors((_s) => ({}));
172
+ const state2 = TestFormHook.api.getState();
173
+ expect(state2.isDirty).toBeTruthy();
174
+ expect(state2.isValid).toBeFalsy();
175
+ expect(state2.errors.age).toEqual('Invalid input');
176
+ });
package/src/index.ts CHANGED
@@ -178,7 +178,8 @@ function createFormStore<StateBeforeValidation extends StateRestriction, Schema
178
178
  },
179
179
  pushFormErrors: (validator) => (prev) => {
180
180
  const pushedErrors = validator(prev.value);
181
- return {...prev, errors: {...prev.errors, ...pushedErrors}};
181
+ const allErrors = {...prev.errors, ...pushedErrors};
182
+ return {...prev, isValid: Object.keys(allErrors).length === 0, errors: allErrors};
182
183
  },
183
184
  reduceFormErrors: (reducer) => (prev) => {
184
185
  return {...prev, errors: reducer(prev.errors)};