Skip to content

Commit

Permalink
Merge pull request aleksanderwozniak#614 from aleksanderwozniak/test
Browse files Browse the repository at this point in the history
Add a test suite
  • Loading branch information
aleksanderwozniak committed Nov 7, 2021
2 parents bc8ad29 + 60cfac1 commit 3abf0e7
Show file tree
Hide file tree
Showing 10 changed files with 2,680 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/src/table_calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {
final isWeekend = _isWeekend(day, weekendDays: widget.weekendDays);

Widget content = CellContent(
key: ValueKey('CellContent-${day.year}-${day.month}-${day.day}'),
day: day,
focusedDay: focusedDay,
calendarStyle: widget.calendarStyle,
Expand Down
202 changes: 202 additions & 0 deletions test/calendar_header_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright 2019 Aleksander Woźniak
// SPDX-License-Identifier: Apache-2.0

import 'package:flutter/material.dart';
import 'package:flutter/widgets.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,
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,
),
),
);
}

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: 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: 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: HeaderStyle(formatButtonVisible: false),
),
);

final formatButton = find.byType(FormatButton);
expect(formatButton, findsNothing);
},
);
}
96 changes: 96 additions & 0 deletions test/calendar_page_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2019 Aleksander Woźniak
// SPDX-License-Identifier: Apache-2.0

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:table_calendar/src/widgets/calendar_page.dart';

Widget setupTestWidget(Widget child) {
return Directionality(
textDirection: TextDirection.ltr,
child: child,
);
}

List<DateTime> visibleDays = getDaysInRange(
DateTime.utc(2021, 6, 27),
DateTime.utc(2021, 7, 31),
);

List<DateTime> getDaysInRange(DateTime first, DateTime last) {
final dayCount = last.difference(first).inDays + 1;
return List.generate(
dayCount,
(index) => DateTime.utc(first.year, first.month, first.day + index),
);
}

void main() {
testWidgets(
'CalendarPage lays out all the visible days',
(tester) async {
await tester.pumpWidget(
setupTestWidget(
CalendarPage(
visibleDays: visibleDays,
dayBuilder: (context, day) {
return Text('${day.day}');
},
dowVisible: false,
),
),
);

final expectedCellCount = visibleDays.length;
expect(find.byType(Text), findsNWidgets(expectedCellCount));
},
);

testWidgets(
'CalendarPage lays out 7 DOW labels',
(tester) async {
await tester.pumpWidget(
setupTestWidget(
CalendarPage(
visibleDays: visibleDays,
dayBuilder: (context, day) {
return Text('${day.day}');
},
dowVisible: true,
dowBuilder: (context, day) {
return Text('${day.weekday}');
},
),
),
);

final expectedCellCount = visibleDays.length;
final expectedDowLabels = 7;

expect(
find.byType(Text),
findsNWidgets(expectedCellCount + expectedDowLabels),
);
},
);

testWidgets(
'Throw AssertionError when CalendarPage is built with dowVisible set to true, but dowBuilder is absent',
(tester) async {
expect(() async {
await tester.pumpWidget(
setupTestWidget(
CalendarPage(
visibleDays: visibleDays,
dayBuilder: (context, day) {
return Text('${day.day}');
},
dowVisible: true,
),
),
);
}, throwsAssertionError);
},
);
}
Loading

0 comments on commit 3abf0e7

Please sign in to comment.