marquex
2015-07-25 516b36dd03cec26a3b34997fa78da3a321347965
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
// Create the dom before requiring react
var DOM = require( './testdom');
DOM();
 
 
// Needs to be global to work in Travis CI
React = require('react');
 
var ReactAddons = require('react/addons'),
    Utils = React.addons.TestUtils,
    Datetime = require('../DateTime'),
    assert = require('assert'),
    moment = require('moment')
;
 
var createDatetime = function( props ){
    document.body.innerHTML = '';
 
    React.render(
        React.createElement( Datetime, props ),
        document.body
    );
 
    return document.body.children[0];
};
 
var date = new Date( 2000, 0, 15 ),
    mDate = moment( date ),
    strDate = mDate.format('L') + ' ' + mDate.format('LT')
;
 
describe( 'Datetime', function(){
    it( 'Create Datetime', function(){
        var component = createDatetime({});
        assert( component );
        assert.equal( component.children.length, 2 );
        assert.equal( component.children[0].tagName , 'INPUT' );
        assert.equal( component.children[1].tagName , 'DIV' );
    });
 
    it( 'input=false', function(){
        var component = createDatetime({ input: false });
        assert( component );
        assert.equal( component.children.length, 1 );
        assert.equal( component.children[0].tagName , 'DIV' );
    });
 
    it( 'Date value', function(){
        var component = createDatetime({ value: date }),
            input = component.children[0]
        ;
        assert.equal( input.value, strDate );
    });
 
    it( 'Moment value', function(){
        var component = createDatetime({ value: mDate }),
            input = component.children[0]
        ;
        assert.equal( input.value, strDate );
    });
 
    it( 'String value', function(){
        var component = createDatetime({ value: strDate }),
            input = component.children[0]
        ;
        assert.equal( input.value, strDate );
    });
 
    it( 'Date defaultValue', function(){
        var component = createDatetime({ defaultValue: date }),
            input = component.children[0]
        ;
        assert.equal( input.value, strDate );
    });
 
    it( 'Moment defaultValue', function(){
        var component = createDatetime({ defaultValue: mDate }),
            input = component.children[0]
        ;
        assert.equal( input.value, strDate );
    });
 
    it( 'String defaultValue', function(){
        var component = createDatetime({ defaultValue: strDate }),
            input = component.children[0]
        ;
        assert.equal( input.value, strDate );
    });
 
    it( 'dateFormat', function(){
        var component = createDatetime({ value: date, dateFormat: 'M&D' }),
            input = component.children[0]
        ;
        assert.equal( input.value, mDate.format('M&D LT') );
    });
 
    it( 'dateFormat=false', function(){
        var component = createDatetime({ value: date, dateFormat: false }),
            input = component.children[0],
            view = component.children[1].children[0]
        ;
        assert.equal( input.value, mDate.format('LT') );
        // The view must be the timepicker
        assert.equal( view.className, 'rdtTime' );
        // There must not be a date toggle
        assert.equal( view.querySelectorAll('thead').length, 0);
    });
 
    it( 'timeFormat', function(){
        var format = 'HH:mm:ss:SSS',
            component = createDatetime({ value: date, timeFormat: format }),
            input = component.children[0]
        ;
        assert.equal( input.value, mDate.format('L ' + format) );
    });
 
    it( 'timeFormat=false', function(){
        var component = createDatetime({ value: date, timeFormat: false }),
            input = component.children[0],
            view = component.children[1].children[0]
        ;
        assert.equal( input.value, mDate.format('L') );
        // The view must be the daypicker
        assert.equal( view.className, 'rdtDays' );
        // There must not be a time toggle
        assert.equal( view.querySelectorAll('.timeToggle').length, 0);
    });
 
    it( 'viewMode=years', function(){
        var component = createDatetime({ viewMode: 'years' }),
            view = component.children[1].children[0]
        ;
 
        assert.equal( view.className, 'rdtYears' );
    });
 
    it( 'viewMode=months', function(){
        var component = createDatetime({ viewMode: 'months' }),
            view = component.children[1].children[0]
        ;
 
        assert.equal( view.className, 'rdtMonths' );
    });
 
    it( 'viewMode=time', function(){
        var component = createDatetime({ viewMode: 'time' }),
            view = component.children[1].children[0]
        ;
 
        assert.equal( view.className, 'rdtTime' );
    });
 
    it( 'className', function(){
        var component = createDatetime({ className: 'custom' });
        assert.notEqual( component.className.indexOf('custom'), -1 );
    });
 
    it( 'inputProps', function(){
        var component = createDatetime({ inputProps: { className: 'myInput', type: 'email' } }),
            input = component.children[0]
        ;
 
        assert.equal( input.className, 'myInput' );
        assert.equal( input.type, 'email' );
    });
 
    it( 'renderDay', function(){
        var props, currentDate, selectedDate,
            component = createDatetime({ value: mDate, renderDay: function( p, current, selected ){
                props = p;
                currentDate = current;
                selectedDate = selected;
 
                return React.DOM.td( props, 'day' );
            }}),
            view = component.children[1].children[0]
        ;
 
        // Last day should be 6th of february
        assert.equal( currentDate.day(), 6 );
        assert.equal( currentDate.month(), 1 );
 
        // The date must be the same
        assert.equal( selectedDate.isSame( mDate ), true );
 
        // There should be a onClick function in the props
        assert.equal( typeof props.onClick, 'function' );
 
        // The cell text should be 'day'
        assert.equal( view.querySelector('.day').innerHTML, 'day' );
    });
 
 
    it( 'renderMonth', function(){
        var props, month, year, selectedDate,
            component = createDatetime({ value: mDate, viewMode: 'months', renderMonth: function( p, m, y, selected ){
                props = p;
                month = m;
                year = y;
                selectedDate = selected;
 
                return React.DOM.td( props, 'month' );
            }}),
            view = component.children[1].children[0]
        ;
 
        // The date must be the same
        assert.equal( selectedDate.isSame( mDate ), true );
 
        assert.equal( month, 11 );
        assert.equal( year, 2000 );
 
        // There should be a onClick function in the props
        assert.equal( typeof props.onClick, 'function' );
 
        // The cell text should be 'day'
        assert.equal( view.querySelector('.month').innerHTML, 'month' );
    });
 
    it( 'renderYear', function(){
        var props, year, selectedDate,
            component = createDatetime({ value: mDate, viewMode: 'years', renderYear: function( p, y, selected ){
                props = p;
                year = y;
                selectedDate = selected;
 
                return React.DOM.td( props, 'year' );
            }}),
            view = component.children[1].children[0]
        ;
 
        // The date must be the same
        assert.equal( selectedDate.isSame( mDate ), true );
 
        assert.equal( year, 2010 );
 
        // There should be a onClick function in the props
        assert.equal( typeof props.onClick, 'function' );
 
        // The cell text should be 'day'
        assert.equal( view.querySelector('.year').innerHTML, 'year' );
    });
});