Skip to content

Commit

Permalink
feat(SubscriptionOperation): only complete Queries and Mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozen-byte committed Oct 22, 2024
1 parent 007ee40 commit e427e2c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/apollo-angular/testing/src/operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ExecutionResult, GraphQLError } from 'graphql';
import { ExecutionResult, GraphQLError, Kind, OperationTypeNode } from 'graphql';
import type { FragmentDefinitionNode, OperationDefinitionNode } from 'graphql/index';
import { Observer } from 'rxjs';
import { ApolloError, FetchResult, Operation as LinkOperation } from '@apollo/client/core';
import { getMainDefinition } from '@apollo/client/utilities';

const isApolloError = (err: any): err is ApolloError => err && err.hasOwnProperty('graphQLErrors');

Expand All @@ -9,21 +11,35 @@ export type Operation = LinkOperation & {
};

export class TestOperation<T = { [key: string]: any }> {
private readonly definition: OperationDefinitionNode | FragmentDefinitionNode;

constructor(
public readonly operation: Operation,
private readonly observer: Observer<FetchResult<T>>,
) {}
) {
this.definition = getMainDefinition(this.operation.query);
}

public flush(result: ExecutionResult | ApolloError): void {
if (isApolloError(result)) {
this.observer.error(result);
} else {
const fetchResult = result ? { ...result } : result;
this.observer.next(fetchResult as any);
this.observer.complete();

if (
this.definition.kind === Kind.OPERATION_DEFINITION &&
this.definition.operation !== OperationTypeNode.SUBSCRIPTION
) {
this.complete();
}
}
}

public complete() {
this.observer.complete();
}

public flushData(data: { [key: string]: any } | null): void {
this.flush({
data,
Expand Down
44 changes: 44 additions & 0 deletions packages/apollo-angular/testing/tests/operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const testQuery = gql`
}
}
`;
const testSubscription = gql`
subscription newHeroes {
heroes {
name
}
}
`;

describe('TestOperation', () => {
let mock: ApolloTestingBackend;
Expand Down Expand Up @@ -52,4 +59,41 @@ describe('TestOperation', () => {
heroes: [],
});
});

test('should close the operation except for subscription', done => {
const operation = buildOperationForLink(testSubscription, {});
const emittedResults: any[] = [];

execute(link, operation as any).subscribe({
next(result: any) {
emittedResults.push(result);
},
complete() {
expect(emittedResults).toEqual([
{
data: {
heroes: ['first Hero'],
},
},
{
data: {
heroes: ['second Hero'],
},
},
]);
done();
},
});

const testOperation = mock.expectOne(testSubscription);

testOperation.flushData({
heroes: ['first Hero'],
});

testOperation.flushData({
heroes: ['second Hero'],
});
testOperation.complete();
});
});
1 change: 1 addition & 0 deletions website/src/pages/docs/development-and-testing/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ It's an object returned by `expectOne` and `match` methods.
ApolloError instance
- `networkError(error: Error): void{:ts}` - to flush an operation with a network error
- `graphqlErrors(errors: GraphQLError[]): void{:ts}` - to flush an operation with graphql errors
- `complete(): void{:ts}` - manually complete the connection, useful for subscription based testing

## Using Named Clients

Expand Down

0 comments on commit e427e2c

Please sign in to comment.