Skip to content

Commit 61e32af

Browse files
committed
chore: add bloc test
1 parent f213f6c commit 61e32af

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

frontend/appflowy_flutter/lib/features/settings/logic/data_location_state.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import 'package:equatable/equatable.dart';
2+
13
import '../data/models/user_data_location.dart';
24

3-
class DataLocationState {
5+
class DataLocationState extends Equatable {
46
const DataLocationState({
57
required this.userDataLocation,
68
required this.didResetToDefault,
@@ -12,6 +14,9 @@ class DataLocationState {
1214
final UserDataLocation? userDataLocation;
1315
final bool didResetToDefault;
1416

17+
@override
18+
List<Object?> get props => [userDataLocation, didResetToDefault];
19+
1520
DataLocationState copyWith({
1621
UserDataLocation? userDataLocation,
1722
bool? didResetToDefault,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)