This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathAvBaseInput.spec.js
More file actions
849 lines (739 loc) · 31.9 KB
/
AvBaseInput.spec.js
File metadata and controls
849 lines (739 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
import { AvBaseInput } from 'availity-reactstrap-validation';
describe('BaseInput', function () {
let touched;
let dirty;
let bad;
let error;
let disabled;
let readOnly;
beforeEach(() => {
touched = false;
dirty = false;
bad = false;
error = false;
disabled = undefined;
readOnly = undefined;
this.inputState = 'danger';
this.props = {
name: 'fieldName',
// default props
validateEvent: '',
validate: {},
trueValue: true,
falseValue: false,
multiple: false,
};
this.context = {
FormCtrl: {
inputs: {},
getDefaultValue: sinon.stub(),
getInputState: sinon.stub().returns(this.inputState),
hasError: () => error,
isDirty: () => dirty,
isTouched: () => touched,
isBad: () => bad,
isDisabled: () => disabled,
isReadOnly: () => readOnly,
setDirty: sinon.spy(),
setTouched: sinon.spy(),
setBad: sinon.spy(),
register: sinon.spy(),
unregister: sinon.spy(),
validate: sinon.spy(),
getValidationEvent: () => 'formCtrlValidationEvent',
parent: null,
},
};
this.component = new AvBaseInput(this.props);
this.component.context = this.context;
// eslint-disable-next-line no-multi-assign
this.setStateSpy = this.component.setState = sinon.spy();
});
describe('class constructor', () => {
it('should set the initial value to an empty string', () => {
expect(this.component.value).to.equal('');
});
it('should set the initial state value to an empty string', () => {
expect(this.component.state.value).to.equal('');
});
it('should set the initial validations to an empty object', () => {
expect(this.component.validations).to.deep.equal({});
});
it('should set the initial state value to an empty array if multiple is specified', () => {
this.props.multiple = true;
this.component.constructor(this.props);
expect(this.component.state.value).to.be.empty;
});
});
describe('component will mount', () => {
it('should get the default value', () => {
const spy = sinon.spy(this.component, 'getDefaultValue');
this.component.UNSAFE_componentWillMount();
expect(spy).to.have.been.calledOnce;
});
it('should set the value to the default value', () => {
const defaultValue = 'some value';
this.component.props.defaultValue = defaultValue;
this.component.UNSAFE_componentWillMount();
expect(this.component.value).to.equal(defaultValue);
});
it('should set the state value to the default value', () => {
const defaultValue = 'some value';
this.component.props.defaultValue = defaultValue;
this.component.UNSAFE_componentWillMount();
expect(this.setStateSpy).to.have.been.calledWithMatch({ value: defaultValue });
});
it('should set the value to the value prop if provided', () => {
const defaultValue = 'some value';
this.component.props.value = defaultValue;
this.component.UNSAFE_componentWillMount();
expect(this.component.value).to.equal(defaultValue);
});
it('should set the state value to the value prop if provided', () => {
const defaultValue = 'some value';
this.component.props.value = defaultValue;
this.component.UNSAFE_componentWillMount();
expect(this.setStateSpy).to.have.been.calledWithMatch({ value: defaultValue });
});
it('should not trigger validation', () => {
const spy = sinon.spy(this.component, 'updateValidations');
this.component.UNSAFE_componentWillMount();
expect(spy).to.have.been.calledOnce;
});
});
describe('component will receive props', () => {
it('should do nothing if the value has not changed', () => {
this.props.value = 123;
const spy = sinon.spy(this.component, 'validate');
this.component.UNSAFE_componentWillReceiveProps(this.props);
expect(this.setStateSpy).to.not.have.been.called;
expect(spy).to.not.have.been.called;
});
describe('when the value changed', () => {
it('should set the value to the new value', () => {
const newValue = 2342;
this.component.UNSAFE_componentWillReceiveProps({ value: newValue });
expect(this.component.value).to.equal(newValue);
});
it('should set the state value to the default value', () => {
const newValue = 2342;
this.component.UNSAFE_componentWillReceiveProps({ value: newValue });
expect(this.setStateSpy).to.have.been.calledWithMatch({ value: newValue });
});
it('should trigger validation', () => {
const newValue = 2342;
const spy = sinon.spy(this.component, 'validate');
this.component._isMounted = true;
this.component.UNSAFE_componentWillReceiveProps({ value: newValue });
expect(spy).to.have.been.calledOnce;
});
it('should reset the value if multiple has changed from false to true', () => {
this.props.multiple = false;
this.component.UNSAFE_componentWillReceiveProps({ multiple: true });
expect(this.component.value).to.be.empty;
expect(this.component.state.value).to.be.empty;
});
it('should reset the value if multiple has changed from true to false', () => {
this.props.multiple = true;
this.component.UNSAFE_componentWillReceiveProps({ multiple: false });
expect(this.component.value).to.equal('');
});
});
describe('when it is a checkbox', () => {
describe('when the checked prop changes', () => {
it('should set the value to the trueValue when the next props checked prop is true', () => {
this.props.checked = false;
this.component.UNSAFE_componentWillReceiveProps({
type: 'checkbox',
checked: true,
trueValue: true,
falseValue: false,
});
expect(this.component.value).to.equal(this.props.trueValue);
});
it('should set the value to the falseValue when the next props checked prop is false', () => {
this.props.checked = true;
this.component.UNSAFE_componentWillReceiveProps({
type: 'checkbox',
checked: false,
trueValue: true,
falseValue: false,
});
expect(this.component.value).to.equal(this.props.falseValue);
});
it('should set the state to the new value', () => {
this.props.checked = false;
this.component.UNSAFE_componentWillReceiveProps({
type: 'checkbox',
checked: true,
trueValue: true,
falseValue: false,
});
expect(this.setStateSpy).to.have.been.calledWithMatch({ value: this.props.trueValue });
});
it('should not set the state', () => {
this.props.checked = true;
this.component.UNSAFE_componentWillReceiveProps({
type: 'checkbox',
checked: true,
trueValue: true,
falseValue: false,
});
expect(this.setStateSpy).to.not.have.been.called;
});
});
});
});
describe('update validations', () => {
it('should register the component', () => {
this.component.updateValidations();
expect(this.context.FormCtrl.register).to.have.been.calledWith(this.component);
});
it('should trigger validation', () => {
const spy = sinon.spy(this.component, 'validate');
this.component._isMounted = true;
this.component.updateValidations();
expect(spy).to.have.been.calledOnce;
});
it('should replace the current validations', () => {
const originalValidations = this.component.validations;
this.component.updateValidations();
expect(this.component.validations).to.not.equal(originalValidations);
});
describe('HTML validation types', () => {
it('should add validators to the list which when the type can trigger a validation', () => {
this.component.props.type = 'date';
this.component.updateValidations();
expect(this.component.validations).to.have.property('date', true);
});
it('should not add validators to the list which when the type can trigger a validation', () => {
this.component.props.type = 'text';
this.component.updateValidations();
expect(this.component.validations).to.not.have.property('text');
});
});
describe('HTML validation attributes', () => {
describe('boolean', () => {
it('should add validators to the list which when the attribute can trigger a validation', () => {
this.component.props.required = true;
this.component.updateValidations();
expect(this.component.validations).to.have.deep.property('required.value', true);
});
it('should not add validators to the list which when the attribute can trigger a validation', () => {
this.component.props.disabled = true;
this.component.updateValidations();
expect(this.component.validations).to.not.have.property('disabled');
});
});
describe('with value', () => {
it('should add validators to the list which when the attribute can trigger a validation', () => {
this.component.props.min = 6;
this.component.props.max = 12;
this.component.updateValidations();
expect(this.component.validations).to.have.deep.property('min.value', 6);
expect(this.component.validations).to.have.deep.property('max.value', 12);
});
it('should not add validators to the list when the attribute can trigger a validation', () => {
this.component.props.value = 'something';
this.component.updateValidations();
expect(this.component.validations).to.not.have.property('value');
});
it('should remove validators from the list when the prop is falsey', () => {
this.component.props.required = false;
this.component.updateValidations();
expect(this.component.validations).to.not.have.property('required');
});
});
});
});
describe('component will ummount', () => {
it('should unregister the component', () => {
this.component.componentWillUnmount();
expect(this.context.FormCtrl.unregister).to.have.been.calledOnce;
});
});
describe('get default value', () => {
describe('for non checkbox', () => {
it('should return the prop value based on the key', () => {
this.props.defaultValue = 'my default';
const result = this.component.getDefaultValue();
expect(this.context.FormCtrl.getDefaultValue).to.not.have.been.called;
expect(result).to.equal(this.props.defaultValue);
});
it('should return the default value from the model when the prop is not present', () => {
const defaultValue = 'something';
this.context.FormCtrl.getDefaultValue.returns(defaultValue);
const result = this.component.getDefaultValue();
expect(this.context.FormCtrl.getDefaultValue).to.have.been.calledOnce;
expect(result).to.equal(defaultValue);
});
it('should return and empty string when the default value from the model and the prop are not present', () => {
this.context.FormCtrl.getDefaultValue.returns(undefined);
const result = this.component.getDefaultValue();
expect(result).to.equal('');
});
});
describe('for select', () => {
it('should return empty array', () => {
this.props.type = 'select';
this.props.multiple = true;
const result = this.component.getDefaultValue();
expect(result).to.be.empty;
});
});
describe('for a checkbox', () => {
it('should return the trueValue prop when defaultChecked is true', () => {
this.props.type = 'checkbox';
this.props.defaultChecked = true;
const result = this.component.getDefaultValue();
expect(this.context.FormCtrl.getDefaultValue).to.not.have.been.called;
expect(result).to.equal(this.props.trueValue);
});
it('should return the falseValue prop when defaultChecked is false', () => {
this.props.type = 'checkbox';
this.props.defaultChecked = false;
const result = this.component.getDefaultValue();
expect(this.context.FormCtrl.getDefaultValue).to.not.have.been.called;
expect(result).to.equal(this.props.falseValue);
});
it('should return the default value from the model when the defaultChecked is not present', () => {
this.props.type = 'checkbox';
const defaultValue = 'something';
this.props.trueValue = defaultValue;
this.context.FormCtrl.getDefaultValue.returns(defaultValue);
const result = this.component.getDefaultValue();
expect(this.context.FormCtrl.getDefaultValue).to.have.been.calledOnce;
expect(result).to.equal(defaultValue);
});
it('should return the falseValue when the model and the defaultChecked are not present', () => {
this.props.type = 'checkbox';
this.context.FormCtrl.getDefaultValue.returns(undefined);
const result = this.component.getDefaultValue();
expect(result).to.equal(this.props.falseValue);
});
it('should return the falseValue when there is a value that is not the trueValue', () => {
this.props.type = 'checkbox';
this.context.FormCtrl.getDefaultValue.returns('something else');
const result = this.component.getDefaultValue();
expect(result).to.equal(this.props.falseValue);
});
});
});
describe('on key up handler', () => {
it('should not throw when event is undefined', () => {
expect(this.component.onKeyUpHandler).to.not.throw();
});
it('should not throw when event.target is undefined', () => {
expect(this.component.onKeyUpHandler.bind(this.component, {})).to.not.throw();
expect(this.context.FormCtrl.setBad).to.not.have.been.called;
});
it('should not throw when event.target.validity is undefined', () => {
expect(this.component.onKeyUpHandler.bind(this.component, { target: {} })).to.not.throw();
expect(this.context.FormCtrl.setBad).to.not.have.been.called;
});
it('should not set badInput to true event.target.validity.badInput is undefined', () => {
expect(this.component.onKeyUpHandler.bind(this.component, { target: { validity: {} } })).to.not.throw();
expect(this.context.FormCtrl.setBad).to.not.have.been.called;
});
it('should not call setBadInput if it has not changed', () => {
bad = true;
this.component.onKeyUpHandler({ target: { validity: { badInput: true } } });
expect(this.context.FormCtrl.setBad).to.not.have.been.called;
});
it('should call setBadInput if it has changed', () => {
bad = true;
this.component.onKeyUpHandler({ target: { validity: {} } });
expect(this.context.FormCtrl.setBad).to.have.been.calledWith(this.props.name, false);
bad = true;
this.component.onKeyUpHandler({ target: { validity: { badInput: false } } });
expect(this.context.FormCtrl.setBad).to.have.been.calledWith(this.props.name, false);
bad = false;
this.component.onKeyUpHandler({ target: { validity: { badInput: true } } });
expect(this.context.FormCtrl.setBad).to.have.been.calledWith(this.props.name, true);
});
it('should trigger the callback prop with the event if present', () => {
this.props.onKeyUp = sinon.spy();
const event = {};
this.component.onKeyUpHandler(event);
expect(this.props.onKeyUp).to.have.been.calledWith(event);
});
});
describe('on input handler', () => {
it('should set the internal value to the passed value', () => {
const value = { some: 'value' };
this.component.onInputHandler(value);
expect(this.component.value).to.equal(value);
});
it('should set the internal value to the passed event value', () => {
const event = { target: { value: 'value' } };
this.component.onInputHandler(event);
expect(this.component.value).to.equal(event.target.value);
});
it('should call valitateEvent with the onInput event', () => {
const spy = sinon.spy(this.component, 'validateEvent');
this.component.onInputHandler('something');
expect(spy).to.have.been.calledWith('onInput');
});
it('should call setDirty if the control was not previously dirty', () => {
dirty = false;
this.component.onInputHandler('something');
expect(this.context.FormCtrl.setDirty).to.have.been.calledWith(this.props.name);
});
it('should not call setDirty if the control was previously dirty', () => {
dirty = true;
this.component.onInputHandler('something');
expect(this.context.FormCtrl.setDirty).to.not.have.been.called;
});
});
describe('on blur handler', () => {
it('should set the internal value to the passed value', () => {
const value = { some: 'value' };
this.component.onBlurHandler(value);
expect(this.component.value).to.equal(value);
});
it('should set the internal value to the passed event value', () => {
const event = { target: { value: 'value' } };
this.component.onBlurHandler(event);
expect(this.component.value).to.equal(event.target.value);
});
it('should call valitateEvent with the onInput event', () => {
const spy = sinon.spy(this.component, 'validateEvent');
this.component.onBlurHandler('something');
expect(spy).to.have.been.calledWith('onBlur');
});
it('should call setTouched if the control was not previously touched', () => {
touched = false;
this.component.onBlurHandler('something');
expect(this.context.FormCtrl.setTouched).to.have.been.calledWith(this.props.name);
});
it('should not call setTouched if the control was previously touched', () => {
touched = true;
this.component.onBlurHandler('something');
expect(this.context.FormCtrl.setTouched).to.not.have.been.called;
});
});
describe('on focus handler', () => {
it('should set the internal value to the passed value', () => {
const value = { some: 'value' };
this.component.onFocusHandler(value);
expect(this.component.value).to.equal(value);
});
it('should set the internal value to the passed event value', () => {
const event = { target: { value: 'value' } };
this.component.onFocusHandler(event);
expect(this.component.value).to.equal(event.target.value);
});
it('should call valitateEvent with the onInput event', () => {
const spy = sinon.spy(this.component, 'validateEvent');
this.component.onFocusHandler('something');
expect(spy).to.have.been.calledWith('onFocus');
});
});
describe('on change handler', () => {
it('should set the internal value to the passed value', () => {
const value = { some: 'value' };
this.component.onChangeHandler(value);
expect(this.component.value).to.equal(value);
});
it('should set the internal value to the passed event value', () => {
const event = { target: { value: 'value' } };
this.component.onChangeHandler(event);
expect(this.component.value).to.equal(event.target.value);
});
it('should call valitateEvent with the onChange event', () => {
const spy = sinon.spy(this.component, 'validateEvent');
this.component.onChangeHandler('something');
expect(spy).to.have.been.calledWith('onChange');
});
it('should call setDirty if the control was not previously touched', () => {
dirty = false;
this.component.onChangeHandler('something');
expect(this.context.FormCtrl.setDirty).to.have.been.calledWith(this.props.name);
});
it('should not call setDirty if the control was previously touched', () => {
dirty = true;
this.component.onChangeHandler('something');
expect(this.context.FormCtrl.setDirty).to.not.have.been.called;
});
});
describe('get field value', () => {
it('should give `true` for a checkbox when it is checked and not trueValue is defined', () => {
this.props.type = 'checkbox';
const event = { target: { checked: true } };
const result = this.component.getFieldValue(event);
expect(result).to.be.true;
});
it('should give `false` for a checkbox when it is not checked and not falseValue is defined', () => {
this.props.type = 'checkbox';
const event = { target: { checked: false } };
const result = this.component.getFieldValue(event);
expect(result).to.be.false;
});
it('should give the value of the "trueValue" props for a checkbox when it is checked', () => {
this.props.type = 'checkbox';
this.props.trueValue = {};
const event = { target: { checked: true } };
const result = this.component.getFieldValue(event);
expect(result).to.equal(this.props.trueValue);
});
it('should give the value of the "falseValue" props for a checkbox when it is not checked', () => {
this.props.type = 'checkbox';
this.props.falseValue = {};
const event = { target: { checked: false } };
const result = this.component.getFieldValue(event);
expect(result).to.equal(this.props.falseValue);
});
it('should give the value of "value" for non checkboxs which have it defined', () => {
this.props.type = 'text';
const event = { target: { value: {} } };
const result = this.component.getFieldValue(event);
expect(result).to.equal(event.target.value);
});
it('should give the event for non checkboxs which do not have a target', () => {
this.props.type = 'text';
const event = { noTarget: { value: {} } };
const result = this.component.getFieldValue(event);
expect(result).to.equal(event);
});
it('should give the event for non checkboxs which do not have a value defined', () => {
this.props.type = 'text';
const event = { target: { noValue: {} } };
const result = this.component.getFieldValue(event);
expect(result).to.equal(event);
});
it('should give the selected options', () => {
this.props.type = 'select';
this.props.multiple = true;
const event = {
target: {
options: [
{ value: 'selected', selected: true },
{ value: 'notSelected', selected: false },
],
},
};
const result = this.component.getFieldValue(event);
expect(result).to.deep.equal(['selected']);
});
});
describe('validate event', () => {
it('should use getValidationEvent to get the validation event', () => {
const spy = sinon.spy(this.component, 'getValidationEvent');
this.component.validateEvent('onChange');
expect(spy).to.have.been.calledOnce;
});
describe('when the event name does not match', () => {
beforeEach(() => {
this.props.validationEvent = 'noMatch';
});
it('should set the state value', () => {
this.component.validateEvent('onChange');
expect(this.setStateSpy).to.have.been.called;
});
it('should not try to validate', () => {
const spy = sinon.spy(this.component, 'validate');
this.component.validateEvent('onChange');
expect(spy).to.not.have.been.called;
});
it('should trigger the callback from props with the original event and the value', () => {
this.props.onChange = sinon.spy();
const value = {};
const originalEvent = {};
this.component.value = value;
this.component.validateEvent('onChange', originalEvent);
expect(this.props.onChange).to.have.been.calledWith(originalEvent, value);
});
});
describe('when the event name does match', () => {
beforeEach(() => {
this.props.validationEvent = 'onChange';
});
it('should set the state value', () => {
const value = {};
this.component.value = value;
this.component.validateEvent('onChange');
expect(this.setStateSpy).to.have.been.calledWithMatch({ value });
});
it('should try to validate', () => {
const spy = sinon.spy(this.component, 'validate');
this.component.validateEvent('onChange');
expect(spy).to.have.been.calledOnce;
});
it('should trigger the callback from props with the value', () => {
this.props.onChange = sinon.spy();
const value = {};
const originalEvent = {};
this.component.value = value;
this.component.validateEvent('onChange', originalEvent);
expect(this.props.onChange).to.have.been.calledWith(originalEvent, value);
});
});
});
describe('validate', () => {
it('should call validate on the form control passing the name of the field', () => {
this.component.validate();
expect(this.context.FormCtrl.validate).to.have.been.calledWith(this.props.name);
});
});
describe('reset', () => {
it('should set the internal value to the default value', () => {
const defaultValue = 'some value';
this.component.props.defaultValue = defaultValue;
this.component.reset();
expect(this.component.value).to.equal(defaultValue);
});
it('should remove dirty', () => {
this.component.reset();
expect(this.context.FormCtrl.setDirty).to.have.been.calledWith(this.props.name, false);
});
it('should remove toucehd', () => {
this.component.reset();
expect(this.context.FormCtrl.setTouched).to.have.been.calledWith(this.props.name, false);
});
it('should remove bad input', () => {
this.component.reset();
expect(this.context.FormCtrl.setBad).to.have.been.calledWith(this.props.name, false);
});
it('should set the state value to the default value', () => {
const defaultValue = 'some value';
this.component.props.defaultValue = defaultValue;
this.component.reset();
expect(this.setStateSpy).to.have.been.calledWithMatch({ value: defaultValue });
});
it('should trigger validation', () => {
const spy = sinon.spy(this.component, 'validate');
this.component.reset();
expect(spy).to.have.been.calledOnce;
});
it('should trigger the callback from props with the [default] value', () => {
const defaultValue = 'some value';
this.component.props.defaultValue = defaultValue;
this.props.onReset = sinon.spy();
this.component.reset();
expect(this.props.onReset).to.have.been.calledWith(defaultValue);
});
});
describe('get validation event', () => {
it('should return the validation event pass via props if present', () => {
this.props.validationEvent = 'myEvent';
expect(this.component.getValidationEvent()).to.eql([this.props.validationEvent]);
});
it('should return the validation event from the form control if not from props', () => {
this.props.validationEvent = '';
expect(this.component.getValidationEvent()).to.eql([this.context.FormCtrl.getValidationEvent()]);
});
});
describe('get validator props', () => {
it('should return an object', () => {
expect(this.component.getValidatorProps()).to.be.an('object');
});
it('should turn the validations in the validate prop into attrs if they are legit attrs', () => {
this.props.validate = {
min: { value: 6 },
required: true,
match: { value: 'something' },
};
const result = this.component.getValidatorProps();
expect(result).to.include({ min: 6, required: true }).and.not.include.keys('match');
});
describe('returned object', () => {
it('should have the default event handlers', () => {
const result = this.component.getValidatorProps();
expect(result).to.include.all.keys('onKeyUp', 'onBlur', 'onInput', 'onChange', 'onFocus');
});
it('should have the current value', () => {
const value = 'something';
this.component.value = value;
const result = this.component.getValidatorProps();
expect(result).to.include({ value });
});
it('should have disabled if AvForm disabled is set', () => {
const undefinedResult = this.component.getValidatorProps();
expect(undefinedResult).to.not.have.key('disabled');
disabled = true;
const trueResult = this.component.getValidatorProps();
expect(trueResult).to.include({ disabled });
disabled = false;
const falseResult = this.component.getValidatorProps();
expect(falseResult).to.include({ disabled });
});
it('should not have disabled if input disabled is set (false)', () => {
this.props.disabled = false;
const undefinedResult = this.component.getValidatorProps();
expect(undefinedResult).to.not.have.key('disabled');
disabled = true;
const trueResult = this.component.getValidatorProps();
expect(trueResult).to.not.have.key('disabled');
disabled = false;
const falseResult = this.component.getValidatorProps();
expect(falseResult).to.not.have.key('disabled');
});
it('should not have disabled if input disabled is set (true)', () => {
this.props.disabled = true;
const undefinedResult = this.component.getValidatorProps();
expect(undefinedResult).to.not.have.key('disabled');
disabled = true;
const trueResult = this.component.getValidatorProps();
expect(trueResult).to.not.have.key('disabled');
disabled = false;
const falseResult = this.component.getValidatorProps();
expect(falseResult).to.not.have.key('disabled');
});
it('should have readOnly if AvForm readOnly is set', () => {
const undefinedResult = this.component.getValidatorProps();
expect(undefinedResult).to.not.have.key('readOnly');
readOnly = true;
const trueResult = this.component.getValidatorProps();
expect(trueResult).to.include({ readOnly });
readOnly = false;
const falseResult = this.component.getValidatorProps();
expect(falseResult).to.include({ readOnly });
});
it('should not have readOnly if input readOnly is set (false)', () => {
this.props.readOnly = false;
const undefinedResult = this.component.getValidatorProps();
expect(undefinedResult).to.not.have.key('readOnly');
readOnly = true;
const trueResult = this.component.getValidatorProps();
expect(trueResult).to.not.have.key('readOnly');
readOnly = false;
const falseResult = this.component.getValidatorProps();
expect(falseResult).to.not.have.key('readOnly');
});
it('should not have readOnly if input readOnly is set (true)', () => {
this.props.readOnly = true;
const undefinedResult = this.component.getValidatorProps();
expect(undefinedResult).to.not.have.key('readOnly');
readOnly = true;
const trueResult = this.component.getValidatorProps();
expect(trueResult).to.not.have.key('readOnly');
readOnly = false;
const falseResult = this.component.getValidatorProps();
expect(falseResult).to.not.have.key('readOnly');
});
describe('when a checkbox', () => {
it('should set the checked attribute to true when the value matches the trueValue', () => {
this.props.type = 'checkbox';
this.props.trueValue = 'yes';
this.component.value = 'yes';
const result = this.component.getValidatorProps();
expect(result.checked).to.be.true;
});
it('should set the checked attribute to false when the value does not match the trueValue', () => {
this.props.type = 'checkbox';
this.props.trueValue = 'yes';
this.component.value = true;
const result = this.component.getValidatorProps();
expect(result.checked).to.be.false;
});
});
});
});
describe('get value', () => {
it('should return the current internal value', () => {
const value = {};
this.component.value = value;
expect(this.component.getValue()).to.equal(value);
});
});
});