Skip to content

Commit

Permalink
Add test for multiple subscribers to a MockSubscriptionLink
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankland committed May 8, 2020
1 parent 327f534 commit dfd36b1
Showing 1 changed file with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { render, cleanup, wait } from '@testing-library/react';
import gql from 'graphql-tag';

import { MockSubscriptionLink } from '../mockSubscriptionLink';
import { ApolloClient } from '../../../../ApolloClient';
import { InMemoryCache as Cache } from '../../../../cache/inmemory/inMemoryCache';
import { requireReactLazily } from '../../../../react/react';
import { ApolloProvider } from '../../../../react/context/ApolloProvider';
import { useSubscription } from '../../../../react/hooks/useSubscription';

const React = requireReactLazily();

describe('mockSubscriptionLink', () => {
it('should work with multiple subscribers to the same mock websocket', () => {
const subscription = gql`
subscription {
car {
make
}
}
`;

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

let renderCountA = 0;
const ComponentA = () => {
useSubscription(subscription);
renderCountA += 1;
return null;
};

let renderCountB = 0;
const ComponentB = () => {
const { loading, data, error } = useSubscription(subscription);
renderCountB += 1;
return null;
};

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

const Component = () => {
const [index, setIndex] = React.useState(0);
React.useEffect(() => {
if (index >= results.length) return;
link.simulateResult(results[index]);
setIndex(index + 1);
}, [index]);
return null;
};

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

return wait(() => {
expect(renderCountA).toBe(results.length + 1);
expect(renderCountB).toBe(results.length + 1);
}, { timeout: 1000 });
});
});

0 comments on commit dfd36b1

Please sign in to comment.