Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
test: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
omartinma committed Nov 23, 2023
1 parent bcd32c9 commit e7849e4
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 18 deletions.
10 changes: 5 additions & 5 deletions lib/home/bloc/home_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
HomeBloc(this._questionsRepository) : super(const HomeState()) {
on<FromWelcomeToQuestion>(_onFromWelcomeToQuestion);
on<AskQuestion>(_onQuestion);
on<QuestionAsked>(_questionAsked);
on<QueryUpdated>(_queryUpdated);
on<QuestionAsked>(_questionAsked);
}

final QuestionsRepository _questionsRepository;
Expand All @@ -32,6 +32,10 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
emit(state.copyWith(status: Status.askQuestion));
}

FutureOr<void> _queryUpdated(QueryUpdated event, Emitter<HomeState> emit) {
emit(state.copyWith(query: event.query));
}

Future<FutureOr<void>> _questionAsked(
QuestionAsked event,
Emitter<HomeState> emit,
Expand All @@ -45,8 +49,4 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
),
);
}

FutureOr<void> _queryUpdated(QueryUpdated event, Emitter<HomeState> emit) {
emit(state.copyWith(query: event.query));
}
}
14 changes: 7 additions & 7 deletions lib/home/bloc/home_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ class AskQuestion extends HomeEvent {
const AskQuestion();
}

class QuestionAsked extends HomeEvent {
const QuestionAsked();

@override
List<Object> get props => [];
}

class QueryUpdated extends HomeEvent {
const QueryUpdated({required this.query});

final String query;
@override
List<Object> get props => [query];
}

class QuestionAsked extends HomeEvent {
const QuestionAsked();

@override
List<Object> get props => [];
}
5 changes: 2 additions & 3 deletions lib/home/bloc/home_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enum Status {
welcomeToAskQuestion,
askQuestion,
askQuestionToThinking,
thinking,
thinkingToResults,
results,
}
Expand All @@ -24,10 +25,8 @@ class HomeState extends Equatable {
status == Status.welcome || status == Status.welcomeToAskQuestion;
bool get isQuestionVisible =>
status == Status.welcomeToAskQuestion || status == Status.askQuestion;

bool get isThinkingVisible =>
status == Status.askQuestionToThinking ||
status == Status.thinkingToResults;
status == Status.askQuestionToThinking || status == Status.thinking;
bool get isResultsVisible =>
status == Status.thinkingToResults || status == Status.results;

Expand Down
32 changes: 32 additions & 0 deletions test/home/bloc/home_bloc_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:api_client/api_client.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:dash_ai_search/home/home.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -47,5 +48,36 @@ void main() {
],
);
});

group('QueryUpdated', () {
blocTest<HomeBloc, HomeState>(
'emits query updated',
build: buildBloc,
act: (bloc) => bloc.add(QueryUpdated(query: 'new query')),
expect: () => [
HomeState(query: 'new query'),
],
);
});

group('QuestionAsked', () {
blocTest<HomeBloc, HomeState>(
'emits [Status.askQuestionToThinking, Status.thinkingToResults] '
'with vertex response from _questionsRepository.getVertexResponse',
setUp: () {
when(() => questionsRepository.getVertexResponse(any()))
.thenAnswer((_) async => VertexResponse.empty());
},
build: buildBloc,
act: (bloc) => bloc.add(QuestionAsked()),
expect: () => [
HomeState(status: Status.askQuestionToThinking),
HomeState(
status: Status.thinkingToResults,
vertexResponse: VertexResponse.empty(),
),
],
);
});
});
}
14 changes: 14 additions & 0 deletions test/home/bloc/home_event_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,19 @@ void main() {
equals(AskQuestion()),
);
});

test('QueryUpdated supports value equality', () {
expect(
QueryUpdated(query: 'query'),
equals(QueryUpdated(query: 'query')),
);
});

test('QuestionAsked supports value equality', () {
expect(
QuestionAsked(),
equals(QuestionAsked()),
);
});
});
}
53 changes: 50 additions & 3 deletions test/home/view/home_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,64 @@ void main() {
when(() => homeBloc.state).thenReturn(HomeState());
});

testWidgets('renders correctly', (tester) async {
testWidgets('renders WelcomeView if isWelcomeVisible', (tester) async {
when(() => homeBloc.state).thenReturn(HomeState());
await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: HomeView(),
),
);

expect(find.byType(Background), findsOneWidget);
expect(find.byType(Logo), findsOneWidget);
expect(find.byType(WelcomeView), findsOneWidget);
});

testWidgets('renders QuestionView if isQuestionVisible', (tester) async {
when(() => homeBloc.state).thenReturn(
HomeState(
status: Status.askQuestion,
),
);
await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: HomeView(),
),
);

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

testWidgets('renders ThinkingView if isThinkingVisible', (tester) async {
when(() => homeBloc.state).thenReturn(
HomeState(
status: Status.thinking,
),
);
await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: HomeView(),
),
);

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

testWidgets('renders ResultsView if isResultsVisible', (tester) async {
when(() => homeBloc.state).thenReturn(
HomeState(
status: Status.results,
),
);
await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: HomeView(),
),
);

expect(find.byType(ResultsView), findsOneWidget);
});
});
}
29 changes: 29 additions & 0 deletions test/home/widgets/question_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,34 @@ void main() {
expect(find.text(l10n.questionScreenTitle), findsOneWidget);
expect(find.byType(QuestionInputTextField), findsOneWidget);
});

testWidgets(
'calls QueryUpdated writing on the TextField',
(WidgetTester tester) async {
await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: Material(child: QuestionView()),
),
);
const newText = 'text';
await tester.enterText(find.byType(TextField), newText);
verify(() => homeBloc.add(QueryUpdated(query: newText))).called(1);
},
);

testWidgets(
'calls QuestionAsked clicking on the CTAButton ',
(WidgetTester tester) async {
await tester.pumpApp(
BlocProvider.value(
value: homeBloc,
child: Material(child: QuestionView()),
),
);
await tester.tap(find.byType(CTAButton));
verify(() => homeBloc.add(QuestionAsked())).called(1);
},
);
});
}

0 comments on commit e7849e4

Please sign in to comment.