This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
widget_test.dart
72 lines (63 loc) · 2.52 KB
/
widget_test.dart
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
import 'package:app/app.dart';
import 'package:app/data/model/result.dart';
import 'package:app/ui/component/loading/loading.dart';
import 'package:app/ui/news/news_page.dart';
import 'package:app/ui/news/news_view_model.dart';
import 'package:app/ui/route/app_route.gr.dart';
import 'package:app/ui/user_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mocktail/mocktail.dart';
import 'package:mocktail_image_network/mocktail_image_network.dart';
import '../data/dummy/dummy_news.dart';
class MockNewsViewModel extends Mock implements NewsViewModel {}
class MockUserViewModel extends Mock implements UserViewModel {}
void main() {
final mockNewsViewModel = MockNewsViewModel();
when(mockNewsViewModel.fetchNews).thenAnswer((_) => Future.value());
when(() => mockNewsViewModel.news)
.thenReturn(Result.success(data: dummyNews));
final mockUserViewModel = MockUserViewModel();
when(mockUserViewModel.signIn).thenAnswer((_) => Future.value());
when(mockUserViewModel.signOut).thenAnswer((_) => Future.value());
testWidgets('App widget test', (tester) async {
await tester.pumpWidget(
ProviderScope(
overrides: [
newsViewModelProvider.overrideWithValue(mockNewsViewModel),
userViewModelProvider.overrideWithValue(mockUserViewModel),
],
child: const App(),
),
);
});
testWidgets('HomePage widget test', (tester) async {
await mockNetworkImages(() async {
final appRouter = AppRouter();
await tester.pumpWidget(
ProviderScope(
overrides: [
newsViewModelProvider.overrideWithValue(mockNewsViewModel),
userViewModelProvider.overrideWithValue(mockUserViewModel),
],
child: MaterialApp.router(
localizationsDelegates: L10n.localizationsDelegates,
supportedLocales: L10n.supportedLocales,
routeInformationParser: appRouter.defaultRouteParser(),
routerDelegate: appRouter.delegate(),
),
),
);
await tester.pumpAndSettle();
expect(appRouter.current.name == HomeRoute.name, true);
expect(find.byType(NewsPage), findsOneWidget);
});
});
testWidgets('Loading widget test', (tester) async {
const loading = Loading();
await tester.pumpWidget(const ProviderScope(child: loading));
expect(find.byWidget(loading), findsOneWidget);
});
}