Skip to content

Commit

Permalink
Fix tests and capture/check invariant.error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jun 22, 2021
1 parent d11ea15 commit 4b4a9b8
Show file tree
Hide file tree
Showing 29 changed files with 768 additions and 121 deletions.
68 changes: 32 additions & 36 deletions src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Observable } from '../utilities';
import { ApolloLink } from '../link/core';
import { HttpLink } from '../link/http';
import { InMemoryCache } from '../cache';
import { stripSymbols } from '../testing';
import { stripSymbols, withErrorSpy } from '../testing';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';

describe('ApolloClient', () => {
Expand Down Expand Up @@ -834,7 +834,7 @@ describe('ApolloClient', () => {
});
});

it('should warn when the data provided does not match the query shape', () => {
withErrorSpy(it, 'should warn when the data provided does not match the query shape', () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache({
Expand All @@ -843,28 +843,26 @@ describe('ApolloClient', () => {
}),
});

expect(() => {
client.writeQuery({
data: {
todos: [
{
id: '1',
name: 'Todo 1',
__typename: 'Todo',
},
],
},
query: gql`
query {
todos {
id
name
description
}
client.writeQuery({
data: {
todos: [
{
id: '1',
name: 'Todo 1',
__typename: 'Todo',
},
],
},
query: gql`
query {
todos {
id
name
description
}
`,
});
}).toThrowError(/Missing field 'description' /);
}
`,
});
});
});

Expand Down Expand Up @@ -1119,7 +1117,7 @@ describe('ApolloClient', () => {
});
});

it('should warn when the data provided does not match the fragment shape', () => {
withErrorSpy(it, 'should warn when the data provided does not match the fragment shape', () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache({
Expand All @@ -1128,18 +1126,16 @@ describe('ApolloClient', () => {
}),
});

expect(() => {
client.writeFragment({
data: { __typename: 'Bar', i: 10 },
id: 'bar',
fragment: gql`
fragment fragmentBar on Bar {
i
e
}
`,
});
}).toThrowError(/Missing field 'e' /);
client.writeFragment({
data: { __typename: 'Bar', i: 10 },
id: 'bar',
fragment: gql`
fragment fragmentBar on Bar {
i
e
}
`,
});
});

describe('change will call observable next', () => {
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/__snapshots__/ApolloClient.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ Object {
}
`;

exports[`ApolloClient writeFragment should warn when the data provided does not match the fragment shape 1`] = `
[MockFunction] {
"calls": Array [
Array [
"Missing field 'e' while writing result {
\\"__typename\\": \\"Bar\\",
\\"i\\": 10
}",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`ApolloClient writeFragment will write some deeply nested data into the store at any id 1`] = `
Object {
"__META": Object {
Expand Down Expand Up @@ -359,6 +378,26 @@ Object {
}
`;

exports[`ApolloClient writeQuery should warn when the data provided does not match the query shape 1`] = `
[MockFunction] {
"calls": Array [
Array [
"Missing field 'description' while writing result {
\\"id\\": \\"1\\",
\\"name\\": \\"Todo 1\\",
\\"__typename\\": \\"Todo\\"
}",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`ApolloClient writeQuery will write some deeply nested data to the store 1`] = `
Object {
"ROOT_QUERY": Object {
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/__snapshots__/client.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,24 @@ Object {
},
}
`;

exports[`client should warn if server returns wrong data 1`] = `
[MockFunction] {
"calls": Array [
Array [
"Missing field 'description' while writing result {
\\"id\\": \\"1\\",
\\"name\\": \\"Todo 1\\",
\\"price\\": 100,
\\"__typename\\": \\"Todo\\"
}",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;
20 changes: 20 additions & 0 deletions src/__tests__/__snapshots__/mutationResults.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mutation results should warn when the result fields don't match the query fields 1`] = `
[MockFunction] {
"calls": Array [
Array [
"Missing field 'description' while writing result {
\\"id\\": \\"2\\",
\\"name\\": \\"Todo 2\\",
\\"__typename\\": \\"createTodo\\"
}",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`mutation results should write results to cache according to errorPolicy 1`] = `Object {}`;

exports[`mutation results should write results to cache according to errorPolicy 2`] = `
Expand Down
35 changes: 21 additions & 14 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
stripSymbols,
subscribeAndCount,
mockSingleLink,
withErrorSpy,
} from '../testing';

describe('client', () => {
Expand Down Expand Up @@ -2081,6 +2082,7 @@ describe('client', () => {
resolve();
});
});

itAsync('should allow errors to be returned from a mutation', (resolve, reject) => {
const mutation = gql`
mutation {
Expand All @@ -2102,7 +2104,12 @@ describe('client', () => {
const client = new ApolloClient({
link: mockSingleLink({
request: { query: mutation },
result: { data, errors },
result: {
errors,
data: {
newPerson: data,
},
},
}).setOnError(reject),
cache: new InMemoryCache({ addTypename: false }),
});
Expand All @@ -2112,7 +2119,9 @@ describe('client', () => {
expect(result.errors).toBeDefined();
expect(result.errors!.length).toBe(1);
expect(result.errors![0].message).toBe(errors[0].message);
expect(result.data).toEqual(data);
expect(result.data).toEqual({
newPerson: data,
});
resolve();
})
.catch((error: ApolloError) => {
Expand All @@ -2132,9 +2141,11 @@ describe('client', () => {
}
`;
const data = {
person: {
firstName: 'John',
lastName: 'Smith',
newPerson: {
person: {
firstName: 'John',
lastName: 'Smith',
},
},
};
const errors = [new Error('Some kind of GraphQL error.')];
Expand Down Expand Up @@ -2631,7 +2642,7 @@ describe('client', () => {
}).then(resolve, reject);
});

itAsync('should warn if server returns wrong data', (resolve, reject) => {
withErrorSpy(itAsync, 'should warn if server returns wrong data', (resolve, reject) => {
const query = gql`
query {
todos {
Expand All @@ -2654,6 +2665,7 @@ describe('client', () => {
],
},
};

const link = mockSingleLink({
request: { query },
result,
Expand All @@ -2666,14 +2678,9 @@ describe('client', () => {
}),
});

return client.query({ query }).then(
result => {
fail("should have errored");
},
error => {
expect(error.message).toMatch(/Missing field 'description' /);
},
).then(resolve, reject);
return client.query({ query }).then(({ data }) => {
expect(data).toEqual(result.data);
}).then(resolve, reject);
});

itAsync('runs a query with the connection directive and writes it to the store key defined in the directive', (resolve, reject) => {
Expand Down
Loading

0 comments on commit 4b4a9b8

Please sign in to comment.