Skip to content

Commit

Permalink
Merge branch 'test/journey' into fix#64/ajusta-refreshIndicator
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasm1nNasc1mento committed Feb 7, 2025
2 parents 50774d5 + f0c86dc commit 23859f6
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 0 deletions.
111 changes: 111 additions & 0 deletions test/ui/journey/view/journey_view_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:aranduapp/ui/journey/view/journey_view.dart';
import 'package:aranduapp/ui/journey/viewmodel/journey_viewmodel.dart';
import 'package:aranduapp/ui/subjects/model/subject_model.dart';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:aranduapp/ui/shared/erro_screen.dart';
import 'package:aranduapp/ui/shared/loading_widget.dart';
import 'package:aranduapp/ui/journey/model/journey_model.dart';
import 'journey_view_test.mocks.dart';

import '../../login/view/login_view_test.mocks.dart';

@GenerateNiceMocks([MockSpec<JourneyViewModel>(), MockSpec<Command1>()])
void main() {
late MockJourneyViewModel mockViewModel;
late MockCommand1<List<JourneyModel>, String> mockGetJourneyCommand;
final testSubject =
SubjectModel(id: '1', name: 'Matemática', shortName: '', description: '');

setUp(() async {
mockViewModel = MockJourneyViewModel();

mockGetJourneyCommand = MockCommand1();
when(mockViewModel.getJourneyCommand).thenReturn(mockGetJourneyCommand);
when(mockGetJourneyCommand.running).thenReturn(false);
when(mockGetJourneyCommand.isError).thenReturn(false);
when(mockGetJourneyCommand.isOk).thenReturn(false);

await GetIt.instance.reset();
GetIt.I.registerLazySingleton<JourneyViewModel>(() => mockViewModel);
});

Widget createJourneyScreen() {
return MaterialApp(
home: Journey(subject: testSubject),
);
}

testWidgets('Deve mostrar LoadingWidget inicialmente',
(WidgetTester tester) async {
when(mockGetJourneyCommand.running).thenReturn(true);

await tester.pumpWidget(createJourneyScreen());

expect(find.byType(LoadingWidget), findsOneWidget);
});

testWidgets('Deve mostrar lista de jornadas quando comando for bem sucedido',
(WidgetTester tester) async {
final testJourneys = [
JourneyModel(id: '1', title: 'Jornada 1', description: 'Descrição 1'),
JourneyModel(id: '2', title: 'Jornada 2', description: 'Descrição 2'),
];

when(mockGetJourneyCommand.isOk).thenReturn(true);
when(mockGetJourneyCommand.result).thenReturn(Result.value(testJourneys));

await tester.pumpWidget(createJourneyScreen());
await tester.pumpAndSettle();

expect(find.byType(ListView), findsOneWidget);
expect(find.text('Jornada 1'), findsOneWidget);
expect(find.text('Jornada 2'), findsOneWidget);
});

testWidgets('Deve mostrar erro quando comando falhar',
(WidgetTester tester) async {
when(mockGetJourneyCommand.isError).thenReturn(true);
when(mockGetJourneyCommand.result)
.thenReturn(Result.error('Erro de conexão'));

await tester.pumpWidget(createJourneyScreen());
await tester.pumpAndSettle();

expect(find.byType(ErrorScreen), findsOneWidget);

expect(find.text('Deslize para baixo\n\n Erro de conexão'), findsOneWidget);
expect(find.text('Algo deu errado...'), findsOneWidget);
});

testWidgets('Deve chamar o comando execute ao iniciar a tela',
(WidgetTester tester) async {
await tester.pumpWidget(createJourneyScreen());
verify(mockGetJourneyCommand.execute(testSubject.id)).called(1);
});

testWidgets(
'Deve chamar o comando execute novamente ao realizar o refresh na tela',
(WidgetTester tester) async {
when(mockGetJourneyCommand.isOk).thenReturn(true);
when(mockGetJourneyCommand.result).thenReturn(Result.value([
JourneyModel(id: '1', title: 'Jornada 1', description: 'Descrição 1'),
]));

await tester.pumpWidget(createJourneyScreen());
await tester.pumpAndSettle();

// Simulate a pull-to-refresh
await tester.fling(find.byType(ListView),const Offset(0.0, 300.0),1000.0,);
await tester.pumpAndSettle();

verify(mockGetJourneyCommand.execute(testSubject.id)).called(greaterThan(1));
});


}
149 changes: 149 additions & 0 deletions test/ui/journey/view/journey_view_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Mocks generated by Mockito 5.4.4 from annotations
// in aranduapp/test/ui/journey/view/journey_view_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i6;
import 'dart:ui' as _i7;

