Skip to content

Commit

Permalink
test: fix test of LanguageBloc
Browse files Browse the repository at this point in the history
  • Loading branch information
esmaeil-ahmadipour committed Jan 16, 2025
1 parent d80518f commit 81da98e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ part 'language_event.dart';
part 'language_state.dart';

class LanguageBloc extends Bloc<LanguageEvent, LanguageState> {
LanguageBloc() : super(LanguageState()) {
LanguageBloc() : super(LanguageInitial()) {
on<ChangeLanguage>(onChangeLanguage);
}
Future<void> onChangeLanguage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,91 @@
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:gui/src/features/main/language/core/language_constants.dart';
import 'package:gui/src/features/main/language/data/language_model.dart';
import 'package:gui/src/features/main/language/presentation/bloc/language_bloc.dart';

void main() {
late LanguageBloc languageBloc;

// Set up the LanguageBloc before each test
setUp(() {
languageBloc = LanguageBloc();
});

// Close the LanguageBloc after each test
tearDown(() {
languageBloc.close();
});
test('initial state is LanguageState with default language', () {
// Verify the initial state is LanguageState, not LanguageInitial directly
expect(languageBloc.state, isA<LanguageState>());

// Verify that the initial language is English (Language.english)
// Helper method to build the widget tree with BlocProvider
Widget buildTestableWidget(Widget child) {
return BlocProvider.value(
value: languageBloc,
child: MaterialApp(
home: Scaffold(body: child),
),
);
}

// Test: Ensure the initial language is English (enUS)
testWidgets('Initial language state is set to English (enUS)',
(tester) async {
// Arrange: Set up the initial language state
await tester.pumpWidget(buildTestableWidget(Container()));

// Act: No actions needed here, just pumping the widget

// Assert: The initial language state should be English (enUS)
expect(languageBloc.state.selectedLanguage, LanguageConstants.enUS);
});

blocTest<LanguageBloc, LanguageState>(
'emits changeLanguage to es',
build: () => languageBloc,
act: (bloc) {
// Change language to Spanish
bloc.add(ChangeLanguage(selectedLanguage: LanguageConstants.esES));
},
expect: () => [
// Expect the new state with the updated language (Spanish)
isA<LanguageState>().having(
(state) => state.selectedLanguage,
'selectedLanguage',
Language.spanish, // Expect the selected language to be Spanish
),
],
);

blocTest<LanguageBloc, LanguageState>(
'emits changeLanguage to Fr',
build: () => languageBloc,
act: (bloc) {
// Change language to Spanish
bloc.add(ChangeLanguage(selectedLanguage: LanguageConstants.frFR));
},
expect: () => [
// Expect the new state with the updated language (Spanish)
isA<LanguageState>().having(
(state) => state.selectedLanguage,
'selectedLanguage',
Language.french, // Expect the selected language to be Spanish
),
],
);
// Test: Ensure language changes to French (frFR) when the ChangeLanguage
// event is triggered
testWidgets('Change language to French (frFR) when triggered',
(tester) async {
// Arrange: Set up the ChangeLanguage event to switch to French
final frenchLanguageEvent =
ChangeLanguage(selectedLanguage: LanguageConstants.frFR);
await tester.pumpWidget(buildTestableWidget(Container()));

// Act: Add the ChangeLanguage event to switch to French
languageBloc.add(frenchLanguageEvent);
await tester.pump();

// Assert: Verify the language is now French (frFR)
expect(languageBloc.state.selectedLanguage, LanguageConstants.frFR);
});

// Test: Ensure language switches back to English (enUS) when triggered
testWidgets('Switch back to English (enUS) when triggered again',
(tester) async {
// Arrange: Change the language to French first
languageBloc.add(ChangeLanguage(selectedLanguage: LanguageConstants.frFR));
await tester.pumpWidget(buildTestableWidget(Container()));
await tester.pump(); // Wait for state change

// Act: Now change back to English
languageBloc.add(ChangeLanguage(selectedLanguage: LanguageConstants.enUS));
await tester.pump();

// Assert: The language should now be English (enUS)
expect(languageBloc.state.selectedLanguage, LanguageConstants.enUS);
});

// Test: Ensure language changes to Spanish (esES) when the ChangeLanguage
// event is triggered
testWidgets('Change language to Spanish (esES) when triggered',
(tester) async {
// Arrange: Set up the ChangeLanguage event to switch to Spanish
final spanishLanguageEvent =
ChangeLanguage(selectedLanguage: LanguageConstants.esES);
await tester.pumpWidget(buildTestableWidget(Container()));

// Act: Add the ChangeLanguage event to switch to Spanish
languageBloc.add(spanishLanguageEvent);
await tester.pump();

// Assert: Verify the language is now Spanish (esES)
expect(languageBloc.state.selectedLanguage, LanguageConstants.esES);
});
}

0 comments on commit 81da98e

Please sign in to comment.