syncfusion / flutter-examples

This repository contains the Syncfusion Flutter UI widgets examples and the guide to use them.
Other
1.98k stars 775 forks source link

Calendar widget test #704

Closed BinaryITSolution closed 1 year ago

BinaryITSolution commented 2 years ago

Can you add the sample code to add a Widget Test for calendar

MuniappanSubramanian commented 2 years ago

You can achieve your requirement by using the "flutter_test" dependency. Please follow the steps below to meet your needs.

  1. Add the flutter test dependency

Before writing tests, include the flutter_test dependency in the dev_dependencies section of the pubspec.yaml file. dev_dependencies: flutter_test: sdk: flutter

Refer to the below link for more details about widget testing in the Flutter https://docs.flutter.dev/cookbook/testing/widget/introduction

  1. Then create a widget to test and create a testWidgets test. By using the testWidgets() from the flutter_test package, you can define a test. Here I have prepared a simple test, in which I have checked whether the next month of the calendar is "December" or not. I got the visible dates collection from the onViewChanged callback of the calendar. Refer to the below code snippets:

void main() { testWidgets("Flutter Widget Test", (WidgetTester tester) async { await tester.pumpWidget(const MyApp()); var button = find.text("NextMonth"); expect(button, findsOneWidget); await tester.tap(button); await tester.pump(); var visibleDates = getVisibleDates(); final DateTime nextMonth = visibleDates[0]; expect(nextMonth.month, DateTime.december); expect(nextMonth, DateTime(2022, 12)); }); }

  1. To run the test, use the below command in the terminal. flutter test./test/widget_testing.dart

I have attached a sample of the same. Please find the attached sample.

widget_test.zip