|
1 | | -// import 'dart:async'; |
| 1 | +import 'package:bloc_test/bloc_test.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:socket_probe/features/event_sockets/bloc/event_sockets_bloc.dart'; |
| 4 | +import 'package:socket_probe/features/event_sockets/bloc/event_sockets_event.dart'; |
| 5 | +import 'package:socket_probe/features/event_sockets/bloc/event_sockets_state.dart'; |
2 | 6 |
|
3 | | -// import 'package:flutter_test/flutter_test.dart'; |
4 | | -// import 'package:bloc_test/bloc_test.dart'; |
5 | | -// import 'package:mocktail/mocktail.dart'; |
6 | | -// import 'package:socket_probe/features/event_sockets/bloc/event_sockets_bloc.dart'; |
7 | | -// import 'package:socket_probe/features/event_sockets/bloc/event_sockets_event.dart'; |
8 | | -// import 'package:socket_probe/features/event_sockets/bloc/event_sockets_state.dart'; |
9 | | -// import 'package:socket_probe/features/event_sockets/data/repository/event_sockets_repo_impl.dart'; |
| 7 | +void main() { |
| 8 | + group(EventSocketsBloc, () { |
| 9 | + late EventSocketsBloc eventSocketsBloc; |
10 | 10 |
|
11 | | -// // Mock repository class |
12 | | -// class MockEventSocketsRepo extends Mock implements EventSocketsRepoImpl {} |
| 11 | + setUp(() { |
| 12 | + eventSocketsBloc = EventSocketsBloc(); |
| 13 | + }); |
13 | 14 |
|
14 | | -// void main() { |
15 | | -// late EventSocketsBloc eventSocketsBloc; |
16 | | -// late MockEventSocketsRepo mockRepository; |
17 | | -// late StreamController<dynamic> messageController; |
| 15 | + blocTest( |
| 16 | + 'This test the websocket connection when ConnectedRequest is added', |
| 17 | + build: () => eventSocketsBloc, |
| 18 | + act: (bloc) => bloc.add( |
| 19 | + ConnectRequested(socketUrl: 'wss://echo.websocket.events', sockedConfigurations: { |
| 20 | + "auth": {"token": "userToken"}, |
| 21 | + "transports": ["websocket"], |
| 22 | + "autoConnect": true, |
| 23 | + }), |
| 24 | + ), |
| 25 | + expect: () => [ |
| 26 | + isA<EventSocketsConnecting>(), |
| 27 | + isA<EventSocketsConnected>(), |
| 28 | + ], |
| 29 | + ); |
18 | 30 |
|
19 | | -// setUp(() { |
20 | | -// mockRepository = MockEventSocketsRepo(); |
21 | | -// messageController = StreamController<dynamic>.broadcast(); |
| 31 | + blocTest( |
| 32 | + 'This test the websocket connection when DisconnectRequested is added', |
| 33 | + build: () => eventSocketsBloc, |
| 34 | + act: (bloc) => bloc.add(DisconnectRequested()), |
| 35 | + expect: () => [isA<EventSocketsDisconnected>()], |
| 36 | + ); |
22 | 37 |
|
23 | | -// when(() => mockRepository.messages).thenAnswer((_) => messageController.stream); |
24 | | -// when(() => mockRepository.connectToSocket()).thenAnswer((_) { |
25 | | -// messageController.add('Connected'); // Simulating successful connection |
26 | | -// }); |
27 | | -// when(() => mockRepository.disconnect()).thenReturn(null); |
28 | | -// when(() => mockRepository.sendEventMessage(any(), any())).thenReturn(null); |
| 38 | + blocTest( |
| 39 | + 'This test the websocket connection when SendMessageRequested is added', |
| 40 | + build: () => eventSocketsBloc, |
| 41 | + act: (bloc) => bloc.add( |
| 42 | + SendMessageRequested(message: "Send Message Requested", event: "message.event"), |
| 43 | + ), |
| 44 | + expect: () => [isA<EventSocketsConnected>()], |
| 45 | + ); |
29 | 46 |
|
30 | | -// eventSocketsBloc = EventSocketsBloc(); |
31 | | -// // eventSocketsBloc._repository = mockRepository; |
32 | | -// }); |
| 47 | + blocTest( |
| 48 | + 'This test the websocket connection when MessageReceived is added', |
| 49 | + build: () => eventSocketsBloc, |
| 50 | + act: (bloc) => bloc.add( |
| 51 | + MessageReceived(message: "Message Received"), |
| 52 | + ), |
| 53 | + expect: () => [isA<EventSocketsConnected>()], |
| 54 | + ); |
33 | 55 |
|
34 | | -// tearDown(() { |
35 | | -// eventSocketsBloc.close(); |
36 | | -// messageController.close(); |
37 | | -// }); |
| 56 | + blocTest( |
| 57 | + 'This test the websocket connection when ConnectionErrorOccurred is added', |
| 58 | + build: () => eventSocketsBloc, |
| 59 | + act: (bloc) => bloc.add( |
| 60 | + ConnectionErrorOccurred(message: "Connection Error Occurred"), |
| 61 | + ), |
| 62 | + expect: () => [isA<EventSocketsError>()], |
| 63 | + ); |
38 | 64 |
|
39 | | -// test('initial state should be EventSocketsInitial', () { |
40 | | -// expect(eventSocketsBloc.state, equals(EventSocketsInitial())); |
41 | | -// }); |
| 65 | + tearDown(() => eventSocketsBloc.close()); |
42 | 66 |
|
43 | | -// blocTest<EventSocketsBloc, EventSocketsState>( |
44 | | -// 'emits [EventSocketsConnecting, EventSocketsConnected] when ConnectRequested is added', |
45 | | -// build: () => eventSocketsBloc, |
46 | | -// act: (bloc) => bloc.add(ConnectRequested(socketUrl: 'ws://test-url', sockedConfigurations: {})), |
47 | | -// expect: () => [ |
48 | | -// EventSocketsConnecting(), |
49 | | -// EventSocketsConnected(messages: []), |
50 | | -// ], |
51 | | -// verify: (_) { |
52 | | -// verify(() => mockRepository.connectToSocket()).called(1); |
53 | | -// }, |
54 | | -// ); |
55 | | - |
56 | | -// blocTest<EventSocketsBloc, EventSocketsState>( |
57 | | -// 'emits [EventSocketsDisconnected] when DisconnectRequested is added', |
58 | | -// build: () => eventSocketsBloc, |
59 | | -// act: (bloc) => bloc.add(DisconnectRequested()), |
60 | | -// expect: () => [ |
61 | | -// EventSocketsDisconnected(), |
62 | | -// ], |
63 | | -// verify: (_) { |
64 | | -// verify(() => mockRepository.disconnect()).called(1); |
65 | | -// }, |
66 | | -// ); |
67 | | - |
68 | | -// blocTest<EventSocketsBloc, EventSocketsState>( |
69 | | -// 'emits [EventSocketsConnected] when a message is received', |
70 | | -// build: () => eventSocketsBloc, |
71 | | -// act: (bloc) { |
72 | | -// messageController.add('New Event Message'); |
73 | | -// bloc.add(MessageReceived(message: 'New Event Message')); |
74 | | -// }, |
75 | | -// expect: () => [ |
76 | | -// EventSocketsConnected(messages: ['Received: New Event Message']), |
77 | | -// ], |
78 | | -// ); |
79 | | - |
80 | | -// blocTest<EventSocketsBloc, EventSocketsState>( |
81 | | -// 'emits [EventSocketsConnected] when SendMessageRequested is added', |
82 | | -// build: () => eventSocketsBloc, |
83 | | -// act: (bloc) => bloc.add(SendMessageRequested(event: 'testEvent', message: 'testMessage')), |
84 | | -// expect: () => [ |
85 | | -// EventSocketsConnected(messages: ['Sent: testMessage']), |
86 | | -// ], |
87 | | -// verify: (_) { |
88 | | -// verify(() => mockRepository.sendEventMessage('testEvent', 'testMessage')).called(1); |
89 | | -// }, |
90 | | -// ); |
91 | | - |
92 | | -// blocTest<EventSocketsBloc, EventSocketsState>( |
93 | | -// 'emits [EventSocketsError] when a connection error occurs', |
94 | | -// build: () => eventSocketsBloc, |
95 | | -// act: (bloc) { |
96 | | -// messageController.addError('Socket connection failed'); |
97 | | -// bloc.add(ConnectionErrorOccurred(message: 'Socket connection failed')); |
98 | | -// }, |
99 | | -// expect: () => [ |
100 | | -// EventSocketsError(error: 'Socket connection failed'), |
101 | | -// ], |
102 | | -// ); |
103 | | -// } |
| 67 | + // end of test |
| 68 | + }); |
| 69 | +} |
0 commit comments