Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Merge 4e694f9 into 8f339f1
Browse files Browse the repository at this point in the history
  • Loading branch information
rosskevin committed Dec 28, 2017
2 parents 8f339f1 + 4e694f9 commit d98aa8b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 203 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -29,6 +29,7 @@ first three params (`TChildProps` can be derived). [#1402](https://github.com/ap
- Minify umd and ensure umd name consistency [#1469](https://github.com/apollographql/react-apollo/pull/1469)
- Converted `test/test-utils/test-utils.test.js` to `test/test-utils.test.tsx` [#1475](https://github.com/apollographql/react-apollo/pull/1475)
- Updates to `examples/typescript` [#1471](https://github.com/apollographql/react-apollo/pull/1471)
- Mutation test cleanup [#1480](https://github.com/apollographql/react-apollo/pull/1480)

### 2.0.4
- rolled back on the lodash-es changes from
Expand Down
119 changes: 18 additions & 101 deletions test/client/graphql/mutations/index.test.tsx
@@ -1,48 +1,42 @@
import * as React from 'react';
import * as renderer from 'react-test-renderer';
import gql from 'graphql-tag';
import ApolloClient from 'apollo-client';
import { InMemoryCache as Cache } from 'apollo-cache-inmemory';
import { mockSingleLink } from '../../../../src/test-utils';
import {
ApolloProvider,
ChildProps,
graphql,
MutateProps,
MutationFunc,
} from '../../../../src';

import stripSymbols from '../../../test-utils/stripSymbols';
import createClient from '../../../test-utils/createClient';

const query = gql`
mutation addPerson {
allPeople(first: 1) {
people {
name
}
}
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};

describe('[mutations]', () => {
describe('graphql(mutation)', () => {
let error;
let client;
beforeEach(() => {
error = console.error;
console.error = jest.fn(() => {}); // tslint:disable-line
client = createClient(expectedData, query);
});
afterEach(() => {
console.error = error;
});

it('binds a mutation to props', () => {
const query = gql`
mutation addPerson {
allPeople(first: 1) {
people {
name
}
}
}
`;
const link = mockSingleLink({
request: { query },
result: { data: { allPeople: { people: [{ name: 'Luke Skywalker' }] } } },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});

const ContainerWithData = graphql(query)(({ mutate }: MutateProps) => {
expect(mutate).toBeTruthy();
expect(typeof mutate).toBe('function');
Expand All @@ -57,24 +51,6 @@ describe('[mutations]', () => {
});

it('binds a mutation to custom props', () => {
const query = gql`
mutation addPerson {
allPeople(first: 1) {
people {
name
}
}
}
`;
const link = mockSingleLink({
request: { query },
result: { data: { allPeople: { people: [{ name: 'Luke Skywalker' }] } } },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});

interface Props {
methodName: string;
}
Expand All @@ -101,23 +77,6 @@ describe('[mutations]', () => {
});

it('does not swallow children errors', done => {
const query = gql`
mutation addPerson {
allPeople(first: 1) {
people {
name
}
}
}
`;
const link = mockSingleLink({
request: { query },
result: { data: { allPeople: { people: [{ name: 'Luke Skywalker' }] } } },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});
let bar;
const ContainerWithData = graphql(query)(() => {
bar(); // this will throw
Expand Down Expand Up @@ -146,27 +105,6 @@ describe('[mutations]', () => {
});

it('can execute a mutation', done => {
const query = gql`
mutation addPerson {
allPeople(first: 1) {
people {
name
}
}
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};
const link = mockSingleLink({
request: { query },
result: { data: expectedData },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});

@graphql(query)
class Container extends React.Component<any, any> {
componentDidMount() {
Expand All @@ -188,28 +126,7 @@ describe('[mutations]', () => {
});

it('can execute a mutation with variables from props', done => {
const query = gql`
mutation addPerson($id: Int) {
allPeople(id: $id) {
people {
name
}
}
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};
const variables = { id: 1 };
const link = mockSingleLink({
request: { query, variables },
result: { data: expectedData },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});

client = createClient(expectedData, query, { id: 1 });
@graphql(query)
class Container extends React.Component<any, any> {
componentDidMount() {
Expand Down
111 changes: 20 additions & 91 deletions test/client/graphql/mutations/lifecycle.test.tsx
@@ -1,36 +1,26 @@
import * as React from 'react';
import * as renderer from 'react-test-renderer';
import gql from 'graphql-tag';
import ApolloClient from 'apollo-client';
import { InMemoryCache as Cache } from 'apollo-cache-inmemory';
import { mockSingleLink } from '../../../../src/test-utils';
import { ApolloProvider, graphql } from '../../../../src';

import stripSymbols from '../../../test-utils/stripSymbols';
import createClient from '../../../test-utils/createClient';

describe('[mutations] lifecycle', () => {
it('allows falsy values in the mapped variables from props', done => {
const query = gql`
mutation addPerson($id: Int) {
allPeople(id: $id) {
people {
name
}
}
const query = gql`
mutation addPerson($id: Int) {
allPeople(id: $id) {
people {
name
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};
const variables = { id: null };
const link = mockSingleLink({
request: { query, variables },
result: { data: expectedData },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});
}
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};

describe('graphql(mutation) lifecycle', () => {
it('allows falsy values in the mapped variables from props', done => {
const client = createClient(expectedData, query, { id: null });

@graphql(query)
class Container extends React.Component<any, any> {
Expand All @@ -40,6 +30,7 @@ describe('[mutations] lifecycle', () => {
done();
});
}

render() {
return null;
}
Expand All @@ -53,29 +44,8 @@ describe('[mutations] lifecycle', () => {
});

it("errors if the passed props don't contain the needed variables", () => {
const query = gql`
mutation addPerson($first: Int) {
allPeople(first: $first) {
people {
name
}
}
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};
const variables = { first: 1 };
const link = mockSingleLink({
request: { query, variables },
result: { data: expectedData },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});
const client = createClient(expectedData, query, { first: 1 });
const Container = graphql(query)(() => null);

try {
renderer.create(
<ApolloProvider client={client}>
Expand All @@ -88,27 +58,7 @@ describe('[mutations] lifecycle', () => {
});

it('rebuilds the mutation on prop change when using `options`', done => {
const query = gql`
mutation addPerson {
allPeople(first: 1) {
people {
name
}
}
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};
const link = mockSingleLink({
request: { query },
result: { data: expectedData },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});

const client = createClient(expectedData, query);
function options(props) {
// expect(props.listId).toBe(2);
return {};
Expand Down Expand Up @@ -144,28 +94,7 @@ describe('[mutations] lifecycle', () => {
});

it('can execute a mutation with custom variables', done => {
const query = gql`
mutation addPerson($id: Int) {
allPeople(id: $id) {
people {
name
}
}
}
`;
const expectedData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};
const variables = { id: 1 };
const link = mockSingleLink({
request: { query, variables },
result: { data: expectedData },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});

const client = createClient(expectedData, query, { id: 1 });
@graphql(query)
class Container extends React.Component<any, any> {
componentDidMount() {
Expand Down
14 changes: 4 additions & 10 deletions test/client/graphql/mutations/queries.test.tsx
Expand Up @@ -11,10 +11,11 @@ import {
MutationFunc,
} from '../../../../src';
import stripSymbols from '../../../test-utils/stripSymbols';
import createClient from '../../../test-utils/createClient';

const compose = require('lodash/flowRight');

describe('[mutations] query integration', () => {
describe('graphql(mutation) query integration', () => {
it('allows for passing optimisticResponse for a mutation', done => {
const query = gql`
mutation createTodo {
Expand All @@ -37,14 +38,7 @@ describe('[mutations] query integration', () => {
completed: true,
},
};

const link = mockSingleLink({
request: { query },
result: { data },
});
const cache = new Cache({ addTypename: false });
const client = new ApolloClient({ link, cache });

const client = createClient(data, query);
@graphql(query)
class Container extends React.Component<any, any> {
componentDidMount() {
Expand All @@ -62,7 +56,7 @@ describe('[mutations] query integration', () => {
done();
});

const dataInStore = cache.extract(true);
const dataInStore = client.cache.extract(true);
expect(stripSymbols(dataInStore['Todo:99'])).toEqual(
optimisticResponse.createTodo,
);
Expand Down
2 changes: 1 addition & 1 deletion test/client/graphql/mutations/recycled-queries.test.tsx
Expand Up @@ -7,7 +7,7 @@ import { mockSingleLink } from '../../../../src/test-utils';
import { ApolloProvider, graphql } from '../../../../src';
import stripSymbols from '../../../test-utils/stripSymbols';

describe('[mutations] update queries', () => {
describe('graphql(mutation) update queries', () => {
// This is a long test that keeps track of a lot of stuff. It is testing
// whether or not the `updateQueries` reducers will run even when a given
// container component is unmounted.
Expand Down

0 comments on commit d98aa8b

Please sign in to comment.