Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
test: transition mixin test
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiMiguel committed Nov 24, 2023
1 parent 11b55a9 commit 8f0d8ed
Showing 1 changed file with 186 additions and 0 deletions.
186 changes: 186 additions & 0 deletions test/home/widgets/transition_screen_mixin_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import 'dart:async';

import 'package:bloc_test/bloc_test.dart';
import 'package:dash_ai_search/home/home.dart';
import 'package:dash_ai_search/home/widgets/transition_screen_mixin.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import '../../helpers/helpers.dart';

class _MockHomeBloc extends MockBloc<HomeEvent, HomeState>
implements HomeBloc {}

class _MockAnimationController extends Mock implements AnimationController {}

class SampleWidget extends StatefulWidget {
const SampleWidget({
required this.mockedEnterTransitionController,
required this.mockedExitTransitionController,
super.key,
});

final AnimationController mockedEnterTransitionController;
final AnimationController mockedExitTransitionController;

@override
State<SampleWidget> createState() => _SampleWidgetState();
}

class _SampleWidgetState extends State<SampleWidget>
with SingleTickerProviderStateMixin, TransitionScreenMixin {
@override
List<Status> get forwardEnterStatuses => [Status.welcome];

@override
List<Status> get forwardExitStatuses => [Status.welcomeToAskQuestion];

@override
List<Status> get backEnterStatuses => [Status.askQuestion];

@override
List<Status> get backExitStatuses => [Status.askQuestionToThinking];

@override
void initializeTransitionController() {
enterTransitionController = widget.mockedEnterTransitionController;
exitTransitionController = widget.mockedExitTransitionController;
}

@override
Widget build(BuildContext context) {
return Container();
}
}

void main() {
group('TransitionScreenMixin', () {
late HomeBloc homeBloc;
late AnimationController enterAnimationController;
late AnimationController exitAnimationController;

setUp(() {
homeBloc = _MockHomeBloc();
when(() => homeBloc.state).thenReturn(HomeState());

enterAnimationController = _MockAnimationController();
when(() => enterAnimationController.forward())
.thenAnswer((invocation) => TickerFuture.complete());
when(() => enterAnimationController.reverse())
.thenAnswer((invocation) => TickerFuture.complete());
when(() => enterAnimationController.addStatusListener(any()))
.thenAnswer((invocation) {});
when(() => enterAnimationController.dispose())
.thenAnswer((invocation) {});

exitAnimationController = _MockAnimationController();
when(() => exitAnimationController.forward())
.thenAnswer((invocation) => TickerFuture.complete());
when(() => exitAnimationController.reverse())
.thenAnswer((invocation) => TickerFuture.complete());
when(() => exitAnimationController.addStatusListener(any()))
.thenAnswer((invocation) {});
when(() => exitAnimationController.dispose()).thenAnswer((invocation) {});
});

testWidgets(
'calls enterAnimationController forward '
'when status is in forwardEnterStatuses', (tester) async {
final streamController = StreamController<HomeState>();
whenListen(
homeBloc,
streamController.stream,
initialState: HomeState(),
);

await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: SampleWidget(
mockedEnterTransitionController: enterAnimationController,
mockedExitTransitionController: exitAnimationController,
),
),
);

streamController.add(const HomeState());
verify(() => enterAnimationController.forward()).called(1);
});

testWidgets(
'calls exitTransitionController forward '
'when status is in forwardExitStatuses', (tester) async {
final streamController = StreamController<HomeState>();
whenListen(
homeBloc,
streamController.stream,
initialState: HomeState(),
);

await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: SampleWidget(
mockedEnterTransitionController: enterAnimationController,
mockedExitTransitionController: exitAnimationController,
),
),
);

streamController
.add(const HomeState(status: Status.welcomeToAskQuestion));
verify(() => exitAnimationController.forward()).called(1);
});

testWidgets(
'calls exitAnimationController reverse '
'when status is in backEnterStatuses', (tester) async {
final streamController = StreamController<HomeState>();
whenListen(
homeBloc,
streamController.stream,
initialState: HomeState(),
);

await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: SampleWidget(
mockedEnterTransitionController: enterAnimationController,
mockedExitTransitionController: exitAnimationController,
),
),
);

streamController.add(const HomeState(status: Status.askQuestion));
verify(() => exitAnimationController.reverse()).called(1);
});

testWidgets(
'calls enterAnimationController reverse '
'when status is in backExitStatuses', (tester) async {
final streamController = StreamController<HomeState>();
whenListen(
homeBloc,
streamController.stream,
initialState: HomeState(),
);

await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: SampleWidget(
mockedEnterTransitionController: enterAnimationController,
mockedExitTransitionController: exitAnimationController,
),
),
);

streamController
.add(const HomeState(status: Status.askQuestionToThinking));
verify(() => enterAnimationController.reverse()).called(1);
});
});
}

0 comments on commit 8f0d8ed

Please sign in to comment.