From bdfbc279173b4b9ec8eef7c062e57b441f251793 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 4 Jun 2021 17:09:41 +0530 Subject: [PATCH] add stream_chat_error_test.dart Signed-off-by: Sahil Kumar --- .../lib/src/core/error/stream_chat_error.dart | 3 + .../http/interceptor/logging_interceptor.dart | 1 + .../core/error/stream_chat_error_test.dart | 126 ++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 packages/stream_chat/test/src/core/error/stream_chat_error_test.dart diff --git a/packages/stream_chat/lib/src/core/error/stream_chat_error.dart b/packages/stream_chat/lib/src/core/error/stream_chat_error.dart index 04214033..a78e9390 100644 --- a/packages/stream_chat/lib/src/core/error/stream_chat_error.dart +++ b/packages/stream_chat/lib/src/core/error/stream_chat_error.dart @@ -45,6 +45,9 @@ class StreamWebSocketError extends StreamChatError { /// Response body. please refer to [ErrorResponse]. final ErrorResponse? data; + @override + List get props => [...super.props, code]; + @override String toString() { var params = 'message: $message'; diff --git a/packages/stream_chat/lib/src/core/http/interceptor/logging_interceptor.dart b/packages/stream_chat/lib/src/core/http/interceptor/logging_interceptor.dart index 41c36326..1f5687a8 100644 --- a/packages/stream_chat/lib/src/core/http/interceptor/logging_interceptor.dart +++ b/packages/stream_chat/lib/src/core/http/interceptor/logging_interceptor.dart @@ -1,4 +1,5 @@ // ignore_for_file: lines_longer_than_80_chars +// coverage:ignore-file import 'dart:math' as math; diff --git a/packages/stream_chat/test/src/core/error/stream_chat_error_test.dart b/packages/stream_chat/test/src/core/error/stream_chat_error_test.dart new file mode 100644 index 00000000..a32e0d71 --- /dev/null +++ b/packages/stream_chat/test/src/core/error/stream_chat_error_test.dart @@ -0,0 +1,126 @@ +import 'package:dio/dio.dart'; +import 'package:stream_chat/src/core/api/responses.dart'; +import 'package:stream_chat/src/core/error/error.dart'; +import 'package:test/test.dart'; + +void main() { + group('StreamChatError', () { + test('should match if message is same', () { + const message = 'test-error-message'; + const error = StreamChatError(message); + const error2 = StreamChatError(message); + + expect(error, error2); + }); + + test('`.toString`', () { + const message = 'test-error-message'; + const error = StreamChatError(message); + + expect(error.toString(), 'StreamChatError(message: $message)'); + }); + }); + + group('StreamWebSocketError', () { + test('.fromStreamError', () { + final data = ErrorResponse()..code = 333; + final error = StreamWebSocketError.fromStreamError(data.toJson()); + expect(error, isNotNull); + expect(error.code, data.code); + }); + + test('should match if message and data.code is same', () { + const message = 'test-error-message'; + final data = ErrorResponse()..code = 333; + final error = StreamWebSocketError(message, data: data); + final error2 = StreamWebSocketError(message, data: data); + + expect(error, error2); + }); + + test('`.toString`', () { + const message = 'test-error-message'; + final data = ErrorResponse()..code = 333; + final error = StreamWebSocketError(message, data: data); + + expect( + error.toString(), + 'WebSocketError(message: $message, data: $data)', + ); + }); + }); + + group('StreamChatNetworkError', () { + test('.raw', () { + const code = 333; + const message = 'test-error-message'; + final error = StreamChatNetworkError.raw(code: code, message: message); + expect(error, isNotNull); + expect(error.code, code); + expect(error.message, message); + }); + + test('.fromDioError', () { + const code = 333; + const statusCode = 666; + const message = 'test-error-message'; + final options = RequestOptions(path: 'test-path'); + final data = ErrorResponse() + ..code = code + ..statusCode = statusCode + ..message = message; + final dioError = DioError( + requestOptions: options, + response: Response( + requestOptions: options, + statusCode: data.statusCode, + data: data.toJson(), + ), + ); + final error = StreamChatNetworkError.fromDioError(dioError); + expect(error, isNotNull); + expect(error.code, code); + expect(error.message, message); + expect(error.statusCode, statusCode); + expect(error.data?.code, data.code); + expect(error.data?.statusCode, data.statusCode); + expect(error.data?.message, data.message); + }); + + test('should match if message, code and statusCode is same', () { + const code = 333; + const statusCode = 666; + const message = 'test-error-message'; + final error = StreamChatNetworkError.raw( + code: code, + statusCode: statusCode, + message: message, + ); + final error2 = StreamChatNetworkError.raw( + code: code, + statusCode: statusCode, + message: message, + ); + + expect(error, error2); + }); + + test('`.retriable` should return true if data is not present', () { + const errorCode = ChatErrorCode.tokenExpired; + final error = StreamChatNetworkError(errorCode); + + expect(error.isRetriable, isTrue); + }); + + test('`.toString`', () { + const errorCode = ChatErrorCode.tokenExpired; + final error = StreamChatNetworkError(errorCode); + expect( + error.toString(), + 'StreamChatNetworkError(' + 'code: ${errorCode.code}, ' + 'message: ${errorCode.message})', + ); + }); + }); +}