Layne Anderson
2018-07-03 49de8cf59095fa3fb450f7ec938c68af4ea9961a
commit | author | age
1ea05f 1 /* global it, describe, expect, jest */
SE 2
3 import React from 'react'; // eslint-disable-line no-unused-vars
4 import Datetime from '../DateTime.js';
5 import renderer from 'react-test-renderer';
6
7 // findDOMNode is not supported by the react-test-renderer,
8 // and even though this component is not using that method
9 // a dependency is probably using it. So we need to mock it
10 // to make the tests pass.
11 // https://github.com/facebook/react/issues/7371
12 jest.mock('react-dom', () => ({
13     findDOMNode: () => {},
14 }));
15
16 // Mock date to get rid of time as a factor to make tests deterministic
17 Date.now = jest.fn(() => 1482363367071);
18
19 it('everything default: renders correctly', () => {
20     const tree = renderer.create(
21         <Datetime />
22     ).toJSON();
23     expect(tree).toMatchSnapshot();
24 });
25
26 it('value: set to arbitrary value', () => {
27     const tree = renderer.create(
28         <Datetime defaultValue={Date.now()} />
29     ).toJSON();
30     expect(tree).toMatchSnapshot();
31 });
32
33 it('defaultValue: set to arbitrary value', () => {
34     const tree = renderer.create(
35         <Datetime defaultValue={Date.now()} />
36     ).toJSON();
37     expect(tree).toMatchSnapshot();
38 });
39
40 describe('dateFormat', () => {
41     it('set to true', () => {
42         const tree = renderer.create(
43             <Datetime dateFormat={true} />
44         ).toJSON();
45         expect(tree).toMatchSnapshot();
46     });
47
48     it('set to false', () => {
49         const tree = renderer.create(
50             <Datetime dateFormat={false} />
51         ).toJSON();
52         expect(tree).toMatchSnapshot();
53     });
54 });
55
56 describe('timeFormat', () => {
57     it('set to true', () => {
58         const tree = renderer.create(
59             <Datetime timeFormat={true} />
60         ).toJSON();
61         expect(tree).toMatchSnapshot();
62     });
63
64     it('set to false', () => {
65         const tree = renderer.create(
66             <Datetime timeFormat={false} />
67         ).toJSON();
68         expect(tree).toMatchSnapshot();
69     });
70 });
71
72 describe('input', () => {
73     it('input: set to true', () => {
74         const tree = renderer.create(
75             <Datetime input={true} />
76         ).toJSON();
77         expect(tree).toMatchSnapshot();
78     });
79
80     it('input: set to false', () => {
81         const tree = renderer.create(
82             <Datetime input={false} />
83         ).toJSON();
84         expect(tree).toMatchSnapshot();
85     });
86 });
87
88 describe('open', () => {
89     it('set to true', () => {
90         const tree = renderer.create(
91             <Datetime open={true} />
92         ).toJSON();
93         expect(tree).toMatchSnapshot();
94     });
95
96     it('set to false', () => {
97         const tree = renderer.create(
98             <Datetime open={false} />
99         ).toJSON();
100         expect(tree).toMatchSnapshot();
101     });
102 });
103
104 describe('viewMode', () => {
105     it('set to days', () => {
106         const tree = renderer.create(
107             <Datetime viewMode={'days'} />
108         ).toJSON();
109         expect(tree).toMatchSnapshot();
110     });
111
112     it('set to months', () => {
113         const tree = renderer.create(
114             <Datetime viewMode={'months'} />
115         ).toJSON();
116         expect(tree).toMatchSnapshot();
117     });
118
119     it('set to years', () => {
120         const tree = renderer.create(
121             <Datetime viewMode={'years'} />
122         ).toJSON();
123         expect(tree).toMatchSnapshot();
124     });
125
126     it('set to time', () => {
127         const tree = renderer.create(
128             <Datetime viewMode={'time'} />
129         ).toJSON();
130         expect(tree).toMatchSnapshot();
131     });
132 });
133
134 it('className: set to arbitraty value', () => {
135     const tree = renderer.create(
136         <Datetime className={'arbitrary-value'} />
137     ).toJSON();
138     expect(tree).toMatchSnapshot();
139 });
140
141 describe('inputProps', () => {
142     it('with placeholder specified', () => {
143         const tree = renderer.create(
144             <Datetime inputProps={{ placeholder: 'arbitrary-placeholder' }} />
145         ).toJSON();
146         expect(tree).toMatchSnapshot();
147     });
148
149     it('with disabled specified', () => {
150         const tree = renderer.create(
151             <Datetime inputProps={{ disabled: true }} />
152         ).toJSON();
153         expect(tree).toMatchSnapshot();
154     });
155
156     it('with required specified', () => {
157         const tree = renderer.create(
158             <Datetime inputProps={{ required: true }} />
159         ).toJSON();
160         expect(tree).toMatchSnapshot();
161     });
162
163     it('with name specified', () => {
164         const tree = renderer.create(
165             <Datetime inputProps={{ name: 'arbitrary-name' }} />
166         ).toJSON();
167         expect(tree).toMatchSnapshot();
168     });
169
170     it('with className specified', () => {
171         const tree = renderer.create(
172             <Datetime inputProps={{ className: 'arbitrary-className' }} />
173         ).toJSON();
174         expect(tree).toMatchSnapshot();
175     });
176 });
177
178 it('isValidDate: only valid if after yesterday', () => {
179     const yesterday = Datetime.moment().subtract(1, 'day');
180     const valid = (current) => current.isAfter(yesterday);
181     const tree = renderer.create(
182         <Datetime isValidDate={ valid } />
183     ).toJSON();
184     expect(tree).toMatchSnapshot();
185 });
186
187 it('renderDay: specified', () => {
188     const renderDay = (props, currentDate) => <td {...props}>{ '0' + currentDate.date() }</td>;
189     const tree = renderer.create(
190         <Datetime renderDay={renderDay} />
191     ).toJSON();
192     expect(tree).toMatchSnapshot();
193 });
194
195 it('renderMonth: specified', () => {
196     const renderMonth = (props, currentDate) => <td {...props}>{ '0' + currentDate.date() }</td>;
197     const tree = renderer.create(
198         <Datetime renderMonth={renderMonth} />
199     ).toJSON();
200     expect(tree).toMatchSnapshot();
201 });
202
203 it('renderYear: specified', () => {
204     const renderYear = (props, currentDate) => <td {...props}>{ '0' + currentDate.date() }</td>;
205     const tree = renderer.create(
206         <Datetime renderYear={renderYear} />
207     ).toJSON();
208     expect(tree).toMatchSnapshot();
209 });