Skip to content

Commit

Permalink
chore: add error message to setValue callback
Browse files Browse the repository at this point in the history
  • Loading branch information
marstamm committed Jul 5, 2023
1 parent 586ee54 commit 425c1f6
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ___Note:__ Yet to be released changes appear here._

__Breaking Changes__

* `setValue` is now also called when `validate` returns an error. Ensure that your `setValue` callback can handle invalid values.
* `setValue` is now also called when `validate` returns an error. The error message is provided as a second argument to the `setValue` callback.

## 2.2.1

Expand Down
2 changes: 1 addition & 1 deletion src/components/entries/FEEL/Feel.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export default function FeelEntry(props) {

// don't create multiple commandStack entries for the same value
if (newValue !== value) {
setValue(newValue);
setValue(newValue, newValidationError);
}

setValidationError(newValidationError);
Expand Down
2 changes: 1 addition & 1 deletion src/components/entries/NumberField.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default function NumberFieldEntry(props) {
newValidationError = validate(newValue) || null;
}

setValue(newValue);
setValue(newValue, newValidationError);
setLocalError(newValidationError);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/entries/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default function SelectEntry(props) {
newValidationError = validate(newValue) || null;
}

setValue(newValue);
setValue(newValue, newValidationError);
setLocalError(newValidationError);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/entries/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default function TextAreaEntry(props) {
newValidationError = validate(newValue) || null;
}

setValue(newValue);
setValue(newValue, newValidationError);

setLocalError(newValidationError);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/entries/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default function TextfieldEntry(props) {
newValidationError = validate(newValue) || null;
}

setValue(newValue);
setValue(newValue, newValidationError);

setLocalError(newValidationError);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/entries/templating/Templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function TemplatingEntry(props) {

// don't create multiple commandStack entries for the same value
if (newValue !== value) {
setValue(newValue);
setValue(newValue, newValidationError);
}

setValidationError(newValidationError);
Expand Down
93 changes: 93 additions & 0 deletions test/spec/components/Feel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,29 @@ describe('<FeelField>', function() {
expect(error.innerText).to.eql('error');
});


it('should pass error to `setValue`', function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === 'bar') {
return 'error';
}
};

const result = createFeelField({ container, validate, setValue: setValueSpy });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('.bio-properties-panel-input', entry);

// when
changeInput(input, 'bar');

// then
expect(setValueSpy).to.have.been.calledWith('bar', 'error');
});

});


Expand Down Expand Up @@ -756,6 +779,28 @@ describe('<FeelField>', function() {
expect(error.innerText).to.eql('error');
});


it('should pass error to `setValue`', function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === 3) {
return 'error';
}
};

const result = createFeelNumber({ container, validate, setValue: setValueSpy });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('.bio-properties-panel-input', entry);

// when
changeInput(input, 3);

// then
expect(setValueSpy).to.have.been.calledWith(3, 'error');
});
});


Expand Down Expand Up @@ -1194,6 +1239,29 @@ describe('<FeelField>', function() {
expect(error.innerText).to.eql('error');
});


it('should pass error to `setValue`', function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === 'bar') {
return 'error';
}
};

const result = createFeelTextArea({ container, validate, setValue: setValueSpy });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('.bio-properties-panel-input', entry);

// when
changeInput(input, 'bar');

// then
expect(setValueSpy).to.have.been.calledWith('bar', 'error');
});

});


Expand Down Expand Up @@ -2046,6 +2114,31 @@ describe('<FeelField>', function() {
});
});


it('should pass error to `setValue`', async function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === '=bar') {
return 'error';
}
};

const result = createFeelTextArea({ container, validate, setValue: setValueSpy, feel: 'required' });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('[role="textbox"]', entry);

// when
await act(() => {
input.textContent = 'bar';
});

// then
expect(setValueSpy).to.have.been.calledWith('=bar', 'error');
});

});


Expand Down
28 changes: 3 additions & 25 deletions test/spec/components/NumberField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('<NumberField>', function() {
});


it('should call setValue if validation succeeds', function() {
it('should pass error to `setValue`', function() {

// given
const validate = (v) => {
Expand All @@ -312,32 +312,10 @@ describe('<NumberField>', function() {
const entry = domQuery('.bio-properties-panel-entry .bio-properties-panel-input', result.container);

// when
changeInput(entry, 2);
changeInput(entry, 1);

// then
expect(setValueSpy).to.have.been.calledWith(2);
});


it('should call setValue even if validation fails', function() {

// given
const validate = (v) => {
if (v % 2 !== 0) {
return 'should be even';
}
};

const setValueSpy = sinon.spy();

const result = createNumberField({ container, validate, setValue: setValueSpy });
const entry = domQuery('.bio-properties-panel-entry .bio-properties-panel-input', result.container);

// when
changeInput(entry, 3);

// then
expect(setValueSpy).to.have.been.called;
expect(setValueSpy).to.have.been.calledWith(1, 'should be even');
});

});
Expand Down
23 changes: 23 additions & 0 deletions test/spec/components/Select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,29 @@ describe('<Select>', function() {
expect(error.innerText).to.eql('error');
});


it('should pass error to `setValue`', function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === 'A') {
return 'error';
}
};
const getOptions = () => createOptions();

const result = createSelect({ container, validate, getOptions, setValue: setValueSpy });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('.bio-properties-panel-input', entry);

// when
changeInput(input, 'A');

// then
expect(setValueSpy).to.have.been.calledWith('A', 'error');
});
});


Expand Down
25 changes: 25 additions & 0 deletions test/spec/components/Templating.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,31 @@ describe('<Templating>', function() {
});
});


it('should set invalid', async function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === '{{bar}}') {
return 'error';
}
};

const result = createTemplatingEntry({ container, validate, setValue: setValueSpy });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('[role="textbox"]', entry);

// when
await act(() => input.textContent = '{{bar}}');

// then
return await waitFor(() => {
expect(setValueSpy).to.have.been.calledWith('{{bar}}', 'error');
});
});

});


Expand Down
22 changes: 22 additions & 0 deletions test/spec/components/TextArea.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,28 @@ describe('<TextArea>', function() {
expect(error.innerText).to.eql('error');
});

it('should set invalid', function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === 'bar') {
return 'error';
}
};

const result = createTextArea({ container, validate, setValue: setValueSpy });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('.bio-properties-panel-input', entry);

// when
changeInput(input, 'bar');

// then
expect(setValueSpy).to.have.been.calledWith('bar', 'error');
});

});


Expand Down
23 changes: 23 additions & 0 deletions test/spec/components/TextField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ describe('<TextField>', function() {
expect(error.innerText).to.eql('error');
});


it('should set invalid', function() {

// given
const setValueSpy = sinon.spy();
const validate = (v) => {
if (v === 'bar') {
return 'error';
}
};

const result = createTextField({ container, validate, setValue: setValueSpy });

const entry = domQuery('.bio-properties-panel-entry', result.container);
const input = domQuery('.bio-properties-panel-input', entry);

// when
changeInput(input, 'bar');

// then
expect(setValueSpy).to.have.been.calledWith('bar', 'error');
});

});


Expand Down

0 comments on commit 425c1f6

Please sign in to comment.