Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtanko committed Aug 27, 2023
1 parent f8bf10f commit 938e350
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/bloc/theme/theme_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:flutter_bloc_app_template/bloc/theme/app_theme.dart';
import 'package:flutter_bloc_app_template/index.dart';
import 'package:flutter_bloc_app_template/repository/theme_repository.dart';

final Map<AppTheme, ThemeData> _themeData = {
final Map<AppTheme, ThemeData> themeData = {
AppTheme.system: Style.light,
AppTheme.light: Style.light,
AppTheme.dark: Style.dark,
AppTheme.yellow: Style.yellow,
Expand Down Expand Up @@ -51,18 +52,18 @@ class ThemeCubit extends Cubit<AppTheme> {
ThemeData getDefaultTheme() {
switch (state) {
case AppTheme.light:
return _themeData[AppTheme.light] ?? Style.light;
return themeData[AppTheme.light] ?? Style.light;
case AppTheme.dark:
return _themeData[AppTheme.dark] ?? Style.dark;
return themeData[AppTheme.dark] ?? Style.dark;
case AppTheme.yellow:
return _themeData[AppTheme.yellow] ?? Style.light;
return themeData[AppTheme.yellow] ?? Style.light;
case AppTheme.system:
return _themeData[AppTheme.system] ?? Style.light;
return themeData[AppTheme.system] ?? Style.light;
case AppTheme.experimental:
return _themeData[AppTheme.experimental] ?? Style.light;
return themeData[AppTheme.experimental] ?? Style.light;
}
}

/// Default dark theme
ThemeData get darkTheme => _themeData[AppTheme.dark] ?? Style.dark;
ThemeData get darkTheme => themeData[AppTheme.dark] ?? Style.dark;
}
53 changes: 53 additions & 0 deletions test/bloc/theme/theme_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,59 @@ void main() {
cubit = ThemeCubit(themeRepository);
});

tearDown(() {
cubit.close();
});

test('Initial state is defaultTheme', () {
expect(cubit.state, ThemeCubit.defaultTheme);
});

test('loadTheme emits saved theme', () async {
await cubit.loadTheme();

expect(cubit.state, AppTheme.light);
});

test('themeMode returns appropriate ThemeMode', () {
cubit.updateTheme(AppTheme.light);
expect(cubit.themeMode, ThemeMode.light);
cubit.updateTheme(AppTheme.dark);
expect(cubit.themeMode, ThemeMode.dark);
cubit.updateTheme(AppTheme.system);
expect(cubit.themeMode, ThemeMode.system);
cubit.updateTheme(AppTheme.experimental);
expect(cubit.themeMode, ThemeMode.light);
});

test('getDefaultTheme returns correct ThemeData', () {
cubit.updateTheme(AppTheme.light);
expect(cubit.getDefaultTheme(), themeData[AppTheme.light]);

cubit.updateTheme(AppTheme.dark);
expect(cubit.getDefaultTheme(), themeData[AppTheme.dark]);

cubit.updateTheme(AppTheme.yellow);
expect(cubit.getDefaultTheme(), themeData[AppTheme.yellow]);

cubit.updateTheme(AppTheme.system);
expect(cubit.getDefaultTheme(), themeData[AppTheme.system]);

cubit.updateTheme(AppTheme.experimental);
expect(cubit.getDefaultTheme(), themeData[AppTheme.experimental]);
});

test('darkTheme returns correct ThemeData', () {
expect(cubit.darkTheme, themeData[AppTheme.dark]);
});

test('setTheme saves the theme and emits', () {
final newTheme = AppTheme.yellow;

cubit.setTheme = newTheme;
expect(cubit.state, newTheme);
});

test('has initial value', () async {
expect(cubit.state, AppTheme.system);
expect(cubit.theme, AppTheme.system);
Expand Down

0 comments on commit 938e350

Please sign in to comment.