calendar_header_test.dart
5.85 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2019 Aleksander Woźniak
// SPDX-License-Identifier: Apache-2.0
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart' as intl;
import 'package:table_calendar/src/customization/header_style.dart';
import 'package:table_calendar/src/shared/utils.dart';
import 'package:table_calendar/src/widgets/calendar_header.dart';
import 'package:table_calendar/src/widgets/custom_icon_button.dart';
import 'package:table_calendar/src/widgets/format_button.dart';
import 'common.dart';
final focusedMonth = DateTime.utc(2021, 7, 15);
Widget setupTestWidget({
HeaderStyle headerStyle = const HeaderStyle(),
VoidCallback? onLeftChevronTap,
VoidCallback? onRightChevronTap,
VoidCallback? onHeaderTap,
VoidCallback? onHeaderLongPress,
VoidCallback? selectPreviousYear,
VoidCallback? selectNextYear,
Function(CalendarFormat)? onFormatButtonTap,
Map<CalendarFormat, String> availableCalendarFormats = calendarFormatMap,
}) {
return Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: CalendarHeader(
focusedMonth: focusedMonth,
calendarFormat: CalendarFormat.month,
headerStyle: headerStyle,
onLeftChevronTap: () => onLeftChevronTap?.call(),
onRightChevronTap: () => onRightChevronTap?.call(),
onHeaderTap: () => onHeaderTap?.call(),
onHeaderLongPress: () => onHeaderLongPress?.call(),
onFormatButtonTap: (format) => onFormatButtonTap?.call(format),
availableCalendarFormats: availableCalendarFormats,
selectPreviousYear: () => selectPreviousYear?.call(),
selectNextYear: () => selectNextYear?.call(),
),
),
);
}
void main() {
testWidgets(
'Displays corrent month and year for given focusedMonth',
(tester) async {
await tester.pumpWidget(setupTestWidget());
final headerText = intl.DateFormat.yMMMM().format(focusedMonth);
expect(find.byType(CalendarHeader), findsOneWidget);
expect(find.text(headerText), findsOneWidget);
},
);
testWidgets(
'Ensure chevrons and FormatButton are visible by default, test onTap callbacks',
(tester) async {
bool leftChevronTapped = false;
bool rightChevronTapped = false;
bool headerTapped = false;
bool headerLongPressed = false;
bool formatButtonTapped = false;
await tester.pumpWidget(
setupTestWidget(
onLeftChevronTap: () => leftChevronTapped = true,
onRightChevronTap: () => rightChevronTapped = true,
onHeaderTap: () => headerTapped = true,
onHeaderLongPress: () => headerLongPressed = true,
onFormatButtonTap: (_) => formatButtonTapped = true,
),
);
final leftChevron = find.widgetWithIcon(
CustomIconButton,
Icons.chevron_left,
);
final rightChevron = find.widgetWithIcon(
CustomIconButton,
Icons.chevron_right,
);
final header = find.byType(CalendarHeader);
final formatButton = find.byType(FormatButton);
expect(leftChevron, findsOneWidget);
expect(rightChevron, findsOneWidget);
expect(header, findsOneWidget);
expect(formatButton, findsOneWidget);
expect(leftChevronTapped, false);
expect(rightChevronTapped, false);
expect(headerTapped, false);
expect(headerLongPressed, false);
expect(formatButtonTapped, false);
await tester.tap(leftChevron);
await tester.pumpAndSettle();
await tester.tap(rightChevron);
await tester.pumpAndSettle();
await tester.tap(header);
await tester.pumpAndSettle();
await tester.longPress(header);
await tester.pumpAndSettle();
await tester.tap(formatButton);
await tester.pumpAndSettle();
expect(leftChevronTapped, true);
expect(rightChevronTapped, true);
expect(headerTapped, true);
expect(headerLongPressed, true);
expect(formatButtonTapped, true);
},
);
testWidgets(
'When leftChevronVisible is false, do not show the left chevron',
(tester) async {
await tester.pumpWidget(
setupTestWidget(
headerStyle: const HeaderStyle(
leftChevronVisible: false,
),
),
);
final leftChevron = find.widgetWithIcon(
CustomIconButton,
Icons.chevron_left,
);
final rightChevron = find.widgetWithIcon(
CustomIconButton,
Icons.chevron_right,
);
expect(leftChevron, findsNothing);
expect(rightChevron, findsOneWidget);
},
);
testWidgets(
'When rightChevronVisible is false, do not show the right chevron',
(tester) async {
await tester.pumpWidget(
setupTestWidget(
headerStyle: const HeaderStyle(
rightChevronVisible: false,
),
),
);
final leftChevron = find.widgetWithIcon(
CustomIconButton,
Icons.chevron_left,
);
final rightChevron = find.widgetWithIcon(
CustomIconButton,
Icons.chevron_right,
);
expect(leftChevron, findsOneWidget);
expect(rightChevron, findsNothing);
},
);
testWidgets(
'When availableCalendarFormats has a single format, do not show the FormatButton',
(tester) async {
await tester.pumpWidget(
setupTestWidget(
availableCalendarFormats: const {CalendarFormat.month: 'Month'},
),
);
final formatButton = find.byType(FormatButton);
expect(formatButton, findsNothing);
},
);
testWidgets(
'When formatButtonVisible is false, do not show the FormatButton',
(tester) async {
await tester.pumpWidget(
setupTestWidget(
headerStyle: const HeaderStyle(formatButtonVisible: false),
),
);
final formatButton = find.byType(FormatButton);
expect(formatButton, findsNothing);
},
);
}