Skip to content

Commit

Permalink
Add onError option to useSubscription hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvisser101 committed Mar 7, 2022
1 parent 013c2f7 commit 71301fd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/react/hooks/__tests__/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import gql from 'graphql-tag';

import { ApolloClient, ApolloLink, concat } from '../../../core';
import { ApolloClient, ApolloError, ApolloLink, concat } from '../../../core';
import { InMemoryCache as Cache } from '../../../cache';
import { ApolloProvider } from '../../context';
import { MockSubscriptionLink } from '../../../testing';
Expand Down Expand Up @@ -61,6 +61,57 @@ describe('useSubscription Hook', () => {
expect(result.current.data).toEqual(results[3].result.data);
});

it('should call onError after error results', async () => {
const subscription = gql`
subscription {
car {
make
}
}
`;

const results = ['Audi', 'BMW', 'Mercedes', 'Hyundai'].map(make => ({
result: { data: { car: { make } } }
}));

const errorResult = {
error: new ApolloError({ errorMessage: "test" }),
result: { data: { car: { make: null } } },
};

const link = new MockSubscriptionLink();
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false })
});


const onError = jest.fn();
const { result, waitForNextUpdate } = renderHook(
() => useSubscription(subscription, { onError }),
{
wrapper: ({ children }) => (
<ApolloProvider client={client}>
{children}
</ApolloProvider>
),
},
);

expect(result.current.loading).toBe(true);
expect(result.current.error).toBe(undefined);
expect(result.current.data).toBe(undefined);
setTimeout(() => link.simulateResult(results[0]));
await waitForNextUpdate();
expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual(results[0].result.data);
setTimeout(() => link.simulateResult(errorResult));
await waitForNextUpdate();
setTimeout(() => {
expect(onError).toHaveBeenCalledTimes(1);
});
});

it('should cleanup after the subscription component has been unmounted', async () => {
const subscription = gql`
subscription {
Expand Down
1 change: 1 addition & 0 deletions src/react/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
error,
variables: options?.variables,
});
ref.current.options?.onError?.(error);
},
complete() {
ref.current.options?.onSubscriptionComplete?.();
Expand Down
1 change: 1 addition & 0 deletions src/react/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export interface BaseSubscriptionOptions<
skip?: boolean;
context?: DefaultContext;
onSubscriptionData?: (options: OnSubscriptionDataOptions<TData>) => any;
onError?: (error: ApolloError) => void;
onSubscriptionComplete?: () => void;
}

Expand Down

0 comments on commit 71301fd

Please sign in to comment.