@@ -7,45 +7,62 @@ describe("DateTimeField", function() {
77 const moment = require ( "moment" ) ;
88 const DateTimeField = require ( "../DateTimeField.js" ) ;
99 const happyDate = moment ( "1990-06-05 07:30" ) ;
10- let parent , TestParent ;
11-
12- beforeEach ( ( ) => {
13- TestParent = React . createFactory ( React . createClass ( {
14- getInitialState ( ) {
15- return { dateTime : happyDate . format ( "x" ) } ;
16- } ,
17-
18- render ( ) {
19- return < DateTimeField { ...this . state } /> ;
20- }
21- } ) ) ;
22- parent = TestUtils . renderIntoDocument ( TestParent ( ) ) ; // eslint-disable-line
23- } ) ;
10+ let createParent , TestParent ;
11+
2412
2513 describe ( "By default" , function ( ) {
2614
2715 it ( "shows the right date for a given dateTime and inputFormat" , function ( ) {
28- const input = TestUtils . findRenderedDOMComponentWithTag ( parent , "input" ) ;
16+ const component = TestUtils . renderIntoDocument ( < DateTimeField dateTime = { happyDate . format ( "x" ) } /> ) ;
17+ const input = TestUtils . findRenderedDOMComponentWithTag ( component , "input" ) ;
2918 expect ( input . getDOMNode ( ) . value ) . toBe ( "06/05/90 7:30 AM" ) ;
3019 } ) ;
3120
3221 } ) ;
3322
3423 describe ( "When changing props" , function ( ) {
3524
36- it ( "changes the displayed date when dateTime changes" , function ( ) {
37- const input = TestUtils . findRenderedDOMComponentWithTag ( parent , "input" ) ;
38- expect ( input . getDOMNode ( ) . value ) . toBe ( "06/05/90 7:30 AM" ) ;
39- parent . setState ( { dateTime : moment ( "1981-06-04 05:45" ) . format ( "x" ) } ) ;
40- expect ( input . getDOMNode ( ) . value ) . toBe ( "06/04/81 5:45 AM" ) ;
25+ beforeEach ( ( ) => {
26+ TestParent = React . createFactory ( React . createClass ( {
27+ getInitialState ( ) {
28+ return {
29+ dateTime : happyDate . format ( "x" ) ,
30+ ...this . props
31+ } ;
32+ } ,
33+
34+ render ( ) {
35+ return < DateTimeField { ...this . state } /> ;
36+ }
37+ } ) ) ;
38+ createParent = ( initalState ) => TestUtils . renderIntoDocument ( TestParent ( initalState ) ) ; // eslint-disable-line
4139 } ) ;
4240
43- it ( "changes the displayed format when inputFormat changes" , function ( ) {
44- const input = TestUtils . findRenderedDOMComponentWithTag ( parent , "input" ) ;
45- expect ( input . getDOMNode ( ) . value ) . toBe ( "06/05/90 7:30 AM" ) ;
46- parent . setState ( { inputFormat : "x" } ) ;
47- expect ( input . getDOMNode ( ) . value ) . toBe ( happyDate . format ( "x" ) ) ;
41+ it ( "changes the displayed date when dateTime changes" , function ( ) {
42+ const Parent = createParent ( ) ;
43+ const input = TestUtils . findRenderedDOMComponentWithTag ( Parent , "input" ) ;
44+ expect ( input . getDOMNode ( ) . value ) . toBe ( "06/05/90 7:30 AM" ) ;
45+ Parent . setState ( { dateTime : moment ( "1981-06-04 05:45" ) . format ( "x" ) } ) ;
46+ expect ( input . getDOMNode ( ) . value ) . toBe ( "06/04/81 5:45 AM" ) ;
47+ } ) ;
48+
49+ it ( "changes the displayed format when inputFormat changes" , function ( ) {
50+ const Parent = createParent ( ) ;
51+ const input = TestUtils . findRenderedDOMComponentWithTag ( Parent , "input" ) ;
52+ expect ( input . getDOMNode ( ) . value ) . toBe ( "06/05/90 7:30 AM" ) ;
53+ Parent . setState ( { inputFormat : "x" } ) ;
54+ expect ( input . getDOMNode ( ) . value ) . toBe ( happyDate . format ( "x" ) ) ;
55+ } ) ;
56+
57+ it ( "doesn't change the defaultText if dateTime didn't change" , function ( ) {
58+ const Parent = createParent ( { defaultText : "Pick a date" } ) ;
59+ const input = TestUtils . findRenderedDOMComponentWithTag ( Parent , "input" ) ;
60+ expect ( input . getDOMNode ( ) . value ) . toBe ( "Pick a date" ) ;
61+ Parent . setState ( { } ) ;
62+ expect ( input . getDOMNode ( ) . value ) . toBe ( "Pick a date" ) ;
4863 } ) ;
4964
65+
5066 } ) ;
67+
5168} ) ;
0 commit comments