Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow merging of mocked queries in GraphQL update #2676

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-pigs-heal.md
@@ -0,0 +1,5 @@
---
'@shopify/graphql-testing': minor
---

Added `updateMockWithMerge` to `GraphQLController` to allow a partial update of mocked queries.
9 changes: 8 additions & 1 deletion packages/graphql-testing/src/graphql-controller.ts
Expand Up @@ -4,7 +4,7 @@
import {MockLink, InflightLink} from './links';
import {Operations} from './operations';
import {operationNameFromFindOptions} from './utilities';
import type {GraphQLMock, MockRequest, FindOptions} from './types';
import type {GraphQLMock, MockGraphQLObject, MockRequest, FindOptions} from './types';

Check failure on line 7 in packages/graphql-testing/src/graphql-controller.ts

View workflow job for this annotation

GitHub Actions / Test (Node 14, React 17)

Replace `GraphQLMock,·MockGraphQLObject,·MockRequest,·FindOptions` with `⏎··GraphQLMock,⏎··MockGraphQLObject,⏎··MockRequest,⏎··FindOptions,⏎`

Check failure on line 7 in packages/graphql-testing/src/graphql-controller.ts

View workflow job for this annotation

GitHub Actions / Test (Node 14, React 18)

Replace `GraphQLMock,·MockGraphQLObject,·MockRequest,·FindOptions` with `⏎··GraphQLMock,⏎··MockGraphQLObject,⏎··MockRequest,⏎··FindOptions,⏎`

Check failure on line 7 in packages/graphql-testing/src/graphql-controller.ts

View workflow job for this annotation

GitHub Actions / Test (Node 16, React 17)

Replace `GraphQLMock,·MockGraphQLObject,·MockRequest,·FindOptions` with `⏎··GraphQLMock,⏎··MockGraphQLObject,⏎··MockRequest,⏎··FindOptions,⏎`

Check failure on line 7 in packages/graphql-testing/src/graphql-controller.ts

View workflow job for this annotation

GitHub Actions / Test (Node 16, React 18)

Replace `GraphQLMock,·MockGraphQLObject,·MockRequest,·FindOptions` with `⏎··GraphQLMock,⏎··MockGraphQLObject,⏎··MockRequest,⏎··FindOptions,⏎`

Check failure on line 7 in packages/graphql-testing/src/graphql-controller.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 17)

Replace `GraphQLMock,·MockGraphQLObject,·MockRequest,·FindOptions` with `⏎··GraphQLMock,⏎··MockGraphQLObject,⏎··MockRequest,⏎··FindOptions,⏎`

Check failure on line 7 in packages/graphql-testing/src/graphql-controller.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 18)

Replace `GraphQLMock,·MockGraphQLObject,·MockRequest,·FindOptions` with `⏎··GraphQLMock,⏎··MockGraphQLObject,⏎··MockRequest,⏎··FindOptions,⏎`

export interface Options {
cacheOptions?: InMemoryCacheConfig;
Expand Down Expand Up @@ -56,6 +56,13 @@
this.mockLink.updateMock(mock);
}

updateWithMerge(mock: MockGraphQLObject) {
if (!this.mockLink) {
return;
}
this.mockLink.updateMockWithMerge(mock);
}

