@@ -752,15 +752,142 @@ describe('ReactDOMInput', () => {
752
752
753
753
it ( 'should not set a value for submit buttons unnecessarily' , ( ) => {
754
754
const stub = < input type = "submit" /> ;
755
- const node = ReactDOM . render ( stub , container ) ;
755
+ ReactDOM . render ( stub , container ) ;
756
+ const node = container . firstChild ;
756
757
757
758
// The value shouldn't be '', or else the button will have no text; it
758
759
// should have the default "Submit" or "Submit Query" label. Most browsers
759
760
// report this as not having a `value` attribute at all; IE reports it as
760
761
// the actual label that the user sees.
761
- expect (
762
- ! node . hasAttribute ( 'value' ) || node . getAttribute ( 'value' ) . length > 0 ,
763
- ) . toBe ( true ) ;
762
+ expect ( node . hasAttribute ( 'value' ) ) . toBe ( false ) ;
763
+ } ) ;
764
+
765
+ it ( 'should remove the value attribute on submit inputs when value is updated to undefined' , ( ) => {
766
+ const stub = < input type = "submit" value = "foo" onChange = { emptyFunction } /> ;
767
+ ReactDOM . render ( stub , container ) ;
768
+
769
+ // Not really relevant to this particular test, but changing to undefined
770
+ // should nonetheless trigger a warning
771
+ expect ( ( ) =>
772
+ ReactDOM . render (
773
+ < input type = "submit" value = { undefined } onChange = { emptyFunction } /> ,
774
+ container ,
775
+ ) ,
776
+ ) . toWarnDev (
777
+ 'A component is changing a controlled input of type ' +
778
+ 'submit to be uncontrolled.' ,
779
+ ) ;
780
+
781
+ const node = container . firstChild ;
782
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
783
+ } ) ;
784
+
785
+ it ( 'should remove the value attribute on reset inputs when value is updated to undefined' , ( ) => {
786
+ const stub = < input type = "reset" value = "foo" onChange = { emptyFunction } /> ;
787
+ ReactDOM . render ( stub , container ) ;
788
+
789
+ // Not really relevant to this particular test, but changing to undefined
790
+ // should nonetheless trigger a warning
791
+ expect ( ( ) =>
792
+ ReactDOM . render (
793
+ < input type = "reset" value = { undefined } onChange = { emptyFunction } /> ,
794
+ container ,
795
+ ) ,
796
+ ) . toWarnDev (
797
+ 'A component is changing a controlled input of type ' +
798
+ 'reset to be uncontrolled.' ,
799
+ ) ;
800
+
801
+ const node = container . firstChild ;
802
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
803
+ } ) ;
804
+
805
+ it ( 'should set a value on a submit input' , ( ) => {
806
+ let stub = < input type = "submit" value = "banana" /> ;
807
+ ReactDOM . render ( stub , container ) ;
808
+ const node = container . firstChild ;
809
+
810
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( 'banana' ) ;
811
+ } ) ;
812
+
813
+ it ( 'should not set an undefined value on a submit input' , ( ) => {
814
+ let stub = < input type = "submit" value = { undefined } /> ;
815
+ ReactDOM . render ( stub , container ) ;
816
+ const node = container . firstChild ;
817
+
818
+ // Note: it shouldn't be an empty string
819
+ // because that would erase the "submit" label.
820
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
821
+
822
+ ReactDOM . render ( stub , container ) ;
823
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
824
+ } ) ;
825
+
826
+ it ( 'should not set an undefined value on a reset input' , ( ) => {
827
+ let stub = < input type = "reset" value = { undefined } /> ;
828
+ ReactDOM . render ( stub , container ) ;
829
+ const node = container . firstChild ;
830
+
831
+ // Note: it shouldn't be an empty string
832
+ // because that would erase the "reset" label.
833
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
834
+
835
+ ReactDOM . render ( stub , container ) ;
836
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
837
+ } ) ;
838
+
839
+ it ( 'should not set a null value on a submit input' , ( ) => {
840
+ let stub = < input type = "submit" value = { null } /> ;
841
+ expect ( ( ) => {
842
+ ReactDOM . render ( stub , container ) ;
843
+ } ) . toWarnDev ( '`value` prop on `input` should not be null' ) ;
844
+ const node = container . firstChild ;
845
+
846
+ // Note: it shouldn't be an empty string
847
+ // because that would erase the "submit" label.
848
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
849
+
850
+ ReactDOM . render ( stub , container ) ;
851
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
852
+ } ) ;
853
+
854
+ it ( 'should not set a null value on a reset input' , ( ) => {
855
+ let stub = < input type = "reset" value = { null } /> ;
856
+ expect ( ( ) => {
857
+ ReactDOM . render ( stub , container ) ;
858
+ } ) . toWarnDev ( '`value` prop on `input` should not be null' ) ;
859
+ const node = container . firstChild ;
860
+
861
+ // Note: it shouldn't be an empty string
862
+ // because that would erase the "reset" label.
863
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
864
+
865
+ ReactDOM . render ( stub , container ) ;
866
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( null ) ;
867
+ } ) ;
868
+
869
+ it ( 'should set a value on a reset input' , ( ) => {
870
+ let stub = < input type = "reset" value = "banana" /> ;
871
+ ReactDOM . render ( stub , container ) ;
872
+ const node = container . firstChild ;
873
+
874
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( 'banana' ) ;
875
+ } ) ;
876
+
877
+ it ( 'should set an empty string value on a submit input' , ( ) => {
878
+ let stub = < input type = "submit" value = "" /> ;
879
+ ReactDOM . render ( stub , container ) ;
880
+ const node = container . firstChild ;
881
+
882
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( '' ) ;
883
+ } ) ;
884
+
885
+ it ( 'should set an empty string value on a reset input' , ( ) => {
886
+ let stub = < input type = "reset" value = "" /> ;
887
+ ReactDOM . render ( stub , container ) ;
888
+ const node = container . firstChild ;
889
+
890
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( '' ) ;
764
891
} ) ;
765
892
766
893
it ( 'should control radio buttons' , ( ) => {
0 commit comments