Navigation.test.tsx
3.91 KB
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
import { DayPickerProps } from 'DayPicker';
import { customRender } from 'test/render';
import { getNextButton, getPrevButton } from 'test/selectors';
import { user } from 'test/user';
import { Navigation, NavigationProps } from './Navigation';
let root: HTMLElement;
function setup(props: NavigationProps, dayPickerProps?: DayPickerProps) {
const view = customRender(<Navigation {...props} />, dayPickerProps);
root = view.container.firstChild as HTMLElement;
}
const props: NavigationProps = {
previousMonth: new Date(2021, 3),
nextMonth: new Date(2021, 5),
displayMonth: new Date(2021, 4),
hidePrevious: false,
hideNext: false,
onNextClick: jest.fn(),
onPreviousClick: jest.fn()
};
const dayPickerProps = {
classNames: {
nav: 'foo'
},
styles: {
nav: { color: 'red' }
},
components: {
IconRight: () => <svg>IconRight</svg>,
IconLeft: () => <svg>IconLeft</svg>
}
};
describe('when rendered', () => {
beforeEach(() => {
setup(props, dayPickerProps);
});
test('should add the class name', () => {
expect(root).toHaveClass(dayPickerProps.classNames.nav);
});
test('should apply the style', () => {
expect(root).toHaveStyle(dayPickerProps.styles.nav);
});
test('the previous button should display the left icon', () => {
const icons = root.getElementsByTagName('svg');
expect(icons[0]).toHaveTextContent('IconLeft');
});
test('the next button should display the right icon', () => {
const icons = root.getElementsByTagName('svg');
expect(icons[1]).toHaveTextContent('IconRight');
});
test('the previous button should be named "previous-month"', () => {
expect(getPrevButton()).toHaveAttribute('name', 'previous-month');
});
test('the next button should be named "next-month"', () => {
expect(getNextButton()).toHaveAttribute('name', 'next-month');
});
beforeEach(async () => {
await user.click(getPrevButton());
});
test('should call "onPreviousClick"', () => {
expect(props.onPreviousClick).toHaveBeenCalled();
});
describe('when clicking the next button', () => {
beforeEach(async () => {
await user.click(getNextButton());
});
test('should call "onNextClick"', () => {
expect(props.onNextClick).toHaveBeenCalled();
});
});
});
describe('when in right-to-left direction', () => {
beforeEach(() => {
setup(props, { ...dayPickerProps, dir: 'rtl' });
});
test('the previous button should display the right icon', () => {
const icons = root.getElementsByTagName('svg');
expect(icons[0]).toHaveTextContent('IconRight');
});
test('the next button should display the left icon', () => {
const icons = root.getElementsByTagName('svg');
expect(icons[1]).toHaveTextContent('IconLeft');
});
describe('when clicking the previous button', () => {
beforeEach(async () => {
await user.click(getPrevButton());
});
test('should call "onPreviousClick"', () => {
expect(props.onPreviousClick).toHaveBeenCalled();
});
});
describe('when clicking the next button', () => {
beforeEach(async () => {
await user.click(getNextButton());
});
test('should call "onNextClick"', () => {
expect(props.onNextClick).toHaveBeenCalled();
});
});
});
describe('when the previous month is undefined', () => {
beforeEach(() => {
setup({ ...props, previousMonth: undefined }, dayPickerProps);
});
test('the previous button should be disabled', () => {
expect(getPrevButton()).toBeDisabled();
});
test('the next button should be enabled', () => {
expect(getNextButton()).toBeEnabled();
});
});
describe('when the next month is undefined', () => {
beforeEach(() => {
setup({ ...props, nextMonth: undefined }, dayPickerProps);
});
test('the previous button should be enabled', () => {
expect(getPrevButton()).toBeEnabled();
});
test('the next button should be disabled', () => {
expect(getNextButton()).toBeDisabled();
});
});