@@ -16,6 +16,7 @@ import 'package:flutter_bloc_advance/presentation/common_blocs/district/district
16
16
import 'package:flutter_bloc_advance/presentation/common_widgets/drawer/drawer_bloc/drawer.dart' ;
17
17
import 'package:flutter_bloc_advance/presentation/screen/user/bloc/user.dart' ;
18
18
import 'package:flutter_bloc_advance/presentation/screen/user/list/list_user_screen.dart' ;
19
+ import 'package:flutter_bloc_advance/utils/storage.dart' ;
19
20
import 'package:flutter_form_builder/flutter_form_builder.dart' ;
20
21
import 'package:flutter_localizations/flutter_localizations.dart' ;
21
22
import 'package:flutter_test/flutter_test.dart' ;
@@ -55,6 +56,10 @@ void main() {
55
56
);
56
57
}
57
58
59
+ void addToken () {
60
+ saveStorage (jwtToken: "MOCK_TOKEN" );
61
+ }
62
+
58
63
testWidgets ('renders ListUserScreen correctly' , (tester) async {
59
64
// Given: A ListUserScreen with mocked state is rendered
60
65
await tester.pumpWidget (getWidget ());
@@ -79,28 +84,51 @@ void main() {
79
84
expect (find.text ('Active' ), findsOneWidget);
80
85
});
81
86
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
+
104
108
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
+ });
105
133
106
134
}
0 commit comments