tests(card_swiper): add tests for card_swiper

This commit is contained in:
ricardodalarme 2023-03-26 12:54:37 -03:00
parent 4a6f664b1d
commit 909c2fa00b
5 changed files with 663 additions and 17 deletions

View File

@ -1,7 +1,9 @@
import 'package:flutter_card_swiper/src/card_swiper_controller.dart';
import 'package:flutter_card_swiper/src/enums.dart';
import 'package:flutter/material.dart';
import 'package:flutter_card_swiper/flutter_card_swiper.dart';
import 'package:flutter_test/flutter_test.dart';
import 'utils.dart';
void main() {
group('CardSwiperController', () {
test('swipe() should change the state to swipe', () {
@ -39,5 +41,170 @@ void main() {
controller.undo();
expect(controller.state, CardSwiperState.undo);
});
testWidgets('swipe() should swipe the card to the defined direction',
(tester) async {
final controller = CardSwiperController();
final swiperKey = GlobalKey();
var direction = CardSwiperDirection.none;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.top,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
),
);
controller.swipe();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.top);
});
testWidgets('swipeLeft() should swipe the card to the left',
(tester) async {
final controller = CardSwiperController();
final swiperKey = GlobalKey();
var direction = CardSwiperDirection.none;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.left,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
),
);
controller.swipeLeft();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.left);
});
testWidgets('swipeRight() should swipe the card to the right',
(tester) async {
final controller = CardSwiperController();
final swiperKey = GlobalKey();
var direction = CardSwiperDirection.none;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
),
);
controller.swipeRight();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.right);
});
testWidgets('swipeTop() should swipe the card to the top', (tester) async {
final controller = CardSwiperController();
final swiperKey = GlobalKey();
var direction = CardSwiperDirection.none;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.top,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
),
);
controller.swipeTop();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.top);
});
testWidgets('swipeBottom() should swipe the card to the bottom',
(tester) async {
final controller = CardSwiperController();
final swiperKey = GlobalKey();
var direction = CardSwiperDirection.none;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.bottom,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
direction = swipeDirection;
return true;
},
),
),
);
controller.swipeBottom();
await tester.pumpAndSettle();
expect(direction, CardSwiperDirection.bottom);
});
testWidgets('undo() should undo the last swipe', (tester) async {
final controller = CardSwiperController();
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
controller: controller,
cardsCount: 10,
cardBuilder: genericBuilder,
direction: CardSwiperDirection.bottom,
),
),
);
controller.swipe();
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
controller.undo();
await tester.pumpAndSettle();
expect(find.text(getIndexText(0)), findsOneWidget);
});
});
}

View File

@ -1 +1,458 @@
void main() {}
import 'package:flutter/material.dart';
import 'package:flutter_card_swiper/flutter_card_swiper.dart';
import 'package:flutter_test/flutter_test.dart';
import 'utils.dart';
void main() {
group('CardSwiper', () {
testWidgets('pump widget', (WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 3,
cardBuilder: genericBuilder,
),
),
);
expect(find.byKey(swiperKey), findsOneWidget);
});
testWidgets(
'when initialIndex is defined expect the related card be on top',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
const initialIndex = 7;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
initialIndex: initialIndex,
),
),
);
expect(find.text(getIndexText(initialIndex)), findsOneWidget);
});
testWidgets('when swiping right expect to see the next card',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
});
testWidgets('when swiping left expect to see the next card',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(-300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
});
testWidgets('when swiping top expect to see the next card',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(0, -300));
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
});
testWidgets('when swiping bottom expect to see the next card',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(0, 300));
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
});
testWidgets('when numberOfCardsDisplayed is 1 expect to see only one card',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
),
),
);
expect(find.byType(Container), findsOneWidget);
});
testWidgets('when numberOfCardsDisplayed is 10 expect to see 10 cards',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 10,
cardBuilder: genericBuilder,
),
),
);
expect(find.byType(Container), findsNWidgets(10));
});
testWidgets('when isDisabled is true expect to block swipes',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
isDisabled: true,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(0)), findsOneWidget);
});
testWidgets('when isDisabled is false expect to allow swipes',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
});
testWidgets('when isLoop is true expect to loop the cards',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 2,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(0)), findsOneWidget);
});
testWidgets('when isLoop is false expect to not return to the first card',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 2,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
isLoop: false,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(1)), findsOneWidget);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.byType(Container), findsNothing);
});
testWidgets('when onSwipe is defined expect to call it on swipe',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
var isCalled = false;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, direction) {
isCalled = true;
return true;
},
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(isCalled, true);
});
testWidgets(
'when onSwipe is defined and it returns false expect to not swipe',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, direction) {
return false;
},
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(find.text(getIndexText(0)), findsOneWidget);
});
testWidgets(
'when onSwipe is defined expect it to return the correct direction',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
final directions = <CardSwiperDirection>[];
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 10,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
directions.add(swipeDirection);
return true;
},
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(-300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(0, 300));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(0, -300));
await tester.pumpAndSettle();
expect(directions, [
CardSwiperDirection.right,
CardSwiperDirection.left,
CardSwiperDirection.bottom,
CardSwiperDirection.top,
]);
});
testWidgets(
'when onSwipe is defined and isLoop is true expect it to return the correct indexes',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
final oldIndexes = <int>[];
final newIndexes = <int?>[];
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 3,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
oldIndexes.add(oldIndex);
newIndexes.add(currentIndex);
return true;
},
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(oldIndexes, [0, 1, 2, 0]);
expect(newIndexes, [1, 2, 0, 1]);
});
testWidgets(
'when onSwipe is defined and isLoop is false expect it to return the correct indexes',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
final oldIndexes = <int>[];
final newIndexes = <int?>[];
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 3,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
onSwipe: (oldIndex, currentIndex, swipeDirection) {
oldIndexes.add(oldIndex);
newIndexes.add(currentIndex);
return true;
},
isLoop: false,
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(oldIndexes, [0, 1, 2]);
expect(newIndexes, [1, 2, null]);
});
testWidgets('when onEnd is defined expect to call it on end',
(WidgetTester tester) async {
final swiperKey = GlobalKey();
var isCalled = false;
await tester.pumpWidget(
MaterialApp(
home: CardSwiper(
key: swiperKey,
cardsCount: 2,
numberOfCardsDisplayed: 1,
cardBuilder: genericBuilder,
onEnd: () {
isCalled = true;
},
),
),
);
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
await tester.drag(find.byKey(swiperKey), const Offset(300, 0));
await tester.pumpAndSettle();
expect(isCalled, true);
});
});
}

