Simon Egersand
2018-02-07 de3fe15c59e7692eda6058a0dc8d0271acbcf3a3
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
/* global it, describe, expect, jest */
 
import React from 'react'; // eslint-disable-line no-unused-vars
import Datetime from '../DateTime.js';
import renderer from 'react-test-renderer';
 
// findDOMNode is not supported by the react-test-renderer,
// and even though this component is not using that method
// a dependency is probably using it. So we need to mock it
// to make the tests pass.
// https://github.com/facebook/react/issues/7371
jest.mock('react-dom', () => ({
  findDOMNode: () => {}
}));
 
// Mock date to get rid of time as a factor to make tests deterministic
Date.now = jest.fn(() => 1482363367071);
 
it('everything default: renders correctly', () => {
  const tree = renderer.create(<Datetime />).toJSON();
  expect(tree).toMatchSnapshot();
});
 
it('value: set to arbitrary value', () => {
  const tree = renderer.create(<Datetime defaultValue={Date.now()} />).toJSON();
  expect(tree).toMatchSnapshot();
});
 
it('defaultValue: set to arbitrary value', () => {
  const tree = renderer.create(<Datetime defaultValue={Date.now()} />).toJSON();
  expect(tree).toMatchSnapshot();
});
 
describe('dateFormat', () => {
  it('set to true', () => {
    const tree = renderer.create(<Datetime dateFormat={true} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('set to false', () => {
    const tree = renderer.create(<Datetime dateFormat={false} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
 
describe('timeFormat', () => {
  it('set to true', () => {
    const tree = renderer.create(<Datetime timeFormat={true} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('set to false', () => {
    const tree = renderer.create(<Datetime timeFormat={false} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
 
describe('input', () => {
  it('input: set to true', () => {
    const tree = renderer.create(<Datetime input={true} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('input: set to false', () => {
    const tree = renderer.create(<Datetime input={false} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
 
describe('open', () => {
  it('set to true', () => {
    const tree = renderer.create(<Datetime open={true} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('set to false', () => {
    const tree = renderer.create(<Datetime open={false} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
 
describe('viewMode', () => {
  it('set to days', () => {
    const tree = renderer.create(<Datetime viewMode={'days'} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('set to months', () => {
    const tree = renderer.create(<Datetime viewMode={'months'} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('set to years', () => {
    const tree = renderer.create(<Datetime viewMode={'years'} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('set to time', () => {
    const tree = renderer.create(<Datetime viewMode={'time'} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
 
it('className: set to arbitraty value', () => {
  const tree = renderer
    .create(<Datetime className={'arbitrary-value'} />)
    .toJSON();
  expect(tree).toMatchSnapshot();
});
 
describe('inputProps', () => {
  it('with placeholder specified', () => {
    const tree = renderer
      .create(
        <Datetime inputProps={{ placeholder: 'arbitrary-placeholder' }} />
      )
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('with disabled specified', () => {
    const tree = renderer
      .create(<Datetime inputProps={{ disabled: true }} />)
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('with required specified', () => {
    const tree = renderer
      .create(<Datetime inputProps={{ required: true }} />)
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('with name specified', () => {
    const tree = renderer
      .create(<Datetime inputProps={{ name: 'arbitrary-name' }} />)
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
 
  it('with className specified', () => {
    const tree = renderer
      .create(<Datetime inputProps={{ className: 'arbitrary-className' }} />)
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
});
 
it('isValidDate: only valid if after yesterday', () => {
  const yesterday = Datetime.moment().subtract(1, 'day');
  const valid = (current) => current.isAfter(yesterday);
  const tree = renderer.create(<Datetime isValidDate={valid} />).toJSON();
  expect(tree).toMatchSnapshot();
});
 
it('renderDay: specified', () => {
  const renderDay = (props, currentDate) => (
    <td {...props}>{'0' + currentDate.date()}</td>
  );
  const tree = renderer.create(<Datetime renderDay={renderDay} />).toJSON();
  expect(tree).toMatchSnapshot();
});
 
it('renderMonth: specified', () => {
  const renderMonth = (props, currentDate) => (
    <td {...props}>{'0' + currentDate.date()}</td>
  );
  const tree = renderer.create(<Datetime renderMonth={renderMonth} />).toJSON();
  expect(tree).toMatchSnapshot();
});
 
it('renderYear: specified', () => {
  const renderYear = (props, currentDate) => (
    <td {...props}>{'0' + currentDate.date()}</td>
  );
  const tree = renderer.create(<Datetime renderYear={renderYear} />).toJSON();
  expect(tree).toMatchSnapshot();
});