Skip to content

Commit

Permalink
Fix context option omission in useSubscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenn Creighton committed Mar 18, 2021
1 parent e7350a6 commit 979b4e5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/react/data/SubscriptionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export class SubscriptionData<
this.currentObservable.query = this.refreshClient().client.subscribe({
query: options.subscription,
variables: options.variables,
fetchPolicy: options.fetchPolicy
fetchPolicy: options.fetchPolicy,
context: options.context,
});
}

Expand Down
69 changes: 68 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 { render, cleanup, wait } from '@testing-library/react';
import gql from 'graphql-tag';

import { ApolloClient } from '../../../core';
import { ApolloClient, ApolloLink, concat } from '../../../core';
import { InMemoryCache as Cache } from '../../../cache';
import { ApolloProvider } from '../../context';
import { MockSubscriptionLink } from '../../../testing';
Expand Down Expand Up @@ -293,4 +293,71 @@ describe('useSubscription Hook', () => {
expect(renderCount).toEqual(7);
});
});

it('should share context set in options', async () => {
const subscription = gql`
subscription {
car {
make
}
}
`;

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

let context: string;
const link = new MockSubscriptionLink();
const contextLink = new ApolloLink((operation, forward) => {
context = operation.getContext()?.make
return forward(operation);
});
const client = new ApolloClient({
link: concat(contextLink, link),
cache: new Cache({ addTypename: false })
});

let renderCount = 0;
const Component = () => {
const { loading, data, error } = useSubscription(subscription, {
context: {
make: 'Audi',
},
});
switch (renderCount) {
case 0:
expect(loading).toBe(true);
expect(error).toBeUndefined();
expect(data).toBeUndefined();
break;
case 1:
expect(loading).toBe(false);
expect(data).toEqual(results[0].result.data);
break;
case 2:
expect(loading).toBe(false);
expect(data).toEqual(results[1].result.data);
break;
default:
}
setTimeout(() => {
renderCount <= results.length &&
link.simulateResult(results[renderCount - 1]);
});
renderCount += 1;
return null;
};

render(
<ApolloProvider client={client}>
<Component />
</ApolloProvider>
);

return wait(() => {
expect(renderCount).toBe(3);
expect(context).toEqual('Audi');
});
});
});
1 change: 1 addition & 0 deletions src/react/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export interface BaseSubscriptionOptions<
| ((options: BaseSubscriptionOptions<TData, TVariables>) => boolean);
client?: ApolloClient<object>;
skip?: boolean;
context?: Context;
onSubscriptionData?: (options: OnSubscriptionDataOptions<TData>) => any;
onSubscriptionComplete?: () => void;
}
Expand Down

0 comments on commit 979b4e5

Please sign in to comment.