Skip to content

Commit

Permalink
Add store response tests (#14)
Browse files Browse the repository at this point in the history
mirland authored Aug 29, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 337c579 commit 1c97055
Showing 2 changed files with 149 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/stock_response_extension_test.dart
Original file line number Diff line number Diff line change
@@ -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<int> {
@override
ResponseOrigin get origin => ResponseOrigin.fetcher;
}
88 changes: 88 additions & 0 deletions test/stock_response_test.dart
Original file line number Diff line number Diff line change
@@ -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<dynamic>.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<String>.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<String>(ResponseOrigin.fetcher, 'error')
.toString(),
equals(
'StockResponse<String>.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));
});
});
});
}

0 comments on commit 1c97055

Please sign in to comment.