From 4fb50639477babc36e03ef18ea040ad4e6cef26c Mon Sep 17 00:00:00 2001 From: frozen_byte Date: Wed, 30 Oct 2024 16:54:46 +0100 Subject: [PATCH] fix(TestOperation): add specific tests for all operation types --- .../testing/tests/operation.spec.ts | 73 ++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/packages/apollo-angular/testing/tests/operation.spec.ts b/packages/apollo-angular/testing/tests/operation.spec.ts index f1a6447e1..5fd34918a 100644 --- a/packages/apollo-angular/testing/tests/operation.spec.ts +++ b/packages/apollo-angular/testing/tests/operation.spec.ts @@ -16,6 +16,13 @@ const testSubscription = gql` } } `; +const testMutation = gql` + mutation addHero($hero: String!) { + addHero(hero: $hero) { + name + } + } +`; describe('TestOperation', () => { let mock: ApolloTestingBackend; @@ -60,7 +67,7 @@ describe('TestOperation', () => { }); }); - test('should close the operation except for subscription', done => { + test('should leave the operation open for a subscription', done => { const operation = buildOperationForLink(testSubscription, {}); const emittedResults: FetchResult[] = []; @@ -84,6 +91,7 @@ describe('TestOperation', () => { done(); }, }); + const testOperation = mock.expectOne(testSubscription); @@ -94,6 +102,69 @@ describe('TestOperation', () => { testOperation.flushData({ heroes: ['second Hero'], }); + testOperation.complete(); }); + + test('should close the operation after a query', done => { + const operation = buildOperationForLink(testQuery, {}); + const emittedResults: FetchResult[] = []; + + execute(link, operation).subscribe({ + next(result) { + emittedResults.push(result); + }, + complete() { + expect(emittedResults).toEqual([ + { + data: { + heroes: ['first Hero'], + }, + } + ]); + done(); + }, + }); + + const testOperation = mock.expectOne(testQuery); + + testOperation.flushData({ + heroes: ['first Hero'], + }); + + testOperation.flushData({ + heroes: ['second Hero'], + }); + }); + + test('should close the operation after a mutation', done => { + const operation = buildOperationForLink(testMutation, {hero: 'firstHero'}); + const emittedResults: FetchResult[] = []; + + execute(link, operation).subscribe({ + next(result) { + emittedResults.push(result); + }, + complete() { + expect(emittedResults).toEqual([ + { + data: { + heroes: ['first Hero'], + }, + }, + ]); + done(); + }, + }); + + const testOperation = mock.expectOne(testMutation); + + testOperation.flushData({ + heroes: ['first Hero'], + }); + + testOperation.flushData({ + heroes: ['second Hero'], + }); + }); });