-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathfragments.test.tsx
122 lines (110 loc) · 3.05 KB
/
fragments.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
import React from "react";
import { render, waitFor } from "@testing-library/react";
import gql from "graphql-tag";
import { DocumentNode } from "graphql";
import { ApolloClient } from "../../../core";
import { ApolloProvider } from "../../context";
import { InMemoryCache as Cache } from "../../../cache";
import { itAsync, mockSingleLink } from "../../../testing";
import { graphql } from "../graphql";
import { ChildProps } from "../types";
describe("fragments", () => {
// XXX in a later version, we should support this for composition
it("throws if you only pass a fragment", () => {
const query: DocumentNode = gql`
fragment Failure on PeopleConnection {
people {
name
}
}
`;
const expectedData = {
allPeople: { people: [{ name: "Luke Skywalker" }] },
};
type Data = typeof expectedData;
const link = mockSingleLink({
request: { query },
result: { data: expectedData },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});
try {
const Container = graphql<{}, Data>(query)(
class extends React.Component<ChildProps<{}, Data>> {
componentDidUpdate() {
const { props } = this;
expect(props.data!.loading).toBeFalsy();
expect(props.data!.allPeople).toEqual(expectedData.allPeople);
}
render() {
return null;
}
}
);
render(
<ApolloProvider client={client}>
<Container />
</ApolloProvider>
);
throw new Error();
} catch (e) {
expect((e as Error).name).toMatch(/Invariant Violation/);
}
});
itAsync(
"correctly fetches a query with inline fragments",
(resolve, reject) => {
let done = false;
const query: DocumentNode = gql`
query people {
allPeople(first: 1) {
__typename
...person
}
}
fragment person on PeopleConnection {
people {
name
}
}
`;
const data = {
allPeople: {
__typename: "PeopleConnection",
people: [{ name: "Luke Skywalker" }],
},
};
type Data = typeof data;
const link = mockSingleLink({
request: { query },
result: { data },
});
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});
const Container = graphql<{}, Data>(query)(
class extends React.Component<ChildProps<{}, Data>> {
componentDidUpdate() {
expect(this.props.data!.loading).toBeFalsy();
expect(this.props.data!.allPeople).toEqual(data.allPeople);
done = true;
}
render() {
return null;
}
}
);
render(
<ApolloProvider client={client}>
<Container />
</ApolloProvider>
);
waitFor(() => {
expect(done).toBe(true);
}).then(resolve, reject);
}
);
});