From 1c97055cd505b0a9708b35360b9eb879fadef7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Irland?= Date: Mon, 29 Aug 2022 19:45:08 -0300 Subject: [PATCH] Add store response tests (#14) --- test/stock_response_extension_test.dart | 61 +++++++++++++++++ test/stock_response_test.dart | 88 +++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 test/stock_response_test.dart diff --git a/test/stock_response_extension_test.dart b/test/stock_response_extension_test.dart index aa9e60f..5294835 100644 --- a/test/stock_response_extension_test.dart +++ b/test/stock_response_extension_test.dart @@ -1,4 +1,5 @@ import 'package:stock/src/errors.dart'; +import 'package:stock/src/extensions/stock_response_internal_extensions.dart'; import 'package:stock/src/stock_response.dart'; import 'package:stock/src/stock_response_extensions.dart'; import 'package:test/test.dart'; @@ -29,6 +30,21 @@ void main() { ); }); }); + group('throwIfError extension', () { + test('throwIfError of an error throws the exception', () async { + final customEx = Exception("Custom ex"); + expect( + StockResponse.error(ResponseOrigin.fetcher, customEx).throwIfError, + throwsA((e) => e == customEx), + ); + }); + test('throwIfError of a loading response does not do anything', () async { + const StockResponse.loading(ResponseOrigin.fetcher).throwIfError(); + }); + test('throwIfError of a data returns the data', () async { + const StockResponse.data(ResponseOrigin.fetcher, 1).throwIfError(); + }); + }); group('Get data extensions', () { test('getData of an error returns null', () async { final customEx = Exception("Custom ex"); @@ -65,6 +81,20 @@ void main() { equals(true), ); }); + test('Data returns true if it is a data event', () async { + expect( + StockResponse.error(ResponseOrigin.fetcher, Error()).isData, + equals(false), + ); + expect( + const StockResponse.data(ResponseOrigin.fetcher, 1).isData, + equals(true), + ); + expect( + const StockResponse.loading(ResponseOrigin.fetcher).isData, + equals(false), + ); + }); test('Error returns true if the response is an error', () async { expect( StockResponse.error(ResponseOrigin.fetcher, Error()).isError, @@ -80,4 +110,35 @@ void main() { ); }); }); + + group('Unknown type', () { + test("Require data throws error if the type is not recognized", () async { + expect( + _FakeType().requireData, + throwsA( + (e) => + e is StockError && + e.toString() == + 'StockError: Type error requireData expect either Success, Error but was given _FakeType', + ), + ); + }); + + test("Swap type throws error if the type is not recognized", () async { + expect( + _FakeType().swapType, + throwsA( + (e) => + e is StockError && + e.toString() == + 'StockError: Type error swapType expect either Success, Error or Loading but was given _FakeType', + ), + ); + }); + }); +} + +class _FakeType implements StockResponse { + @override + ResponseOrigin get origin => ResponseOrigin.fetcher; } diff --git a/test/stock_response_test.dart b/test/stock_response_test.dart new file mode 100644 index 0000000..59aad71 --- /dev/null +++ b/test/stock_response_test.dart @@ -0,0 +1,88 @@ +import 'package:stock/src/stock_response.dart'; +import 'package:test/test.dart'; + +void main() { + group('StockResponseLoading', () { + test('toString()', () async { + expect( + const StockResponseLoading(ResponseOrigin.fetcher).toString(), + equals( + 'StockResponse.loading(origin: ResponseOrigin.fetcher)', + ), + ); + }); + test('equal', () async { + final fetcher1 = const StockResponseLoading(ResponseOrigin.fetcher); + final fetcher2 = const StockResponseLoading(ResponseOrigin.fetcher); + final sot1 = const StockResponseLoading( + ResponseOrigin.sourceOfTruth, + ); + expect(fetcher1 == sot1, equals(false)); + expect(fetcher1 == fetcher2, equals(true)); + }); + test('hashcode', () async { + final fetcher1 = const StockResponseLoading(ResponseOrigin.fetcher); + final fetcher2 = const StockResponseLoading(ResponseOrigin.fetcher); + final sot1 = const StockResponseLoading(ResponseOrigin.sourceOfTruth); + expect(fetcher1.hashCode == sot1.hashCode, equals(false)); + expect(fetcher1.hashCode == fetcher2.hashCode, equals(true)); + }); + group('StockResponseData', () { + test('toString()', () async { + expect( + const StockResponseData(ResponseOrigin.fetcher, '1').toString(), + equals( + 'StockResponse.data(origin: ResponseOrigin.fetcher, value: 1)', + ), + ); + }); + test('equal', () async { + final fetcher1 = const StockResponseData(ResponseOrigin.fetcher, 1); + final fetcher1Copy = const StockResponseData(ResponseOrigin.fetcher, 1); + final fetcher2 = const StockResponseData(ResponseOrigin.fetcher, 2); + final sot1 = const StockResponseData(ResponseOrigin.sourceOfTruth, 1); + expect(fetcher1 == sot1, equals(false)); + expect(fetcher1 == fetcher1Copy, equals(true)); + expect(fetcher1 == fetcher2, equals(false)); + }); + test('hashcode', () async { + final fetcher1 = const StockResponseData(ResponseOrigin.fetcher, 1); + final fetcher1Copy = const StockResponseData(ResponseOrigin.fetcher, 1); + final fetcher2 = const StockResponseData(ResponseOrigin.fetcher, 2); + final sot1 = const StockResponseData(ResponseOrigin.sourceOfTruth, 1); + expect(fetcher1.hashCode == sot1.hashCode, equals(false)); + expect(fetcher1.hashCode == fetcher1Copy.hashCode, equals(true)); + expect(fetcher1.hashCode == fetcher2.hashCode, equals(false)); + }); + }); + group('StockResponseError', () { + test('toString()', () async { + expect( + const StockResponseError(ResponseOrigin.fetcher, 'error') + .toString(), + equals( + 'StockResponse.error(origin: ResponseOrigin.fetcher, error: error, stackTrace: null)', + ), + ); + }); + test('equal', () async { + final fetcher1 = const StockResponseError(ResponseOrigin.fetcher, 1); + final fetcher1Tmp = const StockResponseError(ResponseOrigin.fetcher, 1); + final fetcher2 = const StockResponseError(ResponseOrigin.fetcher, 2); + final sot1 = const StockResponseError(ResponseOrigin.sourceOfTruth, 1); + expect(fetcher1 == sot1, equals(false)); + expect(fetcher1 == fetcher1Tmp, equals(true)); + expect(fetcher1 == fetcher2, equals(false)); + }); + test('hashcode', () async { + final fetcher1 = const StockResponseError(ResponseOrigin.fetcher, 1); + final fetcher1Tmp = const StockResponseError(ResponseOrigin.fetcher, 1); + final fetcher2 = const StockResponseError(ResponseOrigin.fetcher, 2); + final sot1 = const StockResponseError(ResponseOrigin.sourceOfTruth, 1); + expect(fetcher1.hashCode == sot1.hashCode, equals(false)); + expect(fetcher1.hashCode == fetcher1Tmp.hashCode, equals(true)); + expect(fetcher1.hashCode == fetcher2.hashCode, equals(false)); + }); + }); + }); +}