@@ -4,9 +4,9 @@ import { ApolloClient, FetchResult } from "../core";
4
4
import { InMemoryCache } from "../cache" ;
5
5
import { ApolloError , PROTOCOL_ERRORS_SYMBOL } from "../errors" ;
6
6
import { QueryManager } from "../core/QueryManager" ;
7
- import { itAsync , mockObservableLink } from "../testing" ;
7
+ import { mockObservableLink } from "../testing" ;
8
8
import { GraphQLError } from "graphql" ;
9
- import { spyOnConsole } from "../testing/internal" ;
9
+ import { ObservableStream , spyOnConsole } from "../testing/internal" ;
10
10
import { getDefaultOptionsForQueryManagerTests } from "../testing/core/mocking/mockQueryManager" ;
11
11
12
12
describe ( "GraphQL Subscriptions" , ( ) => {
@@ -47,62 +47,40 @@ describe("GraphQL Subscriptions", () => {
47
47
} ;
48
48
} ) ;
49
49
50
- itAsync (
51
- "should start a subscription on network interface and unsubscribe" ,
52
- ( resolve , reject ) => {
53
- const link = mockObservableLink ( ) ;
54
- // This test calls directly through Apollo Client
55
- const client = new ApolloClient ( {
56
- link,
57
- cache : new InMemoryCache ( { addTypename : false } ) ,
58
- } ) ;
50
+ it ( "should start a subscription on network interface and unsubscribe" , async ( ) => {
51
+ const link = mockObservableLink ( ) ;
52
+ // This test calls directly through Apollo Client
53
+ const client = new ApolloClient ( {
54
+ link,
55
+ cache : new InMemoryCache ( { addTypename : false } ) ,
56
+ } ) ;
59
57
60
- let count = 0 ;
61
- const sub = client . subscribe ( defaultOptions ) . subscribe ( {
62
- next ( result ) {
63
- count ++ ;
64
- expect ( result ) . toEqual ( results [ 0 ] . result ) ;
58
+ const stream = new ObservableStream ( client . subscribe ( defaultOptions ) ) ;
59
+ link . simulateResult ( results [ 0 ] ) ;
65
60
66
- // Test unsubscribing
67
- if ( count > 1 ) {
68
- throw new Error ( "next fired after unsubscribing" ) ;
69
- }
70
- sub . unsubscribe ( ) ;
71
- resolve ( ) ;
72
- } ,
73
- } ) ;
61
+ await expect ( stream ) . toEmitValue ( results [ 0 ] . result ) ;
74
62
75
- link . simulateResult ( results [ 0 ] ) ;
76
- }
77
- ) ;
63
+ stream . unsubscribe ( ) ;
64
+ } ) ;
78
65
79
- itAsync ( "should subscribe with default values" , ( resolve , reject ) => {
66
+ it ( "should subscribe with default values" , async ( ) => {
80
67
const link = mockObservableLink ( ) ;
81
68
// This test calls directly through Apollo Client
82
69
const client = new ApolloClient ( {
83
70
link,
84
71
cache : new InMemoryCache ( { addTypename : false } ) ,
85
72
} ) ;
86
73
87
- let count = 0 ;
88
- const sub = client . subscribe ( options ) . subscribe ( {
89
- next ( result ) {
90
- expect ( result ) . toEqual ( results [ 0 ] . result ) ;
74
+ const stream = new ObservableStream ( client . subscribe ( options ) ) ;
91
75
92
- // Test unsubscribing
93
- if ( count > 1 ) {
94
- throw new Error ( "next fired after unsubscribing" ) ;
95
- }
96
- sub . unsubscribe ( ) ;
76
+ link . simulateResult ( results [ 0 ] ) ;
97
77
98
- resolve ( ) ;
99
- } ,
100
- } ) ;
78
+ await expect ( stream ) . toEmitValue ( results [ 0 ] . result ) ;
101
79
102
- link . simulateResult ( results [ 0 ] ) ;
80
+ stream . unsubscribe ( ) ;
103
81
} ) ;
104
82
105
- itAsync ( "should multiplex subscriptions" , ( resolve , reject ) => {
83
+ it ( "should multiplex subscriptions" , async ( ) => {
106
84
const link = mockObservableLink ( ) ;
107
85
const queryManager = new QueryManager (
108
86
getDefaultOptionsForQueryManagerTests ( {
@@ -112,88 +90,57 @@ describe("GraphQL Subscriptions", () => {
112
90
) ;
113
91
114
92
const obs = queryManager . startGraphQLSubscription ( options ) ;
115
-
116
- let counter = 0 ;
117
-
118
- // tslint:disable-next-line
119
- obs . subscribe ( {
120
- next ( result ) {
121
- expect ( result ) . toEqual ( results [ 0 ] . result ) ;
122
- counter ++ ;
123
- if ( counter === 2 ) {
124
- resolve ( ) ;
125
- }
126
- } ,
127
- } ) as any ;
128
-
129
- // Subscribe again. Should also receive the same result.
130
- // tslint:disable-next-line
131
- obs . subscribe ( {
132
- next ( result ) {
133
- expect ( result ) . toEqual ( results [ 0 ] . result ) ;
134
- counter ++ ;
135
- if ( counter === 2 ) {
136
- resolve ( ) ;
137
- }
138
- } ,
139
- } ) as any ;
93
+ const stream1 = new ObservableStream ( obs ) ;
94
+ const stream2 = new ObservableStream ( obs ) ;
140
95
141
96
link . simulateResult ( results [ 0 ] ) ;
97
+
98
+ await expect ( stream1 ) . toEmitValue ( results [ 0 ] . result ) ;
99
+ await expect ( stream2 ) . toEmitValue ( results [ 0 ] . result ) ;
142
100
} ) ;
143
101
144
- itAsync (
145
- "should receive multiple results for a subscription" ,
146
- ( resolve , reject ) => {
147
- const link = mockObservableLink ( ) ;
148
- let numResults = 0 ;
149
- const queryManager = new QueryManager (
150
- getDefaultOptionsForQueryManagerTests ( {
151
- link,
152
- cache : new InMemoryCache ( { addTypename : false } ) ,
153
- } )
154
- ) ;
155
-
156
- // tslint:disable-next-line
157
- queryManager . startGraphQLSubscription ( options ) . subscribe ( {
158
- next ( result ) {
159
- expect ( result ) . toEqual ( results [ numResults ] . result ) ;
160
- numResults ++ ;
161
- if ( numResults === 4 ) {
162
- resolve ( ) ;
163
- }
164
- } ,
165
- } ) as any ;
102
+ it ( "should receive multiple results for a subscription" , async ( ) => {
103
+ const link = mockObservableLink ( ) ;
104
+ const queryManager = new QueryManager (
105
+ getDefaultOptionsForQueryManagerTests ( {
106
+ link,
107
+ cache : new InMemoryCache ( { addTypename : false } ) ,
108
+ } )
109
+ ) ;
166
110
167
- for ( let i = 0 ; i < 4 ; i ++ ) {
168
- link . simulateResult ( results [ i ] ) ;
169
- }
111
+ const stream = new ObservableStream (
112
+ queryManager . startGraphQLSubscription ( options )
113
+ ) ;
114
+
115
+ for ( let i = 0 ; i < 4 ; i ++ ) {
116
+ link . simulateResult ( results [ i ] ) ;
170
117
}
171
- ) ;
172
-
173
- itAsync (
174
- "should not cache subscription data if a `no-cache` fetch policy is used" ,
175
- ( resolve , reject ) => {
176
- const link = mockObservableLink ( ) ;
177
- const cache = new InMemoryCache ( { addTypename : false } ) ;
178
- const client = new ApolloClient ( {
179
- link,
180
- cache,
181
- } ) ;
182
118
183
- expect ( cache . extract ( ) ) . toEqual ( { } ) ;
119
+ await expect ( stream ) . toEmitValue ( results [ 0 ] . result ) ;
120
+ await expect ( stream ) . toEmitValue ( results [ 1 ] . result ) ;
121
+ await expect ( stream ) . toEmitValue ( results [ 2 ] . result ) ;
122
+ await expect ( stream ) . toEmitValue ( results [ 3 ] . result ) ;
123
+ await expect ( stream ) . not . toEmitAnything ( ) ;
124
+ } ) ;
184
125
185
- options . fetchPolicy = "no-cache" ;
186
- const sub = client . subscribe ( options ) . subscribe ( {
187
- next ( ) {
188
- expect ( cache . extract ( ) ) . toEqual ( { } ) ;
189
- sub . unsubscribe ( ) ;
190
- resolve ( ) ;
191
- } ,
192
- } ) ;
126
+ it ( "should not cache subscription data if a `no-cache` fetch policy is used" , async ( ) => {
127
+ const link = mockObservableLink ( ) ;
128
+ const cache = new InMemoryCache ( { addTypename : false } ) ;
129
+ const client = new ApolloClient ( {
130
+ link,
131
+ cache,
132
+ } ) ;
193
133
194
- link . simulateResult ( results [ 0 ] ) ;
195
- }
196
- ) ;
134
+ expect ( cache . extract ( ) ) . toEqual ( { } ) ;
135
+
136
+ options . fetchPolicy = "no-cache" ;
137
+ const stream = new ObservableStream ( client . subscribe ( options ) ) ;
138
+
139
+ link . simulateResult ( results [ 0 ] ) ;
140
+
141
+ await expect ( stream ) . toEmitNext ( ) ;
142
+ expect ( cache . extract ( ) ) . toEqual ( { } ) ;
143
+ } ) ;
197
144
198
145
it ( "should throw an error if the result has errors on it" , ( ) => {
199
146
const link = mockObservableLink ( ) ;
@@ -492,27 +439,22 @@ describe("GraphQL Subscriptions", () => {
492
439
} ) ;
493
440
} ) ;
494
441
495
- itAsync (
496
- "should pass a context object through the link execution chain" ,
497
- ( resolve , reject ) => {
498
- const link = mockObservableLink ( ) ;
499
- const client = new ApolloClient ( {
500
- cache : new InMemoryCache ( ) ,
501
- link,
502
- } ) ;
442
+ it ( "should pass a context object through the link execution chain" , async ( ) => {
443
+ const link = mockObservableLink ( ) ;
444
+ const client = new ApolloClient ( {
445
+ cache : new InMemoryCache ( ) ,
446
+ link,
447
+ } ) ;
503
448
504
- client . subscribe ( options ) . subscribe ( {
505
- next ( ) {
506
- expect ( link . operation ?. getContext ( ) . someVar ) . toEqual (
507
- options . context . someVar
508
- ) ;
509
- resolve ( ) ;
510
- } ,
511
- } ) ;
449
+ const stream = new ObservableStream ( client . subscribe ( options ) ) ;
512
450
513
- link . simulateResult ( results [ 0 ] ) ;
514
- }
515
- ) ;
451
+ link . simulateResult ( results [ 0 ] ) ;
452
+
453
+ await expect ( stream ) . toEmitNext ( ) ;
454
+ expect ( link . operation ?. getContext ( ) . someVar ) . toEqual (
455
+ options . context . someVar
456
+ ) ;
457
+ } ) ;
516
458
517
459
it ( "should throw an error if the result has protocolErrors on it" , async ( ) => {
518
460
const link = mockObservableLink ( ) ;
0 commit comments