Skip to content

Commit 9d71da4

Browse files
committed
implement unit testing for all blocs
1 parent 40e764e commit 9d71da4

19 files changed

+1117
-727
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:bloc_test/bloc_test.dart';
2+
import 'package:flood_mobile/Blocs/api_bloc/api_bloc.dart';
3+
import 'package:flood_mobile/Blocs/api_bloc/api_bloc_event.dart';
4+
import 'package:flood_mobile/Blocs/api_bloc/api_bloc_state.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
void main() {
8+
TestWidgetsFlutterBinding.ensureInitialized(); // Initialize Flutter bindings
9+
10+
group('ApiBloc', () {
11+
late ApiBloc apiBloc;
12+
13+
setUp(() {
14+
apiBloc = ApiBloc();
15+
});
16+
17+
tearDown(() {
18+
apiBloc.close();
19+
});
20+
21+
blocTest<ApiBloc, ApiState>(
22+
'emits ApiState with updated URL after SetBaseUrlEvent',
23+
build: () => apiBloc,
24+
act: (bloc) => bloc.add(SetBaseUrlEvent(url: 'http://example.com')),
25+
expect: () => [
26+
ApiState(baseUrl: 'http://example.com'),
27+
],
28+
);
29+
});
30+
}

test/unit_test/api_provider_unit_test.dart

Lines changed: 0 additions & 27 deletions
This file was deleted.

test/unit_test/client_provider_unit_test.dart renamed to test/unit_test/client_bloc_unit_test.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import 'package:flood_mobile/Model/client_settings_model.dart';
2-
import 'package:flood_mobile/Provider/client_provider.dart';
2+
import 'package:flood_mobile/Blocs/client_settings_bloc/client_settings_bloc.dart';
33
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:bloc_test/bloc_test.dart';
45

