Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit c5d5163

Browse files
committed
fix(project): use callbacks to propagate changes
1 parent edae305 commit c5d5163

25 files changed

+939
-521
lines changed

__test__/AvBaseInput.spec.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { AvBaseInput } from 'availity-reactstrap-validation';
22

33
describe('BaseInput', function () {
4+
let touched;
5+
let dirty;
6+
let bad;
7+
let error;
8+
49
beforeEach(() => {
10+
touched = false;
11+
dirty = false;
12+
bad = false;
13+
error = false;
514
this.inputState = 'danger';
615
this.props = {
716
name: 'fieldName',
@@ -17,17 +26,17 @@ describe('BaseInput', function () {
1726
inputs: {},
1827
getDefaultValue: sinon.stub(),
1928
getInputState: sinon.stub().returns(this.inputState),
20-
hasError: {},
21-
isDirty: {},
22-
isTouched: {},
23-
isBad: {},
29+
hasError: () => error,
30+
isDirty: () => dirty,
31+
isTouched: () => touched,
32+
isBad: () => bad,
2433
setDirty: sinon.spy(),
2534
setTouched: sinon.spy(),
2635
setBad: sinon.spy(),
2736
register: sinon.spy(),
2837
unregister: sinon.spy(),
2938
validate: sinon.spy(),
30-
validationEvent: 'formCtrlValidationEvent',
39+
getValidationEvent: () => 'formCtrlValidationEvent',
3140
parent: null,
3241
},
3342
};
@@ -361,17 +370,17 @@ describe('BaseInput', function () {
361370
});
362371

363372
it('should not call setBadInput if it has not changed', () => {
364-
this.context.FormCtrl.isBad[this.props.name] = true;
373+
bad = true;
365374
this.component.onKeyUpHandler({ target: { validity: { badInput: true } } });
366375
expect(this.context.FormCtrl.setBad).to.not.have.been.called;
367376
});
368377

369378
it('should call setBadInput if it has changed', () => {
370-
this.context.FormCtrl.isBad[this.props.name] = true;
379+
bad = true;
371380
this.component.onKeyUpHandler({ target: { validity: { badInput: false } } });
372381
expect(this.context.FormCtrl.setBad).to.have.been.calledWith(this.props.name, false);
373382

374-
this.context.FormCtrl.isBad[this.props.name] = false;
383+
bad = false;
375384
this.component.onKeyUpHandler({ target: { validity: { badInput: true } } });
376385
expect(this.context.FormCtrl.setBad).to.have.been.calledWith(this.props.name, true);
377386
});
@@ -404,13 +413,13 @@ describe('BaseInput', function () {
404413
});
405414

406415
it('should call setDirty if the control was not previously dirty', () => {
407-
this.context.FormCtrl.isDirty[this.props.name] = false;
416+
dirty = false;
408417
this.component.onInputHandler('something');
409418
expect(this.context.FormCtrl.setDirty).to.have.been.calledWith(this.props.name);
410419
});
411420

412421
it('should not call setDirty if the control was previously dirty', () => {
413-
this.context.FormCtrl.isDirty[this.props.name] = true;
422+
dirty = true;
414423
this.component.onInputHandler('something');
415424
expect(this.context.FormCtrl.setDirty).to.not.have.been.called;
416425
});
@@ -436,13 +445,13 @@ describe('BaseInput', function () {
436445
});
437446

438447
it('should call setTouched if the control was not previously touched', () => {
439-
this.context.FormCtrl.isTouched[this.props.name] = false;
448+
touched = false;
440449
this.component.onBlurHandler('something');
441450
expect(this.context.FormCtrl.setTouched).to.have.been.calledWith(this.props.name);
442451
});
443452

444453
it('should not call setTouched if the control was previously touched', () => {
445-
this.context.FormCtrl.isTouched[this.props.name] = true;
454+
touched = true;
446455
this.component.onBlurHandler('something');
447456
expect(this.context.FormCtrl.setTouched).to.not.have.been.called;
448457
});
@@ -488,13 +497,13 @@ describe('BaseInput', function () {
488497
});
489498

490499
it('should call setDirty if the control was not previously touched', () => {
491-
this.context.FormCtrl.isDirty[this.props.name] = false;
500+
dirty = false;
492501
this.component.onChangeHandler('something');
493502
expect(this.context.FormCtrl.setDirty).to.have.been.calledWith(this.props.name);
494503
});
495504

496505
it('should not call setDirty if the control was previously touched', () => {
497-
this.context.FormCtrl.isDirty[this.props.name] = true;
506+
dirty = true;
498507
this.component.onChangeHandler('something');
499508
expect(this.context.FormCtrl.setDirty).to.not.have.been.called;
500509
});
@@ -573,9 +582,9 @@ describe('BaseInput', function () {
573582
this.props.validationEvent = 'noMatch';
574583
});
575584

576-
it('should not set the state value', () => {
585+
it('should set the state value', () => {
577586
this.component.validateEvent('onChange');
578-
expect(this.setStateSpy).to.not.have.been.called;
587+
expect(this.setStateSpy).to.have.been.called;
579588
});
580589

581590
it('should not try to validate', () => {
@@ -683,7 +692,7 @@ describe('BaseInput', function () {
683692

684693
it('should return the validation event from the form control if not from props', () => {
685694
this.props.validationEvent = '';
686-
expect(this.component.getValidationEvent()).to.eql([this.context.FormCtrl.validationEvent]);
695+
expect(this.component.getValidationEvent()).to.eql([this.context.FormCtrl.getValidationEvent()]);
687696
});
688697
});
689698

__test__/AvFeedback.spec.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { shallow } from 'enzyme';
33
import { AvFeedback } from 'availity-reactstrap-validation';
44
import { FormFeedback } from 'reactstrap';
55

6-
let state = {};
6+
const state = {};
77
const options = {
88
context: {
99
FormCtrl: {},
@@ -14,15 +14,9 @@ const options = {
1414
};
1515

1616
describe('AvFeedback', () => {
17-
it('should render null when there is no error', () => {
18-
const wrapper = shallow(<AvFeedback>Yo!</AvFeedback>, options);
19-
20-
expect(wrapper.type()).to.be.null;
21-
});
22-
2317
describe('when there is an error', () => {
2418
beforeEach(() => {
25-
state.color = "danger";
19+
state.color = 'danger';
2620
});
2721

2822
it('should render with "FormFeedback"', () => {
@@ -38,7 +32,10 @@ describe('AvFeedback', () => {
3832
});
3933

4034
it('should render with the props passed in', () => {
41-
const wrapper = shallow(<AvFeedback style={{textAlign: 'center'}}>Yo!</AvFeedback>, options);
35+
const wrapper = shallow(
36+
<AvFeedback style={{ textAlign: 'center' }}>Yo!</AvFeedback>,
37+
options
38+
);
4239

4340
expect(wrapper.prop('style').textAlign).to.equal('center');
4441
});

0 commit comments

Comments
 (0)