|
| 1 | +import 'package:appflowy/features/settings/settings.dart'; |
| 2 | +import 'package:appflowy_result/appflowy_result.dart'; |
| 3 | +import 'package:bloc_test/bloc_test.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:mocktail/mocktail.dart'; |
| 6 | + |
| 7 | +class MockSettingsRepository extends Mock implements SettingsRepository {} |
| 8 | + |
| 9 | +void main() { |
| 10 | + late MockSettingsRepository repository; |
| 11 | + late DataLocationBloc bloc; |
| 12 | + |
| 13 | + const defaultPath = '/default/path'; |
| 14 | + const customPath = '/custom/path'; |
| 15 | + |
| 16 | + setUp(() { |
| 17 | + repository = MockSettingsRepository(); |
| 18 | + when(() => repository.getUserDataLocation()).thenAnswer( |
| 19 | + (_) async => FlowyResult.success( |
| 20 | + UserDataLocation(path: defaultPath, isCustom: false), |
| 21 | + ), |
| 22 | + ); |
| 23 | + bloc = DataLocationBloc(repository: repository) |
| 24 | + ..add(DataLocationEvent.initial()); |
| 25 | + }); |
| 26 | + |
| 27 | + tearDown(() async { |
| 28 | + await bloc.close(); |
| 29 | + }); |
| 30 | + |
| 31 | + blocTest<DataLocationBloc, DataLocationState>( |
| 32 | + 'emits updated state when resetting to default', |
| 33 | + build: () => bloc, |
| 34 | + setUp: () { |
| 35 | + when(() => repository.resetUserDataLocation()).thenAnswer( |
| 36 | + (_) async => FlowyResult.success( |
| 37 | + UserDataLocation(path: defaultPath, isCustom: false), |
| 38 | + ), |
| 39 | + ); |
| 40 | + }, |
| 41 | + act: (bloc) => bloc.add(DataLocationEvent.resetToDefault()), |
| 42 | + wait: const Duration(milliseconds: 100), |
| 43 | + expect: () => [ |
| 44 | + DataLocationState( |
| 45 | + userDataLocation: UserDataLocation(path: defaultPath, isCustom: false), |
| 46 | + didResetToDefault: true, |
| 47 | + ), |
| 48 | + ], |
| 49 | + ); |
| 50 | + |
| 51 | + blocTest<DataLocationBloc, DataLocationState>( |
| 52 | + 'emits updated state when setting custom path', |
| 53 | + build: () => bloc, |
| 54 | + setUp: () { |
| 55 | + when(() => repository.setCustomLocation(customPath)).thenAnswer( |
| 56 | + (_) async => FlowyResult.success( |
| 57 | + UserDataLocation(path: customPath, isCustom: true), |
| 58 | + ), |
| 59 | + ); |
| 60 | + }, |
| 61 | + act: (bloc) => bloc.add(DataLocationEvent.setCustomPath(customPath)), |
| 62 | + wait: const Duration(milliseconds: 100), |
| 63 | + expect: () => [ |
| 64 | + DataLocationState( |
| 65 | + userDataLocation: UserDataLocation(path: customPath, isCustom: true), |
| 66 | + didResetToDefault: false, |
| 67 | + ), |
| 68 | + ], |
| 69 | + ); |
| 70 | + |
| 71 | + blocTest<DataLocationBloc, DataLocationState>( |
| 72 | + 'emits state with cleared reset flag', |
| 73 | + build: () => bloc, |
| 74 | + seed: () => DataLocationState( |
| 75 | + userDataLocation: UserDataLocation(path: defaultPath, isCustom: false), |
| 76 | + didResetToDefault: true, |
| 77 | + ), |
| 78 | + act: (bloc) => bloc.add(DataLocationEvent.clearState()), |
| 79 | + wait: const Duration(milliseconds: 100), |
| 80 | + expect: () => [ |
| 81 | + DataLocationState( |
| 82 | + userDataLocation: UserDataLocation( |
| 83 | + path: defaultPath, |
| 84 | + isCustom: false, |
| 85 | + ), |
| 86 | + didResetToDefault: false, |
| 87 | + ), |
| 88 | + ], |
| 89 | + ); |
| 90 | +} |
0 commit comments