Skip to content

Instantly share code, notes, and snippets.

@veeramarni
Last active April 9, 2019 22:33
Show Gist options
  • Save veeramarni/0360e0d2034476982497a2641e00a90e to your computer and use it in GitHub Desktop.
Save veeramarni/0360e0d2034476982497a2641e00a90e to your computer and use it in GitHub Desktop.
Apollo Client Test Sample
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { withClientState } from 'apollo-link-state';
import { ApolloLink } from 'apollo-link';
import gql from 'graphql-tag';
import 'jest';
describe('Apollo Client tests', () => {
const query = gql`
{
field @client
}
`;
let count = 0;
const local = withClientState({
resolvers: {
ClassName: {
subFunction: () => {
return 2;
},
isSubFunction: () => {
return true;
},
},
Query: {
field: () => {
count++;
return 1;
},
className: () => ({
__typename: 'ClassName',
}),
},
},
defaults: {
},
});
const client = new ApolloClient({
cache: new InMemoryCache(),
link: local,
});
it('test', async () => {
const result = await client.query({ query });
expect({ ...result.data }).toMatchObject({ field: 1 });
expect(count).toBe(1);
});
it('test ClassName', async () => {
const classQuery = gql`
{
className @client {
isSubFunction
}
}
`;
const result = await client.query({ query: classQuery });
expect({ ...result.data }).toMatchObject({ className: { isSubFunction: true, __typename: 'ClassName' } });
});
it(' service', async () => {
const isSubFunction = gql`
{
className @client {
isSubFunction
}
}
`;
const subFunction = gql`
{
className @client {
subFunction
}
}
`;
class NewService {
public async isSubFunction() {
const result = await client.query({ query: isSubFunction });
return result.data.className.isSubFunction;
}
public async subFunction() {
const result = await client.query({ query: subFunction });
return result.data.className.subFunction;
}
}
const service = new NewService();
expect(await service.isSubFunction()).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment