Skip to content

Commit 5874633

Browse files
committed
test: add list user search integration tests
1 parent c65f563 commit 5874633

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

lib/configuration/environment.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class ProfileConstants {
1010
static void setEnvironment(Environment env) {
1111
switch (env) {
1212
case Environment.DEV:
13-
case Environment.TEST:
1413
_config = _Config.devConstants;
14+
case Environment.TEST:
15+
_config = _Config.testConstants;
1516
break;
1617
case Environment.PROD:
1718
_config = _Config.prodConstants;
@@ -38,7 +39,11 @@ class _Config {
3839
static const API = "API";
3940

4041
static Map<String, dynamic> devConstants = {
41-
API: "mock/",
42+
API: "mock",
43+
};
44+
45+
static Map<String, dynamic> testConstants = {
46+
API: "assets/mock",
4247
};
4348

4449
static Map<String, dynamic> prodConstants = {

lib/data/http_utils.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ class HttpUtils {
286286
try {
287287
String path = ProfileConstants.api;
288288
String fileName = "$httpMethod${endpoint.replaceAll("/", "_")}.json";
289-
String mockDataPath = path + fileName;
289+
String mockDataPath = "$path/$fileName";
290+
290291
responseBody = await rootBundle.loadString(mockDataPath);
291292
response = Future.value(http.Response(responseBody, httpStatusCode));
292293
debugPrint("Mock data loaded from $httpMethod $endpoint : $responseBody");

lib/presentation/screen/user/list/list_user_screen.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ListUserScreen extends StatelessWidget {
8282
Expanded(
8383
flex: 7,
8484
child: Text(
85-
state.userList[index].authorities.toString() == "[ROLE_ADMIN]" ? S.of(context).admin : S.of(context).guest,
85+
state.userList[index].authorities!.contains("ROLE_ADMIN")? S.of(context).admin : S.of(context).guest,
8686
textAlign: TextAlign.left),
8787
),
8888
SizedBox(width: 5),

test/presentation/screen/user/list_user_screen_test.dart

+50-22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:flutter_bloc_advance/presentation/common_blocs/district/district
1616
import 'package:flutter_bloc_advance/presentation/common_widgets/drawer/drawer_bloc/drawer.dart';
1717
import 'package:flutter_bloc_advance/presentation/screen/user/bloc/user.dart';
1818
import 'package:flutter_bloc_advance/presentation/screen/user/list/list_user_screen.dart';
19+
import 'package:flutter_bloc_advance/utils/storage.dart';
1920
import 'package:flutter_form_builder/flutter_form_builder.dart';
2021
import 'package:flutter_localizations/flutter_localizations.dart';
2122
import 'package:flutter_test/flutter_test.dart';
@@ -55,6 +56,10 @@ void main() {
5556
);
5657
}
5758

59+
void addToken() {
60+
saveStorage(jwtToken: "MOCK_TOKEN");
61+
}
62+
5863
testWidgets('renders ListUserScreen correctly', (tester) async {
5964
// Given: A ListUserScreen with mocked state is rendered
6065
await tester.pumpWidget(getWidget());
@@ -79,28 +84,51 @@ void main() {
7984
expect(find.text('Active'), findsOneWidget);
8085
});
8186

82-
// testWidgets('FormBuilderDropdown is displayed when state is AuthoritiesLoadSuccessState', (WidgetTester tester) async {
83-
// final mockAuthoritiesBloc = MockAuthoritiesBloc();
84-
//
85-
// whenListen(
86-
// mockAuthoritiesBloc,
87-
// Stream.fromIterable([AuthoritiesLoadSuccessState(roleList: ['ROLE_USER', 'ROLE_ADMIN'])]),
88-
// initialState: AuthoritiesInitial(),
89-
// );
90-
//
91-
// await tester.pumpWidget(
92-
// MaterialApp(
93-
// home: BlocProvider<AuthoritiesBloc>(
94-
// create: (context) => mockAuthoritiesBloc,
95-
// child: ListUserScreen(),
96-
// ),
97-
// ),
98-
// );
99-
//
100-
// await tester.pumpAndSettle();
101-
//
102-
// expect(find.byType(FormBuilderDropdown), findsOneWidget);
103-
// });
87+
testWidgets('displays user list when UserSearchSuccessState is emitted with JWTToken', (tester) async {
88+
// Given: A mock UserSearchSuccessState with a list of users
89+
await tester.pumpWidget(getWidget());
90+
91+
addToken();
92+
93+
// When: ListUserScreen is shown and UserBloc emits the state
94+
// final userSearchEvent = UserSearch(0, 100, "-", "");
95+
// BlocProvider.of<UserBloc>(tester.element(find.byType(ListUserScreen))).add(userSearchEvent);
96+
await tester.tap(find.text('List'));
97+
await tester.pumpAndSettle();
98+
99+
// Then: Verify that the list is displayed with correct data
100+
expect(find.text('Admin'), findsAtLeastNWidgets(1));
101+
expect(find.text('admin'), findsAtLeastNWidgets(1));
102+
expect(find.text('User'), findsAtLeastNWidgets(1));
103+
expect(find.text('[email protected]'), findsAtLeastNWidgets(1));
104+
expect(find.text('active'), findsAtLeastNWidgets(1));
105+
expect(find.byType(IconButton), findsAtLeastNWidgets(1));
106+
});
107+
104108

109+
testWidgets('displays user list when UserSearchSuccessState is emitted without token and fail', (tester) async {
110+
// Given: A mock UserSearchSuccessState with a list of users
111+
112+
await tester.pumpWidget(getWidget());
113+
final block = BlocProvider.of<UserBloc>(tester.element(find.byType(ListUserScreen)));
114+
// When: ListUserScreen is shown and UserBloc emits the state
115+
// final userSearchEvent = UserSearch(0, 100, "-", "");
116+
// BlocProvider.of<UserBloc>(tester.element(find.byType(ListUserScreen))).add(userSearchEvent);
117+
await tester.tap(find.text('List'));
118+
await tester.pumpAndSettle();
119+
120+
// Then handle the Unauthorized error with UserSearchFailureState
121+
expect(block.state, isA<UserSearchFailureState>());
122+
123+
// Then: Verify that the list is not displayed
124+
expect(find.text('Admin'), findsNothing);
125+
expect(find.text('admin'), findsNothing);
126+
expect(find.text('User'), findsNothing);
127+
expect(find.text('[email protected]'), findsNothing);
128+
expect(find.text('active'), findsNothing);
129+
expect(find.byType(IconButton), findsNothing);
130+
131+
132+
});
105133

106134
}

0 commit comments

Comments
 (0)