Skip to content

Commit

Permalink
chore(api): Fix failing test on web
Browse files Browse the repository at this point in the history
  • Loading branch information
Dillon Nys committed Sep 1, 2023
1 parent 31bbbdf commit de810a2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class WebSocketBloc with AWSDebuggable, AmplifyLoggerMixin {
onError: (Object error, StackTrace st) {
final exception = NetworkException(
'Exception from WebSocketService.',
underlyingException: error.toString(),
underlyingException: error,
);
_shutdownWithException(exception, st);
},
Expand Down Expand Up @@ -405,10 +405,7 @@ class WebSocketBloc with AWSDebuggable, AmplifyLoggerMixin {
}

currentState.service
.register(
currentState,
request,
)
.register(currentState, request)
.onError<Object>(subscriptionBloc.addResponseError);
}

Expand Down
14 changes: 8 additions & 6 deletions packages/api/amplify_api_dart/test/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,17 @@ class MockWebSocketService extends AmplifyWebSocketService {

@override
Stream<WebSocketEvent> init(WebSocketState state) {
if (badInit) {
return Stream.error(
WebSocketChannelException('Mock Web Socket Exception'),
);
}
channel = MockWebSocketChannel();

sink = channel.sink;

if (badInit) {
Timer.run(() {
channel.controller.addError(
WebSocketChannelException('Mock Web Socket Exception'),
);
});
}

return transformStream(channel.stream);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,47 +336,31 @@ void main() {
bloc = null;
service = null; // service gets closed in bloc
});
test(
'triggering FailureState on Exception during init',
() async {
final subscribeEvent = SubscribeEvent(
subscriptionRequest,
() {
service!.channel.sink.add(mockDataString);
},
);

final badService = MockWebSocketService(badInit: true);
mockNetworkStreamController = StreamController<ConnectivityStatus>();
final bloc = WebSocketBloc(
config: testApiKeyConfig,
authProviderRepo: getTestAuthProviderRepo(),
wsService: badService,
subscriptionOptions: subscriptionOptions,
pollClientOverride: mockPollClient.client,
connectivity: const MockConnectivity(),
);

expect(
bloc.stream,
emitsInOrder(
[
isA<DisconnectedState>(),
isA<ConnectingState>(),
isA<FailureState>(),
isA<PendingDisconnect>(),
isA<DisconnectedState>(),
],
),
);
test('triggering FailureState on Exception during init', () async {
final badService = MockWebSocketService(badInit: true);
mockNetworkStreamController = StreamController<ConnectivityStatus>();
final bloc = WebSocketBloc(
config: testApiKeyConfig,
authProviderRepo: getTestAuthProviderRepo(),
wsService: badService,
subscriptionOptions: subscriptionOptions,
pollClientOverride: mockPollClient.client,
connectivity: const MockConnectivity(),
);

bloc.subscribe(
subscribeEvent,
);
// TODO(equartey): Fix this test on web
},
skip: zIsWeb,
);
expect(
bloc.stream,
emitsInOrder(
[
isA<DisconnectedState>(),
isA<ConnectingState>(),
isA<FailureState>(),
isA<PendingDisconnect>(),
isA<DisconnectedState>(),
],
),
);
});

test('Exception from service and should return error to user', () async {
final subscribeEvent = SubscribeEvent(
Expand Down

0 comments on commit de810a2

Please sign in to comment.