1
1
import { envelop , useSchema } from '@envelop/core'
2
2
import { makeExecutableSchema } from '@graphql-tools/schema'
3
+ import { Repeater } from '@repeaterjs/repeater'
3
4
import { parse } from 'graphql'
4
5
import usePollingLive from '../src/index.js'
5
6
@@ -19,6 +20,9 @@ describe('Polling Live Queries', () => {
19
20
const getEnveloped = envelop ( {
20
21
plugins : [ useSchema ( schema ) , usePollingLive ( ) ] ,
21
22
} )
23
+ afterEach ( async ( ) => {
24
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) )
25
+ } )
22
26
it ( 'should create a live stream with the given argument as an interval' , async ( ) => {
23
27
const enveloped = getEnveloped ( )
24
28
@@ -37,6 +41,7 @@ describe('Polling Live Queries', () => {
37
41
let lastTimestamp
38
42
let i = 0
39
43
for await ( const item of result ) {
44
+ expect ( item ?. isLive ) . toBe ( true )
40
45
expect ( item ?. data ?. timestamp ) . toBeDefined ( )
41
46
if ( lastTimestamp ) {
42
47
expect ( new Date ( item ?. data ?. timestamp ) . getTime ( ) - new Date ( lastTimestamp ) . getTime ( ) ) . toBeGreaterThan ( 0.49 )
@@ -47,7 +52,7 @@ describe('Polling Live Queries', () => {
47
52
}
48
53
i ++
49
54
}
50
- expect . assertions ( 8 )
55
+ expect . assertions ( 12 )
51
56
} )
52
57
it ( 'should create a live stream with 1000 ms interval by default' , async ( ) => {
53
58
const enveloped = getEnveloped ( )
@@ -67,6 +72,7 @@ describe('Polling Live Queries', () => {
67
72
let lastTimestamp
68
73
let i = 0
69
74
for await ( const item of result ) {
75
+ expect ( item ?. isLive ) . toBe ( true )
70
76
expect ( item ?. data ?. timestamp ) . toBeDefined ( )
71
77
if ( lastTimestamp ) {
72
78
expect ( new Date ( item ?. data ?. timestamp ) . getTime ( ) - new Date ( lastTimestamp ) . getTime ( ) ) . toBeGreaterThan ( 0.99 )
@@ -77,7 +83,7 @@ describe('Polling Live Queries', () => {
77
83
}
78
84
i ++
79
85
}
80
- expect . assertions ( 8 )
86
+ expect . assertions ( 12 )
81
87
} )
82
88
it ( 'should return regular results if there is no @live' , async ( ) => {
83
89
const enveloped = getEnveloped ( )
@@ -94,4 +100,43 @@ describe('Polling Live Queries', () => {
94
100
95
101
expect ( result ?. data ?. timestamp ) . toBeDefined ( )
96
102
} )
103
+ it ( 'should throw an error if the original executor returns AsyncIterable' , async ( ) => {
104
+ const getEnveloped = envelop ( {
105
+ plugins : [
106
+ useSchema ( schema ) ,
107
+ {
108
+ onExecute ( { setExecuteFn } ) {
109
+ return setExecuteFn (
110
+ ( ) =>
111
+ new Repeater ( ( push , stop ) => {
112
+ push ( { data : { timestamp : new Date ( ) . toISOString ( ) } } )
113
+ stop ( )
114
+ } ) as any ,
115
+ )
116
+ } ,
117
+ } ,
118
+ usePollingLive ( ) ,
119
+ ] ,
120
+ } )
121
+
122
+ const enveloped = getEnveloped ( )
123
+
124
+ const result = await enveloped . execute ( {
125
+ schema : enveloped . schema ,
126
+ document : parse ( /* GraphQL */ `
127
+ query TestQuery @live {
128
+ timestamp
129
+ }
130
+ ` ) ,
131
+ operationName : 'TestQuery' ,
132
+ } )
133
+
134
+ expect ( result [ Symbol . asyncIterator ] ) . toBeDefined ( )
135
+ for await ( const item of result as AsyncIterable < any > ) {
136
+ expect ( item ?. data ) . toBe ( null )
137
+ expect ( item ?. errors ?. [ 0 ] ?. message ) . toBe ( 'Execution returned AsyncIterable which is not supported!' )
138
+ }
139
+
140
+ expect . assertions ( 3 )
141
+ } )
97
142
} )
0 commit comments