import 'package:aranduapp/core/state/command.dart' as _i2;
import 'package:aranduapp/ui/journey/model/journey_model.dart' as _i5;
import 'package:aranduapp/ui/journey/viewmodel/journey_viewmodel.dart' as _i4;
import 'package:async/async.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: deprecated_member_use
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

class _FakeCommand1_0<T, A> extends _i1.SmartFake
implements _i2.Command1<T, A> {
_FakeCommand1_0(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

class _FakeResult_1<T> extends _i1.SmartFake implements _i3.Result<T> {
_FakeResult_1(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

/// A class which mocks [JourneyViewModel].
///
/// See the documentation for Mockito's code generation for more information.
class MockJourneyViewModel extends _i1.Mock implements _i4.JourneyViewModel {
@override
_i2.Command1<List<_i5.JourneyModel>, String> get getJourneyCommand =>
(super.noSuchMethod(
Invocation.getter(#getJourneyCommand),
returnValue: _FakeCommand1_0<List<_i5.JourneyModel>, String>(
this,
Invocation.getter(#getJourneyCommand),
),
returnValueForMissingStub:
_FakeCommand1_0<List<_i5.JourneyModel>, String>(
this,
Invocation.getter(#getJourneyCommand),
),
) as _i2.Command1<List<_i5.JourneyModel>, String>);

@override
set getJourneyCommand(
_i2.Command1<List<_i5.JourneyModel>, String>? _getJourneyCommand) =>
super.noSuchMethod(
Invocation.setter(
#getJourneyCommand,
_getJourneyCommand,
),
returnValueForMissingStub: null,
);

@override
bool get hasListeners => (super.noSuchMethod(
Invocation.getter(#hasListeners),
returnValue: false,
returnValueForMissingStub: false,
) as bool);

@override
_i6.Future<_i3.Result<List<_i5.JourneyModel>>> getJourney(
String? subjectId) =>
(super.noSuchMethod(
Invocation.method(
#getJourney,
[subjectId],
),
returnValue: _i6.Future<_i3.Result<List<_i5.JourneyModel>>>.value(
_FakeResult_1<List<_i5.JourneyModel>>(
this,
Invocation.method(
#getJourney,
[subjectId],
),
)),
returnValueForMissingStub:
_i6.Future<_i3.Result<List<_i5.JourneyModel>>>.value(
_FakeResult_1<List<_i5.JourneyModel>>(
this,
Invocation.method(
#getJourney,
[subjectId],
),
)),
) as _i6.Future<_i3.Result<List<_i5.JourneyModel>>>);

@override
void addListener(_i7.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(
#addListener,
[listener],
),
returnValueForMissingStub: null,
);

@override
void removeListener(_i7.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(
#removeListener,
[listener],
),
returnValueForMissingStub: null,
);

@override
void dispose() => super.noSuchMethod(
Invocation.method(
#dispose,
[],
),
returnValueForMissingStub: null,
);

@override
void notifyListeners() => super.noSuchMethod(
Invocation.method(
#notifyListeners,
[],
),
returnValueForMissingStub: null,
);
}

0 comments on commit 23859f6

Please sign in to comment.