56
void main() {
6-
late ClientSettingsProvider sut;
7+
late ClientSettingsBloc sut;
78
late ClientSettingsModel newClientSettings;
89

910
setUp(() {
10-
sut = ClientSettingsProvider();
11+
sut = ClientSettingsBloc();
1112
newClientSettings = ClientSettingsModel(
1213
dht: false,
1314
dhtPort: 1234,
@@ -35,11 +36,14 @@ void main() {
3536
);
3637
});
3738

38-
test(
39-
"initial values are correct",
40-
() {
41-
sut.setClientSettings(newClientSettings);
42-
expect(sut.clientSettings, newClientSettings);
43-
},
39+
tearDown(() => sut.close());
40+
41+
blocTest<ClientSettingsBloc, ClientSettingsState>(
42+
'initial values are correct',
43+
build: () => sut,
44+
act: (bloc) =>
45+
bloc.add(SetClientSettingsEvent(clientSettings: newClientSettings)),
46+
expect: () =>
47+
[ClientSettingsUpdatedState(clientSettings: newClientSettings)],
4448
);
4549
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:bloc_test/bloc_test.dart';
2+
import 'package:flood_mobile/Blocs/onboarding_main_page_bloc/on_boarding_page_color_bloc.dart';
3+
import 'package:flood_mobile/Pages/onboarding_main_screen/data/onboard_page_data.dart';
4+
import 'package:flutter/material.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
void main() {
8+
group('ColorBloc', () {
9+
late OnBoardingPageColorBloc colorBloc;
10+
11+
setUp(() {
12+
colorBloc = OnBoardingPageColorBloc();
13+
});
14+
15+
test(
16+
'Initial color should match the first accent color from the onboard data',
17+
() {
18+
expect(colorBloc.color, equals(onboardData[0].accentColor));
19+
});
20+
21+
blocTest<OnBoardingPageColorBloc, OnBoardingPageColorState>(
22+
'Setting a new color should update the color state',
23+
build: () => colorBloc,
24+
act: (bloc) => bloc..add(SetColorEvent(color: Colors.blue)),
25+
expect: () => [
26+
ColorUpdated(color: Colors.blue),
27+
],
28+
);
29+
});
30+
}

test/unit_test/color_provider_unit_test.dart

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/unit_test/date_converter_unit_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:flood_mobile/Services/date_converter.dart';
1+
import 'package:flood_mobile/Pages/torrent_screen/services/date_converter.dart';
22
import 'package:flutter_test/flutter_test.dart';
33

44
void main() {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import 'package:bloc_test/bloc_test.dart';
2+
import 'package:flood_mobile/Blocs/filter_torrent_bloc/filter_torrent_bloc.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group('FilterTorrentBloc', () {
7+
late FilterTorrentBloc filterTorrentBloc;
8+
9+
setUp(() {
10+
filterTorrentBloc = FilterTorrentBloc();
11+
});
12+
13+
tearDown(() {
14+
filterTorrentBloc.close();
15+
});
16+
17+
test('initial state should be correct', () {
18+
expect(filterTorrentBloc.state, FilterTorrentState());
19+
});
20+
21+
blocTest<FilterTorrentBloc, FilterTorrentState>(
22+
'emits FilterTorrentState with updated filterStatus when SetFilterSelectedEvent is added',
23+
build: () => FilterTorrentBloc(),
24+
act: (bloc) => bloc
25+
.add(SetFilterSelectedEvent(filterStatus: FilterValue.downloading)),
26+
expect: () => [
27+
FilterTorrentState(
28+
filterStatus: FilterValue.downloading,
29+
statusList: [],
30+
mapStatus: {},
31+
tagSelected: '',
32+
mapTags: {'Untagged': 0},
33+
trackerURISelected: '',
34+
trackersSizeList: [],
35+
tagsSizeList: [],
36+
maptrackerURIs: {},
37+
),
38+
],
39+
);
40+
blocTest<FilterTorrentBloc, FilterTorrentState>(
41+
'emits FilterTorrentState with updated statusList when StatusListChanged event is added',
42+
build: () => filterTorrentBloc,
43+
act: (bloc) =>
44+
bloc.add(SetStatusListEvent(statusList: ['status1', 'status2'])),
45+
expect: () => [
46+
FilterTorrentState(statusList: ['status1', 'status2'])
47+
],
48+
);
49+
50+
blocTest<FilterTorrentBloc, FilterTorrentState>(
51+
'emits FilterTorrentState with updated tagSelected when TagSelectedChanged event is added',
52+
build: () => filterTorrentBloc,
53+
act: (bloc) => bloc.add(SetTagSelectedEvent(tagSelected: 'tag1')),
54+
expect: () => [FilterTorrentState(tagSelected: 'tag1')],
55+
);
56+
57+
blocTest<FilterTorrentBloc, FilterTorrentState>(
58+
'emits FilterTorrentState with updated trackerURISelected when TrackerURISelectedChanged event is added',
59+
build: () => filterTorrentBloc,
60+
act: (bloc) =>
61+
bloc.add(SetTrackerURISelectedEvent(trackerURISelected: 'tracker1')),
62+
expect: () => [FilterTorrentState(trackerURISelected: 'tracker1')],
63+
);
64+
65+
blocTest<FilterTorrentBloc, FilterTorrentState>(
66+
'emits FilterTorrentState with updated mapStatus when MapStatusChanged event is added',
67+
build: () => filterTorrentBloc,
68+
act: (bloc) =>
69+
bloc.add(SetMapStatusEvent(mapStatus: {'status': 'value'})),
70+
expect: () => [
71+
FilterTorrentState(mapStatus: {'status': 'value'})
72+
],
73+
);
74+
75+
blocTest<FilterTorrentBloc, FilterTorrentState>(
76+
'emits FilterTorrentState with updated mapTags when MapTagsChanged event is added',
77+
build: () => filterTorrentBloc,
78+
act: (bloc) => bloc.add(SetMapTagsEvent(mapTags: {'tag': 'value'})),
79+
expect: () => [
80+
FilterTorrentState(mapTags: {'tag': 'value'})
81+
],
82+
);
83+
84+
blocTest<FilterTorrentBloc, FilterTorrentState>(
85+
'emits FilterTorrentState with updated trackersSizeList when TrackersSizeListChanged event is added',
86+
build: () => filterTorrentBloc,
87+
act: (bloc) =>
88+
bloc.add(SetTrackersSizeListEvent(trackersSizeList: [1, 2, 3])),
89+
expect: () => [
90+
FilterTorrentState(trackersSizeList: [1, 2, 3])
91+
],
92+
);
93+
94+
blocTest<FilterTorrentBloc, FilterTorrentState>(
95+
'emits FilterTorrentState with updated tagsSizeList when TagsSizeListChanged event is added',
96+
build: () => filterTorrentBloc,
97+
act: (bloc) => bloc.add(SetTagsSizeListEvent(tagsSizeList: [4, 5, 6])),
98+
expect: () => [
99+
FilterTorrentState(tagsSizeList: [4, 5, 6])
100+
],
101+
);
102+
103+
blocTest<FilterTorrentBloc, FilterTorrentState>(
104+
'emits FilterTorrentState with updated maptrackerURIs when MaptrackerURIsChanged event is added',
105+
build: () => filterTorrentBloc,
106+
act: (bloc) =>
107+
bloc.add(SetMapTrackerURIsEvent(maptrackerURIs: {'uri': 'value'})),
108+
expect: () => [
109+
FilterTorrentState(maptrackerURIs: {'uri': 'value'})
110+
],
111+
);
112+
});
113+
}

test/unit_test/filter_provider_unit_test.dart

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)