View File

@ -5,7 +5,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
group('num.isBetween', () {
test('should return true when value is within range', () {
test('when value is within range expect to return true', () {
const value = 5;
const from = 1;
const to = 10;
@ -15,7 +15,7 @@ void main() {
expect(result, isTrue);
});
test('should return true when value is equal to the range limits', () {
test('when value is equal to the range limits expect to return true', () {
const value = 1;
const from = 1;
const to = 1;
@ -25,7 +25,7 @@ void main() {
expect(result, isTrue);
});
test('should return false when value is outside the range', () {
test('when value is outside the range expect to return false', () {
const value = 15;
const from = 1;
const to = 10;
@ -35,7 +35,7 @@ void main() {
expect(result, isFalse);
});
test('should return false when the range limits are inverted', () {
test('when the range limits are inverted expect to return false', () {
const value = 5;
const from = 10;
const to = 1;
@ -47,27 +47,27 @@ void main() {
});
group('CardSwiperDirection.axis', () {
test('should return horizontal when direction is left', () {
test('when direction is left expect to return horizontal ', () {
final axis = CardSwiperDirection.left.axis;
expect(axis, Axis.horizontal);
});
test('should return horizontal when direction is right', () {
test('when direction is right expect to return horizontal', () {
final axis = CardSwiperDirection.right.axis;
expect(axis, Axis.horizontal);
});
test('should return vertical when direction is top', () {
test('when direction is top expect to return vertical', () {
final axis = CardSwiperDirection.top.axis;
expect(axis, Axis.vertical);
});
test('should return vertical when direction is bottom', () {
test('when direction is bottom expect to return vertical ', () {
final axis = CardSwiperDirection.bottom.axis;
expect(axis, Axis.vertical);
});
test('should throw exception when direction is none', () {
test('when direction is none expect to throw exception ', () {
expect(() => CardSwiperDirection.none.axis, throwsException);
});
});

View File

@ -2,20 +2,21 @@ import 'package:flutter_card_swiper/src/undoable.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('should store and retrieve state', () {
test('when defined the initial value expect to store and retrieve state', () {
final undoable = Undoable<int>(0);
expect(undoable.state, equals(0));
expect(undoable.previousState, isNull);
});
test('should store previous state when state is changed', () {
test('when state is changed expect to store previous state', () {
final undoable = Undoable<int>(0);
undoable.state = 1;
expect(undoable.state, equals(1));
expect(undoable.previousState, equals(0));
});
test('should store previous state when state is changed multiple times', () {
test('when state is changed multiple times expect to store previous state',
() {
final undoable = Undoable<int>(0);
undoable.state = 1;
undoable.state = 2;
@ -23,7 +24,7 @@ void main() {
expect(undoable.previousState, equals(1));
});
test('should return previous state when undo is called', () {
test('when undo is called expect to return previous state', () {
final undoable = Undoable<int>(0);
undoable.state = 1;
undoable.undo();
@ -31,7 +32,8 @@ void main() {
expect(undoable.previousState, isNull);
});
test('should return previous state when undo is called multiple times', () {
test('when undo is called multiple times expect to return previous state',
() {
final undoable = Undoable<int>(0);
undoable.state = 1;
undoable.state = 2;

20
test/utils.dart Normal file
View File

@ -0,0 +1,20 @@
import 'package:flutter/material.dart';
Widget? genericBuilder(BuildContext context, int index) {
return Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10),
),
child: Text(
getIndexText(index),
style: const TextStyle(fontSize: 24, color: Colors.white),
),
);
}
String getIndexText(int index) {
return 'Card $index';
}