Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Test Dependencies
- import 'package:flutter/material.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
- import 'package:flutter_test/flutter_test.dart';
- import 'package:mockito/mockito.dart';
- // TodoList Dependencies
- import 'package:todolist/widgets/todo_list.dart';
- import 'package:todolist/blocs/todo_list/bloc.dart';
- import 'package:todolist/blocs/todo_list/state.dart';
- class MockTodoListBloc extends Mock implements TodoListBloc {}
- void main() {
- // Testing the TodoList widget
- group('TodoList', () {
- // Test how it reacts when the Bloc state is TodoLoadingState
- group('TodoLoadingState', () {
- testWidgets('should render a loading icon when handling the TodoLoadingState', (WidgetTester tester) async {
- // Setup the mock bloc
- TodoListBloc todoListBloc = MockTodoListBloc();
- // Mock the injected TodoListBloc state.
- // See _BlocBuilderBaseState for how it uses currentState to setup the state
- when(todoListBloc.currentState).thenReturn(TodoLoadingState());
- // Wrap our widget test with a BlocProvider since our widget depends
- // on a BlocBuilder. Our mock TodoListBloc will be passed
- await tester.pumpWidget(
- MaterialApp(
- home: BlocProvider<TodoListBloc>(
- builder: (context) => todoListBloc,
- child: TodoList(),
- ),
- )
- );
- expect(find.byType(CircularProgressIndicator), findsOneWidget);
- });
- });
- // Test how it reacts when the Bloc state is TodoLoadingState
- group('TodoLoadedState', () {
- testWidgets('should render a ListView of ListTile todos when TodoLoadedState', (WidgetTester tester) async {
- // Mock our todo list items
- final List<String> todos = [ "Take out the trash", "Feed the cat" ];
- // Setup the mock bloc
- TodoListBloc todoListBloc = MockTodoListBloc();
- // Mock the injected TodoListBloc state and pass our mock todo list items.
- // See _BlocBuilderBaseState for how it uses currentState to setup the state
- when(todoListBloc.currentState).thenReturn(TodoLoadedState(
- todos: todos
- ));
- // Wrap our widget test with a BlocProvider since our widget depends
- // on a BlocBuilder. Our mock TodoListBloc will be passed
- await tester.pumpWidget(
- MaterialApp(
- home: BlocProvider<TodoListBloc>(
- builder: (context) => todoListBloc,
- child: TodoList(),
- ),
- )
- );
- expect(find.byType(ListView), findsOneWidget);
- expect(find.byType(ListTile), findsNWidgets(todos.length));
- });
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement