@@ -16,6 +16,13 @@ const testSubscription = gql`
16
16
}
17
17
}
18
18
` ;
19
+ const testMutation = gql `
20
+ mutation addHero($hero: String!) {
21
+ addHero(hero: $hero) {
22
+ name
23
+ }
24
+ }
25
+ ` ;
19
26
20
27
describe ( 'TestOperation' , ( ) => {
21
28
let mock : ApolloTestingBackend ;
@@ -60,7 +67,7 @@ describe('TestOperation', () => {
60
67
} ) ;
61
68
} ) ;
62
69
63
- test ( 'should close the operation except for subscription' , done => {
70
+ test ( 'should leave the operation open for a subscription' , done => {
64
71
const operation = buildOperationForLink ( testSubscription , { } ) ;
65
72
const emittedResults : FetchResult [ ] = [ ] ;
66
73
@@ -84,6 +91,7 @@ describe('TestOperation', () => {
84
91
done ( ) ;
85
92
} ,
86
93
} ) ;
94
+
87
95
88
96
const testOperation = mock . expectOne ( testSubscription ) ;
89
97
@@ -94,6 +102,69 @@ describe('TestOperation', () => {
94
102
testOperation . flushData ( {
95
103
heroes : [ 'second Hero' ] ,
96
104
} ) ;
105
+
97
106
testOperation . complete ( ) ;
98
107
} ) ;
108
+
109
+ test ( 'should close the operation after a query' , done => {
110
+ const operation = buildOperationForLink ( testQuery , { } ) ;
111
+ const emittedResults : FetchResult [ ] = [ ] ;
112
+
113
+ execute ( link , operation ) . subscribe ( {
114
+ next ( result ) {
115
+ emittedResults . push ( result ) ;
116
+ } ,
117
+ complete ( ) {
118
+ expect ( emittedResults ) . toEqual ( [
119
+ {
120
+ data : {
121
+ heroes : [ 'first Hero' ] ,
122
+ } ,
123
+ }
124
+ ] ) ;
125
+ done ( ) ;
126
+ } ,
127
+ } ) ;
128
+
129
+ const testOperation = mock . expectOne ( testQuery ) ;
130
+
131
+ testOperation . flushData ( {
132
+ heroes : [ 'first Hero' ] ,
133
+ } ) ;
134
+
135
+ testOperation . flushData ( {
136
+ heroes : [ 'second Hero' ] ,
137
+ } ) ;
138
+ } ) ;
139
+
140
+ test ( 'should close the operation after a mutation' , done => {
141
+ const operation = buildOperationForLink ( testMutation , { hero : 'firstHero' } ) ;
142
+ const emittedResults : FetchResult [ ] = [ ] ;
143
+
144
+ execute ( link , operation ) . subscribe ( {
145
+ next ( result ) {
146
+ emittedResults . push ( result ) ;
147
+ } ,
148
+ complete ( ) {
149
+ expect ( emittedResults ) . toEqual ( [
150
+ {
151
+ data : {
152
+ heroes : [ 'first Hero' ] ,
153
+ } ,
154
+ } ,
155
+ ] ) ;
156
+ done ( ) ;
157
+ } ,
158
+ } ) ;
159
+
160
+ const testOperation = mock . expectOne ( testMutation ) ;
161
+
162
+ testOperation . flushData ( {
163
+ heroes : [ 'first Hero' ] ,
164
+ } ) ;
165
+
166
+ testOperation . flushData ( {
167
+ heroes : [ 'second Hero' ] ,
168
+ } ) ;
169
+ } ) ;
99
170
} ) ;
0 commit comments