1+ import 'dart:async' ;
2+
13import 'package:brick_offline_first_with_graphql/src/graphql_offline_queue_link.dart' ;
24import 'package:brick_offline_first_with_graphql/src/graphql_offline_request_queue.dart' ;
35import 'package:brick_offline_first_with_graphql/src/graphql_request_sqlite_cache_manager.dart' ;
6+ import 'package:gql_exec/gql_exec.dart' ;
7+ import 'package:mockito/mockito.dart' ;
48import 'package:sqflite_common_ffi/sqflite_ffi.dart' ;
59import 'package:test/test.dart' ;
610
11+ import '__helpers__.dart' ;
12+
713void main () {
814 final offlineClient = GraphqlOfflineQueueLink (
915 GraphqlRequestSqliteCacheManager ('db' , databaseFactory: databaseFactoryFfi),
@@ -15,23 +21,85 @@ void main() {
1521 databaseFactory: databaseFactoryFfi,
1622 );
1723
18- test ('#start' , () {
19- final queue = GraphqlOfflineRequestQueue (
20- link: offlineClient,
21- requestManager: requestManager,
22- )..start ();
23- expect (queue.isRunning, isTrue);
24- queue.stop ();
24+ group ('Queue Lifecycle' , () {
25+ test ('#start' , () {
26+ final queue = GraphqlOfflineRequestQueue (
27+ link: offlineClient,
28+ requestManager: requestManager,
29+ )..start ();
30+ expect (queue.isRunning, isTrue);
31+ queue.stop ();
32+ });
33+
34+ test ('#stop' , () {
35+ final queue = GraphqlOfflineRequestQueue (
36+ link: offlineClient,
37+ requestManager: requestManager,
38+ )..start ();
39+ expect (queue.isRunning, isTrue);
40+ queue.stop ();
41+ expect (queue.isRunning, isFalse);
42+ });
2543 });
2644
27- test ('#stop' , () {
28- final queue = GraphqlOfflineRequestQueue (
29- link: offlineClient,
30- requestManager: requestManager,
31- )..start ();
32- expect (queue.isRunning, isTrue);
33- queue.stop ();
34- expect (queue.isRunning, isFalse);
45+ group ('Request Processing' , () {
46+ test ('#transmitRequest' , () async {
47+ final mockLink = MockLink ();
48+ var streamConsumed = false ;
49+
50+ when (mockLink.request (any)).thenAnswer ((_) {
51+ return Stream <Response >.eventTransformed (
52+ Stream .fromIterable ([
53+ const Response (
54+ data: {'test' : 'data' },
55+ response: {'body' : '{"test":"data"}' },
56+ ),
57+ ]),
58+ (sink) {
59+ streamConsumed = true ;
60+ return sink;
61+ },
62+ );
63+ });
64+
65+ final testQueue = GraphqlOfflineRequestQueue (
66+ link: mockLink,
67+ requestManager: requestManager,
68+ );
69+
70+ final testRequest = Request (
71+ operation: Operation (
72+ document: gql ('mutation TestMutation { test }' ),
73+ ),
74+ );
75+
76+ await testQueue.transmitRequest (testRequest);
77+
78+ expect (streamConsumed, isTrue);
79+ });
80+
81+ test ('#transmitRequest with errors' , () async {
82+ final mockLink = MockLink ();
83+ when (mockLink.request (any)).thenAnswer (
84+ (_) => Stream .error (Exception ('Network error' )),
85+ );
86+
87+ final testQueue = GraphqlOfflineRequestQueue (
88+ link: mockLink,
89+ requestManager: requestManager,
90+ );
91+
92+ final testRequest = Request (
93+ operation: Operation (
94+ document: gql ('mutation TestMutation { test }' ),
95+ ),
96+ );
97+
98+ expect (
99+ () => testQueue.transmitRequest (testRequest),
100+ throwsA (isA <Exception >()),
101+ );
102+ });
35103 });
36104 });
37105}
0 commit comments