aboutsummaryrefslogtreecommitdiffstats
path: root/packages/flutter_calendar_carousel/test/flutter_calendar_carousel_test.dart
blob: 1a171493f2491f398519d514871f30799641aa4c (plain)
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
// This is a basic Flutter widget test.
// To perform an interaction with a widget in your test, use the WidgetTester utility that Flutter
// provides. For example, you can send tap and scroll gestures. You can also use WidgetTester to
// find child widgets in the widget tree, read text, and verify that the values of widget properties
// are correct.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart';

import '../lib/classes/event.dart';

Type typeOf<T>() => T;

void main() {
  testWidgets('Default test for Calendar Carousel',
      (WidgetTester tester) async {
    DateTime? pressedDay;
    //  Build our app and trigger a frame.
    final carousel = CalendarCarousel(
      daysHaveCircularBorder: null,
      weekendTextStyle: TextStyle(
        color: Colors.red,
      ),
      thisMonthDayBorderColor: Colors.grey,
      headerText: 'Custom Header',
      weekFormat: true,
      height: 200.0,
      showIconBehindDayText: true,
      customGridViewPhysics: NeverScrollableScrollPhysics(),
      markedDateShowIcon: true,
      markedDateIconMaxShown: 2,
      selectedDayTextStyle: TextStyle(
        color: Colors.yellow,
      ),
      todayTextStyle: TextStyle(
        color: Colors.blue,
      ),
      markedDateIconBuilder: (Event event) {
        return event.icon ?? Icon(Icons.help_outline);
      },
      todayButtonColor: Colors.transparent,
      todayBorderColor: Colors.green,
      markedDateMoreShowTotal: true,
      // null for not showing hidden events indicator
      onDayPressed: (date, event) {
        pressedDay = date;
      },
    );
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Container(
          child: carousel,
        ),
      ),
    ));

    expect(find.byWidget(carousel), findsOneWidget);
    expect(pressedDay, isNull);
  });

  testWidgets(
    'make sure onDayPressed is called when the user tap',
    (WidgetTester tester) async {
      DateTime? pressedDay;

      final carousel = CalendarCarousel(
        weekFormat: true,
        height: 200.0,
        onDayPressed: (date, event) {
          pressedDay = date;
        },
      );
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Container(
              child: carousel,
            ),
          ),
        ),
      );

      expect(find.byWidget(carousel), findsOneWidget);

      expect(pressedDay, isNull);

      await tester.tap(
          find.text(DateTime.now().subtract(Duration(days: 1)).day.toString()));

      await tester.pump();

      expect(pressedDay, isNotNull);
    },
  );

  testWidgets(
    'should do nothing when the user tap and onDayPressed is not provided',
    (WidgetTester tester) async {
      final carousel = CalendarCarousel(
        weekFormat: true,
        height: 200.0,
      );
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Container(
              child: carousel,
            ),
          ),
        ),
      );

      expect(find.byWidget(carousel), findsOneWidget);

      await tester.tap(
          find.text(DateTime.now().subtract(Duration(days: 1)).day.toString()));
      await tester.pump();
    },
  );

  testWidgets(
    'make sure onDayLongPressed is called when the user press and hold',
    (WidgetTester tester) async {
      DateTime? longPressedDay;

      final carousel = CalendarCarousel(
        weekFormat: true,
        height: 200.0,
        onDayLongPressed: (date) {
          longPressedDay = date;
        },
      );
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Container(
              child: carousel,
            ),
          ),
        ),
      );

      expect(find.byWidget(carousel), findsOneWidget);

      expect(longPressedDay, isNull);

      await tester.longPress(
          find.text(DateTime.now().subtract(Duration(days: 1)).day.toString()));
      await tester.pump();

      expect(longPressedDay, isNotNull);
    },
  );

  testWidgets(
    'should do nothing when the user press and hold and onDayLongPressed is not provided',
    (WidgetTester tester) async {
      final carousel = CalendarCarousel(
        weekFormat: true,
        height: 200.0,
      );
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Container(
              child: carousel,
            ),
          ),
        ),
      );

      expect(find.byWidget(carousel), findsOneWidget);

      await tester.longPress(
          find.text(DateTime.now().subtract(Duration(days: 1)).day.toString()));
      await tester.pump();
    },
  );
}