Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Substituir modularstate por state em ComposeTweetPage #398

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:flutter_svg/svg.dart';
import 'package:mobx/mobx.dart';

Expand All @@ -17,24 +16,26 @@ class ComposeTweetPage extends StatefulWidget {
Key? key,
this.title = 'Publicar',
this.showAppBar = false,
required this.composeTweetController,
}) : super(key: key);

final String title;
final bool showAppBar;
final ComposeTweetController composeTweetController;

@override
_ComposeTweetPageState createState() => _ComposeTweetPageState();
}

class _ComposeTweetPageState
extends ModularState<ComposeTweetPage, ComposeTweetController>
class _ComposeTweetPageState extends State<ComposeTweetPage>
RoxyRoses marked this conversation as resolved.
Show resolved Hide resolved
with SnackBarHandler {
final String inputHint = 'Deixe seu comentário';
final String anonymousHint =
'Sua publicação é anônima. As usuárias do app podem comentar sua publicação, mas só você pode iniciar uma conversa com elas.';
List<ReactionDisposer>? _disposers;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
PageProgressState _currentState = PageProgressState.initial;
ComposeTweetController get controller => widget.composeTweetController;

TextStyle get _kTextStyleNewTweetAnonymousHint => const TextStyle(
fontFamily: 'Lato',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class MainboardModule extends Module {
Modular.initialRoute,
child: (_, args) => MainboardPage(
controller: Modular.get<MainboardController>(),
composeTweetController: Modular.get<ComposeTweetController>(),
),
),
...tweetRoutes,
Expand All @@ -151,7 +152,10 @@ class MainboardModule extends Module {
),
ChildRoute(
'/compose',
child: (_, args) => const ComposeTweetPage(showAppBar: true),
child: (_, args) => ComposeTweetPage(
showAppBar: true,
composeTweetController: Modular.get<ComposeTweetController>(),
),
transition: TransitionType.rightToLeft,
),
ChildRoute(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';

import '../../../feed/presentation/compose_tweet/compose_tweet_controller.dart';
import '../../../main_menu/presentation/penhas_drawer_page.dart';
import 'mainboard_controller.dart';
import 'pages/mainboard_app_bar_page.dart';
Expand All @@ -11,9 +12,11 @@ class MainboardPage extends StatefulWidget {
const MainboardPage({
Key? key,
required this.controller,
required this.composeTweetController,
}) : super(key: key);

final MainboardController controller;
final ComposeTweetController composeTweetController;

static final mainBoardKey = GlobalKey<ScaffoldState>(
debugLabel: 'main-board-scaffold-key',
Expand All @@ -29,6 +32,8 @@ class MainboardPage extends StatefulWidget {
class MainboardPageState extends State<MainboardPage>
with WidgetsBindingObserver {
MainboardController get controller => widget.controller;
ComposeTweetController get composeTweetController =>
widget.composeTweetController;

@override
void initState() {
Expand Down Expand Up @@ -61,6 +66,7 @@ class MainboardPageState extends State<MainboardPage>
body: MainboardBodyPage(
pages: controller.mainboardStore.pages,
pageController: controller.mainboardStore.pageController,
composeTweetController: composeTweetController,
),
),
bottomNavigationBar: MainboardBottomNavigationPage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';

import '../../../../chat/presentation/chat_main_module.dart';
import '../../../../escape_manual/escape_manual_module.dart';
import '../../../../feed/presentation/compose_tweet/compose_tweet_controller.dart';
import '../../../../feed/presentation/compose_tweet/compose_tweet_page.dart';
import '../../../../feed/presentation/feed_module.dart';
import '../../../../help_center/presentation/help_center_module.dart';
Expand All @@ -13,10 +14,12 @@ class MainboardBodyPage extends StatelessWidget {
Key? key,
required this.pages,
required this.pageController,
required this.composeTweetController,
}) : super(key: key);

final List<MainboardState> pages;
final PageController pageController;
final ComposeTweetController composeTweetController;

@override
Widget build(BuildContext context) {
Expand All @@ -32,7 +35,9 @@ class MainboardBodyPage extends StatelessWidget {
chat: () => ChatMainModule(),
feed: () => FeedModule(),
escapeManual: () => EscapeManualModule(),
compose: () => const ComposeTweetPage(),
compose: () => ComposeTweetPage(
composeTweetController: composeTweetController,
),
supportPoint: () => SupportCenterModule(),
helpCenter: () => HelpCenterModule(),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import 'package:dartz/dartz.dart' show left, right;
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:flutter_modular_test/flutter_modular_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:penhas/app/app_module.dart';
import 'package:penhas/app/core/error/failures.dart';
import 'package:penhas/app/features/feed/domain/usecases/feed_use_cases.dart';
import 'package:penhas/app/features/feed/presentation/compose_tweet/compose_tweet_controller.dart';
import 'package:penhas/app/features/feed/presentation/compose_tweet/compose_tweet_navigator.dart';
import 'package:penhas/app/features/feed/presentation/compose_tweet/compose_tweet_page.dart';
import 'package:penhas/app/features/mainboard/presentation/mainboard/mainboard_module.dart';

import '../../../../../utils/golden_tests.dart';
import '../../../../../utils/widget_tester_ext.dart';

void main() {
late FeedUseCases mockFeedUseCases;
late ComposeTweetNavigator mockNavigator;

late ComposeTweetController composeTweetController;
group(ComposeTweetPage, () {
setUpAll(() {
registerFallbackValue(_FakeContext());
Expand All @@ -26,40 +24,34 @@ void main() {
setUp(() {
mockFeedUseCases = _MockFeedUseCases();
mockNavigator = _MockComposeTweetNavigator();
composeTweetController = ComposeTweetController(
navigator: mockNavigator, useCase: mockFeedUseCases);
});

initModules(
[
AppModule(),
MainboardModule(),
],
replaceBinds: [
Bind<FeedUseCases>((i) => mockFeedUseCases),
Bind<ComposeTweetNavigator>((i) => mockNavigator),
],
screenshotTest('should render ComposeTweetPage',
fileName: 'compose_tweet_page', pageBuilder: () {
return ComposeTweetPage(
composeTweetController: composeTweetController,
);
});

tearDown(() {
Modular.removeModule(AppModule());
Modular.removeModule(MainboardModule());
screenshotTest('should render ComposeTweetPage with app bar',
fileName: 'compose_tweet_page_with_app_bar', pageBuilder: () {
return ComposeTweetPage(
showAppBar: true,
composeTweetController: composeTweetController,
);
});

screenshotTest(
'should render ComposeTweetPage',
fileName: 'compose_tweet_page',
pageBuilder: () => ComposeTweetPage(),
);

screenshotTest(
'should render ComposeTweetPage with app bar',
fileName: 'compose_tweet_page_with_app_bar',
pageBuilder: () => ComposeTweetPage(showAppBar: true),
);

screenshotTest(
'when user types a tweet, should enable create button',
fileName: 'compose_tweet_page_with_text',
pageBuilder: () => ComposeTweetPage(showAppBar: true),
pageBuilder: () {
return ComposeTweetPage(
showAppBar: true,
composeTweetController: composeTweetController,
);
},
pumpBeforeTest: (tester) async {
await tester.enterTextAll(find.byType(TextField), 'Hello, world!');
await tester.pumpAndSettle();
Expand All @@ -69,7 +61,10 @@ void main() {
screenshotTest(
'when create tweet fails should show error message',
fileName: 'compose_tweet_page_with_error',
pageBuilder: () => ComposeTweetPage(showAppBar: true),
pageBuilder: () => ComposeTweetPage(
showAppBar: true,
composeTweetController: composeTweetController,
),
setUp: () {
when(() => mockFeedUseCases.create(any())).thenAnswer(
(_) async => left<Failure, FeedCache>(ServerFailure()),
Expand All @@ -90,7 +85,9 @@ void main() {
(_) async => right<Failure, FeedCache>(FeedCache(tweets: [])),
);

await tester.pumpWidget(buildTestableWidget(ComposeTweetPage()));
await tester.pumpWidget(buildTestableWidget(ComposeTweetPage(
composeTweetController: composeTweetController,
)));
await tester.pumpAndSettle();

// act
Expand All @@ -108,13 +105,16 @@ void main() {
'when tweet created successfully should navigate to mainboard',
(tester) async {
// arrange

when(() => mockFeedUseCases.create(any())).thenAnswer(
(_) async => right<Failure, FeedCache>(FeedCache(tweets: [])),
);
when(() => mockNavigator.navigateToFeed(any()))
.thenAnswer((_) => Future.value());

await tester.pumpWidget(buildTestableWidget(ComposeTweetPage()));
await tester.pumpWidget(buildTestableWidget(ComposeTweetPage(
composeTweetController: composeTweetController,
)));
await tester.pumpAndSettle();

// act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mobx/mobx.dart' as mobx;
import 'package:mocktail/mocktail.dart';
import 'package:penhas/app/features/appstate/domain/usecases/app_preferences_use_case.dart';
import 'package:penhas/app/features/feed/presentation/compose_tweet/compose_tweet_controller.dart';
import 'package:penhas/app/features/mainboard/domain/states/mainboard_state.dart';
import 'package:penhas/app/features/mainboard/domain/states/mainboard_store.dart';
import 'package:penhas/app/features/mainboard/presentation/mainboard/mainboard_controller.dart';
Expand All @@ -25,8 +26,12 @@ class MockNotificationRepository extends Mock

class FakeTimer extends Fake implements Timer {}

class MockComposeTweetController extends Mock
implements ComposeTweetController {}

void main() {
late MainboardController controller;
late ComposeTweetController composeTweetController;
late MainboardStore mainboardStore;
late InactivityLogoutUseCase inactivityLogoutUseCase;
late INotificationRepository notification;
Expand Down Expand Up @@ -56,13 +61,17 @@ void main() {
notification: notification,
notificationTimer: notificationTimer,
);
composeTweetController = MockComposeTweetController();
});

group(MainboardPage, () {
screenshotTest(
'mainboard page',
fileName: 'mainboard_page',
pageBuilder: () => MainboardPage(controller: controller),
pageBuilder: () => MainboardPage(
controller: controller,
composeTweetController: composeTweetController,
),
skip: true,
reason:
'possuiu vários elementos difíceis de testar. Vou revisar esta tela para melhorar a cobertura de testes',
Expand Down
Loading