more tests added for Query
prefetchQuery support added seperate methods for adding & removing listeners for events of both query & mutations refetchInterval not working when used Query.fromOptions bug fixed
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:fl_query/src/models/query_job.dart';
|
||||
import 'package:fl_query/src/query.dart';
|
||||
import 'package:fl_query/src/query_bowl.dart';
|
||||
import 'package:fl_query/src/utils.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'query_test.mocks.dart';
|
||||
|
||||
@GenerateMocks(
|
||||
[QueryBowl],
|
||||
[QueryBowl, Connectivity],
|
||||
customMocks: [
|
||||
MockSpec<QueryJob<Object, void>>(
|
||||
as: #MockQueryJobVoidObject,
|
||||
@@ -129,7 +132,7 @@ void main() {
|
||||
"onData listeners are called When new data is fetched and set",
|
||||
() async {
|
||||
int count = 0;
|
||||
query.onDataListeners.add((_) {
|
||||
query.addDataListener((_) {
|
||||
count++;
|
||||
});
|
||||
await query.fetch();
|
||||
@@ -138,20 +141,111 @@ void main() {
|
||||
);
|
||||
test(
|
||||
"onError listeners are called When any error occurs",
|
||||
() {},
|
||||
() async {
|
||||
reset(queryJob);
|
||||
when(queryJob.queryKey).thenReturn("test");
|
||||
when(queryJob.task).thenAnswer(
|
||||
(_) => (_, __) => Future.error("Error"),
|
||||
);
|
||||
query = Query.fromOptions(
|
||||
queryJob,
|
||||
externalData: null,
|
||||
queryBowl: queryBowl,
|
||||
);
|
||||
int count = 0;
|
||||
query.addErrorListener((_) {
|
||||
count++;
|
||||
});
|
||||
await query.fetch();
|
||||
expect(count, 1);
|
||||
},
|
||||
);
|
||||
test("query should become stale after defined amount time", () {});
|
||||
test("query should become stale after defined amount time", () async {
|
||||
when(queryJob.staleTime).thenReturn(Duration(milliseconds: 500));
|
||||
query = Query.fromOptions(
|
||||
queryJob,
|
||||
externalData: null,
|
||||
queryBowl: queryBowl,
|
||||
);
|
||||
expect(query.isStale, isFalse);
|
||||
await Future.delayed(Duration(milliseconds: 300));
|
||||
expect(query.isStale, isFalse);
|
||||
await Future.delayed(Duration(milliseconds: 600));
|
||||
expect(query.isStale, isTrue);
|
||||
});
|
||||
test(
|
||||
"query should refetch in interval When refetchInterval is specified",
|
||||
() {},
|
||||
() async {
|
||||
reset(queryJob);
|
||||
when(queryJob.queryKey).thenReturn("test");
|
||||
when(queryJob.task).thenAnswer(
|
||||
(_) => (_, __) => Future.value(Random().nextInt(100)),
|
||||
);
|
||||
when(queryJob.staleTime).thenReturn(
|
||||
Duration(milliseconds: 1),
|
||||
);
|
||||
when(queryJob.refetchInterval).thenReturn(
|
||||
Duration(milliseconds: 200),
|
||||
);
|
||||
query = Query.fromOptions(
|
||||
queryJob,
|
||||
externalData: null,
|
||||
queryBowl: queryBowl,
|
||||
);
|
||||
|
||||
final data = await query.fetch();
|
||||
await Future.delayed(Duration(milliseconds: 400));
|
||||
expect(data, isNot(equals(await query.fetch())));
|
||||
},
|
||||
);
|
||||
test(
|
||||
"query should revalidate When a new caller gets mounted",
|
||||
() {},
|
||||
() async {
|
||||
reset(queryJob);
|
||||
when(queryJob.queryKey).thenReturn("test");
|
||||
when(queryJob.task).thenAnswer(
|
||||
(_) => (_, __) => Future.value(Random().nextInt(150)),
|
||||
);
|
||||
when(queryJob.refetchOnMount).thenReturn(true);
|
||||
query = Query.fromOptions(
|
||||
queryJob,
|
||||
externalData: null,
|
||||
queryBowl: queryBowl,
|
||||
);
|
||||
final data = await query.fetch();
|
||||
query.invalidate();
|
||||
query.mount(ValueKey<String>(uuid.v4()));
|
||||
await Future.delayed(Duration(milliseconds: 100));
|
||||
expect(data, isNot(equals(await query.fetch())));
|
||||
expect(query.refetchCount, 1);
|
||||
},
|
||||
);
|
||||
test(
|
||||
"query should not revalidate When there's no Internet Connectivity and a new caller gets mounted",
|
||||
() {},
|
||||
() async {
|
||||
final connectivityMock = MockConnectivity();
|
||||
when(connectivityMock.checkConnectivity()).thenAnswer(
|
||||
(_) async => ConnectivityResult.none,
|
||||
);
|
||||
reset(queryJob);
|
||||
when(queryJob.queryKey).thenReturn("test");
|
||||
when(queryJob.task).thenAnswer(
|
||||
(_) => (_, __) => Future.value(Random().nextInt(150)),
|
||||
);
|
||||
when(queryJob.connectivity).thenReturn(connectivityMock);
|
||||
when(queryJob.refetchOnMount).thenReturn(true);
|
||||
query = Query.fromOptions(
|
||||
queryJob,
|
||||
externalData: null,
|
||||
queryBowl: queryBowl,
|
||||
);
|
||||
final data = await query.fetch();
|
||||
query.invalidate();
|
||||
query.mount(ValueKey<String>(uuid.v4()));
|
||||
await Future.delayed(Duration(milliseconds: 100));
|
||||
expect(data, equals(await query.fetch()));
|
||||
expect(query.refetchCount, 0);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
|
||||
import 'dart:async' as _i7;
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart' as _i10;
|
||||
import 'package:fl_query/src/models/mutation_job.dart' as _i9;
|
||||
import 'package:fl_query/src/models/query_job.dart' as _i8;
|
||||
import 'package:fl_query/src/mutation.dart' as _i4;
|
||||
import 'package:fl_query/src/query.dart' as _i3;
|
||||
import 'package:fl_query/src/query_bowl.dart' as _i6;
|
||||
import 'package:flutter/foundation.dart' as _i5;
|
||||
import 'package:flutter/rendering.dart' as _i10;
|
||||
import 'package:flutter/widgets.dart' as _i2;
|
||||
import 'package:mockito/mockito.dart' as _i1;
|
||||
|
||||
@@ -136,9 +136,9 @@ class MockQueryBowl extends _i1.Mock implements _i6.QueryBowl {
|
||||
@override
|
||||
_i4.Mutation<T, V> addMutation<T extends Object, V>(
|
||||
_i9.MutationJob<T, V>? mutationJob,
|
||||
{_i4.MutationListener<T>? onData,
|
||||
_i4.MutationListener<dynamic>? onError,
|
||||
_i4.MutationListener<V>? onMutate,
|
||||
{_i4.MutationListener<T, V>? onData,
|
||||
_i4.MutationListener<dynamic, V>? onError,
|
||||
_i4.MutationListenerReturnable<V, dynamic>? onMutate,
|
||||
_i2.ValueKey<String>? key}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#addMutation, [
|
||||
@@ -190,7 +190,7 @@ class MockQueryBowl extends _i1.Mock implements _i6.QueryBowl {
|
||||
.noSuchMethod(Invocation.method(#toStringShort, []), returnValue: '')
|
||||
as String);
|
||||
@override
|
||||
void debugFillProperties(_i10.DiagnosticPropertiesBuilder? properties) =>
|
||||
void debugFillProperties(_i5.DiagnosticPropertiesBuilder? properties) =>
|
||||
super.noSuchMethod(Invocation.method(#debugFillProperties, [properties]),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
@@ -229,6 +229,27 @@ class MockQueryBowl extends _i1.Mock implements _i6.QueryBowl {
|
||||
super.toString();
|
||||
}
|
||||
|
||||
/// A class which mocks [Connectivity].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockConnectivity extends _i1.Mock implements _i10.Connectivity {
|
||||
MockConnectivity() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i7.Stream<_i10.ConnectivityResult> get onConnectivityChanged =>
|
||||
(super.noSuchMethod(Invocation.getter(#onConnectivityChanged),
|
||||
returnValue: Stream<_i10.ConnectivityResult>.empty())
|
||||
as _i7.Stream<_i10.ConnectivityResult>);
|
||||
@override
|
||||
_i7.Future<_i10.ConnectivityResult> checkConnectivity() =>
|
||||
(super.noSuchMethod(Invocation.method(#checkConnectivity, []),
|
||||
returnValue: Future<_i10.ConnectivityResult>.value(
|
||||
_i10.ConnectivityResult.bluetooth))
|
||||
as _i7.Future<_i10.ConnectivityResult>);
|
||||
}
|
||||
|
||||
/// A class which mocks [QueryJob].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
@@ -271,6 +292,10 @@ class MockQueryJobVoidObject extends _i1.Mock
|
||||
super.noSuchMethod(Invocation.setter(#refetchInterval, _refetchInterval),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
set connectivity(_i10.Connectivity? _connectivity) =>
|
||||
super.noSuchMethod(Invocation.setter(#connectivity, _connectivity),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
String get queryKey =>
|
||||
(super.noSuchMethod(Invocation.getter(#queryKey), returnValue: '')
|
||||
as String);
|
||||
|
||||
Reference in New Issue
Block a user