@@ -16,6 +16,13 @@ const testSubscription = gql`
1616 }
1717 }
1818` ;
19+ const testMutation = gql `
20+ mutation addHero($hero: String!) {
21+ addHero(hero: $hero) {
22+ name
23+ }
24+ }
25+ ` ;
1926
2027describe ( 'TestOperation' , ( ) => {
2128 let mock : ApolloTestingBackend ;
@@ -60,7 +67,7 @@ describe('TestOperation', () => {
6067 } ) ;
6168 } ) ;
6269
63- test ( 'should close the operation except for subscription' , done => {
70+ test ( 'should leave the operation open for a subscription' , done => {
6471 const operation = buildOperationForLink ( testSubscription , { } ) ;
6572 const emittedResults : FetchResult [ ] = [ ] ;
6673
@@ -84,6 +91,7 @@ describe('TestOperation', () => {
8491 done ( ) ;
8592 } ,
8693 } ) ;
94+
8795
8896 const testOperation = mock . expectOne ( testSubscription ) ;
8997
@@ -94,6 +102,69 @@ describe('TestOperation', () => {
94102 testOperation . flushData ( {
95103 heroes : [ 'second Hero' ] ,
96104 } ) ;
105+
97106 testOperation . complete ( ) ;
98107 } ) ;
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+ } ) ;
99170} ) ;
0 commit comments