Simon Egersand
2018-02-07 63e0b0b2fc6f361b5ac4751eb05bc5983aea5873
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', () => ({
de3fe1 13   findDOMNode: () => {}
1ea05f 14 }));
SE 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', () => {
de3fe1 20   const tree = renderer.create(<Datetime />).toJSON();
SE 21   expect(tree).toMatchSnapshot();
1ea05f 22 });
SE 23
24 it('value: set to arbitrary value', () => {
de3fe1 25   const tree = renderer.create(<Datetime defaultValue={Date.now()} />).toJSON();
SE 26   expect(tree).toMatchSnapshot();
1ea05f 27 });
SE 28
29 it('defaultValue: set to arbitrary value', () => {
de3fe1 30   const tree = renderer.create(<Datetime defaultValue={Date.now()} />).toJSON();
SE 31   expect(tree).toMatchSnapshot();
1ea05f 32 });
SE 33
34 describe('dateFormat', () => {
de3fe1 35   it('set to true', () => {
SE 36     const tree = renderer.create(<Datetime dateFormat={true} />).toJSON();
37     expect(tree).toMatchSnapshot();
38   });
1ea05f 39
de3fe1 40   it('set to false', () => {
SE 41     const tree = renderer.create(<Datetime dateFormat={false} />).toJSON();
42     expect(tree).toMatchSnapshot();
43   });
1ea05f 44 });
SE 45
46 describe('timeFormat', () => {
de3fe1 47   it('set to true', () => {
SE 48     const tree = renderer.create(<Datetime timeFormat={true} />).toJSON();
49     expect(tree).toMatchSnapshot();
50   });
1ea05f 51
de3fe1 52   it('set to false', () => {
SE 53     const tree = renderer.create(<Datetime timeFormat={false} />).toJSON();
54     expect(tree).toMatchSnapshot();
55   });
1ea05f 56 });
SE 57
58 describe('input', () => {
de3fe1 59   it('input: set to true', () => {
SE 60     const tree = renderer.create(<Datetime input={true} />).toJSON();
61     expect(tree).toMatchSnapshot();
62   });
1ea05f 63
de3fe1 64   it('input: set to false', () => {
SE 65     const tree = renderer.create(<Datetime input={false} />).toJSON();
66     expect(tree).toMatchSnapshot();
67   });
1ea05f 68 });
SE 69
70 describe('open', () => {
de3fe1 71   it('set to true', () => {
SE 72     const tree = renderer.create(<Datetime open={true} />).toJSON();
73     expect(tree).toMatchSnapshot();
74   });
1ea05f 75
de3fe1 76   it('set to false', () => {
SE 77     const tree = renderer.create(<Datetime open={false} />).toJSON();
78     expect(tree).toMatchSnapshot();
79   });
1ea05f 80 });
SE 81
82 describe('viewMode', () => {
de3fe1 83   it('set to days', () => {
SE 84     const tree = renderer.create(<Datetime viewMode={'days'} />).toJSON();
85     expect(tree).toMatchSnapshot();
86   });
1ea05f 87
de3fe1 88   it('set to months', () => {
SE 89     const tree = renderer.create(<Datetime viewMode={'months'} />).toJSON();
90     expect(tree).toMatchSnapshot();
91   });
1ea05f 92
de3fe1 93   it('set to years', () => {
SE 94     const tree = renderer.create(<Datetime viewMode={'years'} />).toJSON();
95     expect(tree).toMatchSnapshot();
96   });
1ea05f 97
de3fe1 98   it('set to time', () => {
SE 99     const tree = renderer.create(<Datetime viewMode={'time'} />).toJSON();
100     expect(tree).toMatchSnapshot();
101   });
1ea05f 102 });
SE 103
104 it('className: set to arbitraty value', () => {
de3fe1 105   const tree = renderer
SE 106     .create(<Datetime className={'arbitrary-value'} />)
107     .toJSON();
108   expect(tree).toMatchSnapshot();
1ea05f 109 });
SE 110
111 describe('inputProps', () => {
de3fe1 112   it('with placeholder specified', () => {
SE 113     const tree = renderer
114       .create(
115         <Datetime inputProps={{ placeholder: 'arbitrary-placeholder' }} />
116       )
117       .toJSON();
118     expect(tree).toMatchSnapshot();
119   });
1ea05f 120
de3fe1 121   it('with disabled specified', () => {
SE 122     const tree = renderer
123       .create(<Datetime inputProps={{ disabled: true }} />)
124       .toJSON();
125     expect(tree).toMatchSnapshot();
126   });
1ea05f 127
de3fe1 128   it('with required specified', () => {
SE 129     const tree = renderer
130       .create(<Datetime inputProps={{ required: true }} />)
131       .toJSON();
132     expect(tree).toMatchSnapshot();
133   });
1ea05f 134
de3fe1 135   it('with name specified', () => {
SE 136     const tree = renderer
137       .create(<Datetime inputProps={{ name: 'arbitrary-name' }} />)
138       .toJSON();
139     expect(tree).toMatchSnapshot();
140   });
1ea05f 141
de3fe1 142   it('with className specified', () => {
SE 143     const tree = renderer
144       .create(<Datetime inputProps={{ className: 'arbitrary-className' }} />)
145       .toJSON();
146     expect(tree).toMatchSnapshot();
147   });
1ea05f 148 });
SE 149
150 it('isValidDate: only valid if after yesterday', () => {
de3fe1 151   const yesterday = Datetime.moment().subtract(1, 'day');
SE 152   const valid = (current) => current.isAfter(yesterday);
153   const tree = renderer.create(<Datetime isValidDate={valid} />).toJSON();
154   expect(tree).toMatchSnapshot();
1ea05f 155 });
SE 156
157 it('renderDay: specified', () => {
de3fe1 158   const renderDay = (props, currentDate) => (
SE 159     <td {...props}>{'0' + currentDate.date()}</td>
160   );
161   const tree = renderer.create(<Datetime renderDay={renderDay} />).toJSON();
162   expect(tree).toMatchSnapshot();
1ea05f 163 });
SE 164
165 it('renderMonth: specified', () => {
de3fe1 166   const renderMonth = (props, currentDate) => (
SE 167     <td {...props}>{'0' + currentDate.date()}</td>
168   );
169   const tree = renderer.create(<Datetime renderMonth={renderMonth} />).toJSON();
170   expect(tree).toMatchSnapshot();
1ea05f 171 });
SE 172
173 it('renderYear: specified', () => {
de3fe1 174   const renderYear = (props, currentDate) => (
SE 175     <td {...props}>{'0' + currentDate.date()}</td>
176   );
177   const tree = renderer.create(<Datetime renderYear={renderYear} />).toJSON();
178   expect(tree).toMatchSnapshot();
1ea05f 179 });