-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathlifecycle.test.tsx
150 lines (130 loc) · 3.67 KB
/
lifecycle.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React from "react";
import { render } from "@testing-library/react";
import gql from "graphql-tag";
import { ApolloProvider } from "../../../context/ApolloProvider";
import { itAsync, createMockClient } from "../../../../testing/core";
import { graphql } from "../../graphql";
import { ChildProps } from "../../types";
const query = gql`
mutation addPerson($id: Int) {
allPeople(id: $id) {
people {
name
}
}
}
`;
const expectedData = {
allPeople: { people: [{ name: "Luke Skywalker" }] },
};
describe("graphql(mutation) lifecycle", () => {
itAsync(
"allows falsy values in the mapped variables from props",
(resolve, reject) => {
const client = createMockClient(expectedData, query, { id: null });
interface Props {
id: string | null;
}
const Container = graphql<Props>(query)(
class extends React.Component<ChildProps<Props>> {
componentDidMount() {
this.props.mutate!().then((result) => {
expect(result && result.data).toEqual(expectedData);
resolve();
});
}
render() {
return null;
}
}
);
render(
<ApolloProvider client={client}>
<Container id={null} />
</ApolloProvider>
);
}
);
it("errors if the passed props don't contain the needed variables", () => {
const client = createMockClient(expectedData, query, { first: 1 });
interface Props {
frst: number;
}
const Container = graphql<Props>(query)(() => null);
try {
render(
<ApolloProvider client={client}>
<Container frst={1} />
</ApolloProvider>
);
} catch (e) {
expect(e).toMatch(/Invariant Violation: The operation 'addPerson'/);
}
});
itAsync(
"rebuilds the mutation on prop change when using `options`",
(resolve, reject) => {
const client = createMockClient(expectedData, query, {
id: 2,
});
interface Props {
listId: number;
}
function options(props: Props) {
return {
variables: {
id: props.listId,
},
};
}
class Container extends React.Component<ChildProps<Props>> {
render() {
if (this.props.listId !== 2) return null;
setTimeout(() => {
this.props.mutate!().then(() => resolve());
});
return null;
}
}
const ContainerWithMutate = graphql<Props>(query, { options })(Container);
class ChangingProps extends React.Component<{}, { listId: number }> {
state = { listId: 1 };
componentDidMount() {
setTimeout(() => this.setState({ listId: 2 }), 50);
}
render() {
return <ContainerWithMutate listId={this.state.listId} />;
}
}
render(
<ApolloProvider client={client}>
<ChangingProps />
</ApolloProvider>
);
}
);
itAsync("can execute a mutation with custom variables", (resolve, reject) => {
const client = createMockClient(expectedData, query, { id: 1 });
interface Variables {
id: number;
}
const Container = graphql<{}, {}, Variables>(query)(
class extends React.Component<ChildProps<{}, {}, Variables>> {
componentDidMount() {
this.props.mutate!({ variables: { id: 1 } }).then((result) => {
expect(result && result.data).toEqual(expectedData);
resolve();
});
}
render() {
return null;
}
}
);
render(
<ApolloProvider client={client}>
<Container />
</ApolloProvider>
);
});
});