Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/validate/__tests__/jest/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ describe("Validate > Integration > API > addGroup", () => {
});
});

it("should find errors in an added validation group", async () => {
document.body.innerHTML = `<form class="form">
<label id="group1-1-label" for="group1-1">group1</label>
<span id="group1-error-message" class=" error-message" data-valmsg-for="group1">You must enter a value</span>
<input
id="group1-1"
name="group1"
value=""
type="text" />
</form>`;
const input = document.querySelector("#group1-1");
const validator = validate("form")[0];

expect(validator.getState().groups).toEqual({});
input.setAttribute("required", "required");
validator.addGroup([input]);
console.log(validator.getState().errors);
expect(validator.getState().errors).toEqual({
group1: "You must enter a value",
});
});

it("should return leave state unchanged if it cannot add the validation group", async () => {
document.body.innerHTML = `<form class="form">
<label id="group1-1-label" for="group1-1">group1</label>
Expand Down
45 changes: 42 additions & 3 deletions packages/validate/__tests__/jest/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ describe('Validate > Unit > Reducers > Add group', () => {
validators: [],
valid: false
}
}
},
errors: {}
};
const newGroup = {
group3: {
Expand All @@ -359,7 +360,7 @@ describe('Validate > Unit > Reducers > Add group', () => {
valid: false
}
};
const output = Reducers[ACTIONS.ADD_GROUP](state, newGroup);
const output = Reducers[ACTIONS.ADD_GROUP](state, newGroup, {});
expect(output).toEqual({
groups: {
group1: {
Expand All @@ -378,7 +379,8 @@ describe('Validate > Unit > Reducers > Add group', () => {
validators: [],
valid: false
}
}
},
errors: {}
});
});
});
Expand Down Expand Up @@ -413,4 +415,41 @@ describe('Validate > Unit > Reducers > Remove group', () => {
}
});
});

it('should remove errors associated with a validation group', async () => {
expect.assertions(1);
const state = {
groups: {
group1: {
fields: [document.createElement('input')],
validators: [],
errorMessages: ['This field is required'],
valid: false
},
group2: {
fields: [document.createElement('input')],
validators: [],
valid: false
}
},
errors: {
group1: 'This field is required',
group2: 'This field is required'
}
};
const output = Reducers[ACTIONS.REMOVE_GROUP](state, 'group2');
expect(output).toEqual({
groups: {
group1: {
fields: [document.createElement('input')],
validators: [],
errorMessages: ['This field is required'],
valid: false
}
},
errors: {
group1: 'This field is required'
}
});
});
});
3 changes: 2 additions & 1 deletion packages/validate/src/lib/factory/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
reduceErrorMessages } from '../validator';
import { initRealTimeValidation } from '../validator/real-time-validation';
import { renderError, clearError, addAXAttributes } from '../dom';
import { findErrors } from '../validator/utils';
import { ACTIONS } from '../constants';
import reducers from '../reducers';

Expand All @@ -20,7 +21,7 @@ export const addGroup = store => nodes => {
const groups = removeUnvalidatableGroups(nodes.reduce(assembleValidationGroup, {}));
if (Object.keys(groups).length === 0) return console.warn('Group cannot be added.');

store.update(reducers[ACTIONS.ADD_GROUP](store.getState(), groups), [ addAXAttributes, () => {
store.update(reducers[ACTIONS.ADD_GROUP](store.getState(), groups, findErrors(groups)), [ addAXAttributes, () => {
if (store.getState().realTimeValidation) {
//if we're already in realtime validation then we need to re-start it with the newly added group
initRealTimeValidation(store);
Expand Down
13 changes: 10 additions & 3 deletions packages/validate/src/lib/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ export default {
groups: Object.assign({}, state.groups, nextGroup)
});
},
[ACTIONS.ADD_GROUP]: (state, data) => Object.assign({}, state, {
groups: Object.assign({}, state.groups, data)
[ACTIONS.ADD_GROUP]: (state, groups, errors) => Object.assign({}, state, {
groups: Object.assign({}, state.groups, groups),
errors: Object.assign({}, state.errors, errors)
}),
[ACTIONS.REMOVE_GROUP]: (state, groupName) => Object.assign({}, state, {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to remove errors in removeGroup to clean up state at some point if we're using it for anything else, but not if you don't need it today!

groups: Object.keys(state.groups).reduce((acc, group) => {
if (group !== groupName) acc[group] = state.groups[group];
return acc;
}, {})
}, {}),
...(state.errors !== undefined ? {
errors: Object.keys(state.errors).reduce((acc, error) => {
if (error !== groupName) acc[error] = state.errors[error];
return acc;
}, {})
} : {})
}),
[ACTIONS.ADD_VALIDATION_METHOD]: (state, data) => {
const nextGroup = Object.assign({},
Expand Down