-
Notifications
You must be signed in to change notification settings - Fork 48
/
Time.spec.js
114 lines (93 loc) · 3.71 KB
/
Time.spec.js
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
import React from 'react';
import Time, { FromToTime, FormatTime } from '../Time';
import { shallow } from 'enzyme';
import moment from 'moment-timezone';
jest.mock('app/config', () => {
return {
timezone: 'GMT',
};
});
describe('<Time />', () => {
it('should show a date with a default format', () => {
const dateTime = '2016-02-02T22:17:21.838103Z';
const wrapper = shallow(<Time time={dateTime} />);
const expected = <time dateTime={dateTime}>2016-02-2</time>;
expect(wrapper.contains(expected)).toEqual(true);
});
it('should show a date with a custom format', () => {
const dateTime = '2016-02-02T22:17:21.838103Z';
const wrapper = shallow(<Time format="HH:mm" time={dateTime} />);
const expected = <time dateTime={dateTime}>22:17</time>;
expect(wrapper.contains(expected)).toEqual(true);
});
it('should show a readable date', () => {
const dateTime = '2016-02-02T22:17:21.838103Z';
const wrapper = shallow(<Time dateTime={dateTime} wordsAgo />);
const time = wrapper.find('time');
expect(time.prop('dateTime')).toEqual(dateTime);
expect(time.prop('children')).toContain('ago'); // "for 3 dager siden"
});
});
describe('<FormatTime />', () => {
it('should render the year if it doesnt equal the current year', () => {
const dateTime = '2016-01-18T20:00:00Z';
const wrapper = shallow(<FormatTime time={dateTime} />);
const time = wrapper.find(Time);
const format = 'dddd DD. MMM YYYY HH:mm';
expect(time.prop('time')).toEqual(moment(dateTime));
expect(time.prop('format')).toEqual(format);
});
it('should not render the year if it equals the current year', () => {
const currentYear = moment().format('YYYY');
const dateTime = `${currentYear}-01-18T20:00:00Z`;
const wrapper = shallow(<FormatTime time={dateTime} />);
const time = wrapper.find('Time');
const format = 'dddd DD. MMM HH:mm';
expect(time.prop('time')).toEqual(moment(dateTime));
expect(time.prop('format')).toEqual(format);
});
});
describe('<FromToTime />', () => {
it('should only render day once when start day === end day', () => {
const from = '2016-01-18T20:00:00Z';
const to = '2016-01-18T22:00:00Z';
const output = 'Monday 18. Jan 2016 20:00 - 22:00';
const wrapper = shallow(<FromToTime from={from} to={to} />);
expect(wrapper.render().text()).toEqual(output);
});
it('should render both days fully when start day != end day', () => {
const from = '2016-01-18T20:00:00Z';
const to = '2016-01-19T22:00:00Z';
const output = 'Monday 18. Jan 2016 20:00 - Tuesday 19. Jan 2016 22:00';
const wrapper = shallow(<FromToTime from={from} to={to} />);
expect(wrapper.render().text()).toEqual(output);
});
it('should not render year if year === currentYear', () => {
const from = '2017-01-18T20:00:00Z';
const to = '2017-01-19T22:00:00Z';
const output = 'Wednesday 18. January, 20:00 - Thursday 19. January, 22:00';
const _now = Date.now;
const mockDate = +moment(from);
Date.now = jest.fn(() => mockDate);
try {
const wrapper = shallow(<FromToTime from={from} to={to} />);
expect(wrapper.render().text()).toEqual(output);
} finally {
Date.now = _now;
}
});
it('should not render year if year === currentYear, and day only once if equal', () => {
const from = '2017-01-18T20:00:00Z';
const to = '2017-01-18T21:00:00Z';
const output = 'Wednesday 18. January, 20:00 - 21:00';
const _now = Date.now;
const mockDate = +moment(from);
Date.now = jest.fn(() => mockDate);
try {
const wrapper = shallow(<FromToTime from={from} to={to} />);
expect(wrapper.render().text()).toEqual(output);
} finally {
Date.now = _now;
}
});
});