async resolveAll(options: ResolveAllFindOptions = {}) {
let requestFilter: ((operation: MockRequest) => boolean) | undefined;

Expand Down
15 changes: 14 additions & 1 deletion packages/graphql-testing/src/links/mocks.ts
Expand Up @@ -3,7 +3,11 @@ import {ApolloLink, Observable} from '@apollo/client';
import type {ExecutionResult} from 'graphql';
import {GraphQLError} from 'graphql';

import type {GraphQLMock, MockGraphQLFunction} from '../types';
import type {
GraphQLMock,
MockGraphQLFunction,
MockGraphQLObject,
} from '../types';

export class MockLink extends ApolloLink {
constructor(private mock: GraphQLMock) {
Expand All @@ -14,6 +18,15 @@ export class MockLink extends ApolloLink {
this.mock = mock;
}

updateMockWithMerge(mock: MockGraphQLObject) {
if (typeof this.mock === 'function') {
throw new Error(
"Can't merge a GraphQL function mock with a GraphQL object mock",
);
}
this.mock = {...this.mock, ...mock};
}

request(operation: Operation): Observable<any> {
return new Observable((obs) => {
const {mock} = this;
Expand Down
76 changes: 76 additions & 0 deletions packages/graphql-testing/src/tests/e2e.test.tsx
Expand Up @@ -344,6 +344,82 @@ describe('graphql-testing', () => {
expect(myComponent).toContainReactText('Garfield2');
});

it('allows for merged mock updates after it has been initialized', async () => {
const MyComponentWithTwoQueries = () => {
const queryResult = useQuery(personQuery, {variables: {id: '1'}});
const {data, refetch} = queryResult;

return (
<>
<div>{data?.person?.name}</div>{' '}
<button onClick={() => refetch()} type="button">
Refetch Person
</button>
<MyComponent id="123" />
</>
);
};

const mockPetQueryData = {pet: {__typename: 'Cat', name: 'Garfield'}};

const graphQL = createGraphQL({
Pet: mockPetQueryData,
});

const mockPersonQueryData = {
person: {__typename: 'Person', name: 'Jon Arbuckle'},
};

graphQL.updateWithMerge({
Person: mockPersonQueryData,
});

const myComponent = mount(
<ApolloProvider client={graphQL.client}>
<MyComponentWithTwoQueries />
</ApolloProvider>,
);

graphQL.wrap((resolve) => myComponent.act(resolve));
await graphQL.resolveAll();

expect(myComponent).toContainReactText('Garfield');
expect(myComponent).toContainReactText('Jon Arbuckle');

const newMockPetQueryData = {pet: {__typename: 'Cat', name: 'Garfield2'}};
graphQL.updateWithMerge({
// Partial update of pet mock
Pet: newMockPetQueryData,
});

const request = myComponent
.find('button', {children: 'Refetch!'})!
.trigger('onClick');
await graphQL.resolveAll();
await request;

expect(myComponent).toContainReactText('Garfield2');
expect(myComponent).toContainReactText('Jon Arbuckle');

const newMockPersonQueryData = {
person: {__typename: 'Person', name: 'Jon Arbuckle Jr.'},
};

graphQL.updateWithMerge({
// Partial update of person mock
Person: newMockPersonQueryData,
});

const personRequest = myComponent
.find('button', {children: 'Refetch Person'})!
.trigger('onClick');
await graphQL.resolveAll();
await personRequest;

expect(myComponent).toContainReactText('Garfield2');
expect(myComponent).toContainReactText('Jon Arbuckle Jr.');
});

it('handles fetchMore', async () => {
const graphQL = createGraphQL({
Pets: ({
Expand Down
9 changes: 6 additions & 3 deletions packages/graphql-testing/src/types.ts
Expand Up @@ -10,9 +10,12 @@
export type MockGraphQLFunction = (
request: GraphQLRequest,
) => MockGraphQLResponse;
export type GraphQLMock =
| {[key: string]: MockGraphQLResponse | MockGraphQLFunction}
| MockGraphQLFunction;

export type MockGraphQLObject = {

Check failure on line 14 in packages/graphql-testing/src/types.ts

View workflow job for this annotation

GitHub Actions / Test (Node 14, React 17)

Use an `interface` instead of a `type`

Check failure on line 14 in packages/graphql-testing/src/types.ts

View workflow job for this annotation

GitHub Actions / Test (Node 14, React 18)

Use an `interface` instead of a `type`

Check failure on line 14 in packages/graphql-testing/src/types.ts

View workflow job for this annotation

GitHub Actions / Test (Node 16, React 17)

Use an `interface` instead of a `type`

Check failure on line 14 in packages/graphql-testing/src/types.ts

View workflow job for this annotation

GitHub Actions / Test (Node 16, React 18)

Use an `interface` instead of a `type`

Check failure on line 14 in packages/graphql-testing/src/types.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 17)

Use an `interface` instead of a `type`

Check failure on line 14 in packages/graphql-testing/src/types.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 18)

Use an `interface` instead of a `type`
[key: string]: MockGraphQLResponse | MockGraphQLFunction;
};

export type GraphQLMock = MockGraphQLObject | MockGraphQLFunction;

export interface MockRequest {
operation: Operation;
Expand Down