Skip to content

Commit 696aa28

Browse files
committed
fix: Flutter test file renamed to _test.dart
1 parent d6be28e commit 696aa28

File tree

4 files changed

+112
-98
lines changed

4 files changed

+112
-98
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ else ifeq ($(UNAME), Windows)
2020
endif
2121

2222
# Mock 이 필요한 파일
23-
DART_MOCK_SRCS := $(shell find client -name "*_with_mocks.dart" -type f)
24-
DART_MOCK_TARGETS := $(DART_MOCK_SRCS:%_with_mocks.dart=%_with_mocks.mocks.dart)
23+
DART_MOCK_SRCS := $(shell find client -name "*_with_mocks_test.dart" -type f)
24+
DART_MOCK_TARGETS := $(DART_MOCK_SRCS:%_with_mocks_test.dart=%_with_mocks_test.mocks.dart)
2525

2626
BIN_INSTALL_DIR := $$HOME/.local/bin
2727
BUF_VERSION := 0.43.2

client/lib/widgets/components/custom_app_bar.dart

+3
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ class CustomAppBar extends AppBar {
2727
width: 5,
2828
),
2929
PopupMenuButton(
30+
key: const ValueKey("popup-menu-button"),
3031
itemBuilder: (BuildContext context) => <PopupMenuEntry<VertMenu>>[
3132
PopupMenuItem<VertMenu>(
33+
key: const ValueKey("icon-theme-toggle-button"),
3234
value: VertMenu.themeMode,
3335
child: ListTile(
3436
leading: Icon(context.read<CustomTheme>().icon),
3537
title: Text(context.read<CustomTheme>().text))),
3638
const PopupMenuDivider(height: 5),
3739
const PopupMenuItem<VertMenu>(
40+
key: ValueKey("popup-menu-item-issue-report"),
3841
value: VertMenu.issueReport,
3942
child: ListTile(
4043
leading: Icon(

client/test/screens/main_screen_test_with_mocks.dart

-96
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:mockito/annotations.dart';
4+
import 'package:mockito/mockito.dart';
5+
import 'package:pr12er/custom_theme.dart';
6+
import 'package:pr12er/protos/pkg/pr12er/messages.pb.dart';
7+
import 'package:pr12er/screens/main_screen.dart';
8+
import 'package:pr12er/service.dart';
9+
import 'package:pr12er/view_models/view_model_videos.dart';
10+
import 'package:provider/provider.dart';
11+
12+
import 'main_screen_with_mocks_test.mocks.dart';
13+
14+
@GenerateMocks([GrpcClient, CustomTheme, FavoriteVideoViewModel])
15+
void main() {
16+
group("MainScreen()", () {
17+
late List<Video> videos;
18+
19+
setUp(() {
20+
final video1 = Video()
21+
..title = "title1"
22+
..prId = 1
23+
..link = "video-link1";
24+
final video2 = Video()
25+
..title = "title2"
26+
..prId = 2
27+
..link = "video-link2";
28+
videos = List.of([video1, video2]);
29+
});
30+
31+
testWidgets('MainWidget has a load view', (WidgetTester tester) async {
32+
final devClient = MockGrpcClient();
33+
when(devClient.getVideos()).thenAnswer((_) => Future.value(videos));
34+
await tester.pumpWidget(MultiProvider(
35+
providers: [
36+
Provider<GrpcClient>(create: (context) => devClient),
37+
ChangeNotifierProvider(create: (context) => null),
38+
],
39+
builder: (context, child) => MainScreen(),
40+
));
41+
42+
final loadView = find.byType(CircularProgressIndicator);
43+
expect(loadView, findsOneWidget);
44+
});
45+
46+
testWidgets("has clickable list tile", (WidgetTester tester) async {
47+
final devClient = MockGrpcClient();
48+
when(devClient.getVideos()).thenAnswer((_) => Future.value(videos));
49+
50+
final mockVM = MockFavoriteVideoViewModel();
51+
when(mockVM.favoriteVideosMap).thenAnswer((_) => Future.value({}));
52+
when(mockVM.isFavoriteVideo(1)).thenAnswer((_) => Future.value(false));
53+
when(mockVM.isFavoriteVideo(2)).thenAnswer((_) => Future.value(false));
54+
await tester.pumpWidget(MultiProvider(
55+
providers: [
56+
Provider<GrpcClient>(create: (context) => devClient),
57+
ChangeNotifierProvider(create: (context) => null),
58+
ChangeNotifierProvider<FavoriteVideoViewModel>(
59+
create: (context) => mockVM),
60+
],
61+
builder: (context, child) => MaterialApp(home: MainScreen()),
62+
));
63+
await tester.pumpAndSettle();
64+
final firstTile = find.byKey(const ValueKey("ListTile-1"));
65+
expect(firstTile, findsOneWidget);
66+
});
67+
68+
testWidgets("Clicking the toggle dark/light mode changes the theme",
69+
(WidgetTester tester) async {
70+
final devClient = MockGrpcClient();
71+
when(devClient.getVideos()).thenAnswer((_) => Future.value(videos));
72+
73+
final mockTheme = MockCustomTheme();
74+
when(mockTheme.themeMode).thenReturn(ThemeMode.dark);
75+
when(mockTheme.icon).thenReturn(Icons.dark_mode);
76+
when(mockTheme.text).thenReturn("다크모드");
77+
78+
final mockVM = MockFavoriteVideoViewModel();
79+
when(mockVM.favoriteVideosMap).thenAnswer((_) => Future.value({}));
80+
when(mockVM.isFavoriteVideo(1)).thenAnswer((_) => Future.value(false));
81+
when(mockVM.isFavoriteVideo(2)).thenAnswer((_) => Future.value(false));
82+
83+
await tester.pumpWidget(MultiProvider(
84+
providers: [
85+
Provider<GrpcClient>(create: (context) => devClient),
86+
ChangeNotifierProvider<CustomTheme>(create: (context) => mockTheme),
87+
ChangeNotifierProvider<FavoriteVideoViewModel>(
88+
create: (context) => mockVM),
89+
],
90+
builder: (context, child) => MaterialApp(home: MainScreen()),
91+
));
92+
await tester.pumpAndSettle();
93+
94+
// GIVEN the theme toggle button.
95+
final popupMenuBtn = find.byKey(const ValueKey("popup-menu-button"));
96+
await tester.tap(popupMenuBtn);
97+
await tester.pumpAndSettle();
98+
99+
// WHEN the toggle button is clicked.
100+
final btn = find.byKey(const ValueKey("icon-theme-toggle-button"));
101+
await tester.tap(btn);
102+
103+
// THEN toggleMode function has been called.
104+
verify(mockTheme.toggleMode()).called(1);
105+
});
106+
});
107+
}

0 commit comments

Comments
 (0)