-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathserver.test.tsx
207 lines (194 loc) · 5.17 KB
/
server.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/** @jest-environment node */
import React from 'react';
import {
print,
graphql as execute,
GraphQLSchema,
GraphQLObjectType,
GraphQLList,
GraphQLString,
GraphQLID
} from 'graphql';
import gql from 'graphql-tag';
import { ApolloClient } from '../../../../core';
import { InMemoryCache as Cache } from '../../../../cache';
import { ApolloProvider } from '../../../context';
import { ApolloLink } from '../../../../link/core';
import { Observable } from '../../../../utilities';
import { renderToStringWithData } from '../../../ssr';
import { Query } from '../../Query';
const planetMap = new Map([['Planet:1', { id: 'Planet:1', name: 'Tatooine' }]]);
const shipMap = new Map([
[
'Ship:2',
{
id: 'Ship:2',
name: 'CR90 corvette',
films: ['Film:4', 'Film:6', 'Film:3']
}
],
[
'Ship:3',
{
id: 'Ship:3',
name: 'Star Destroyer',
films: ['Film:4', 'Film:5', 'Film:6']
}
]
]);
const filmMap = new Map([
['Film:3', { id: 'Film:3', title: 'Revenge of the Sith' }],
['Film:4', { id: 'Film:4', title: 'A New Hope' }],
['Film:5', { id: 'Film:5', title: 'the Empire Strikes Back' }],
['Film:6', { id: 'Film:6', title: 'Return of the Jedi' }]
]);
const PlanetType = new GraphQLObjectType({
name: 'Planet',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString }
}
});
const FilmType = new GraphQLObjectType({
name: 'Film',
fields: {
id: { type: GraphQLID },
title: { type: GraphQLString }
}
});
const ShipType = new GraphQLObjectType({
name: 'Ship',
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
films: {
type: new GraphQLList(FilmType),
resolve: ({ films }) => films.map((id: string) => filmMap.get(id))
}
}
});
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
allPlanets: {
type: new GraphQLList(PlanetType),
resolve: () => Array.from(planetMap.values())
},
allShips: {
type: new GraphQLList(ShipType),
resolve: () => Array.from(shipMap.values())
},
ship: {
type: ShipType,
args: { id: { type: GraphQLID } },
resolve: (_, { id }) => shipMap.get(id)
},
film: {
type: FilmType,
args: { id: { type: GraphQLID } },
resolve: (_, { id }) => filmMap.get(id)
}
}
});
const Schema = new GraphQLSchema({ query: QueryType });
describe('SSR', () => {
it('should work with React.createContext', async () => {
let defaultValue = 'default';
let Context = React.createContext(defaultValue);
let providerValue = 'provider';
expect(
await renderToStringWithData(
<React.Fragment>
<Context.Provider value={providerValue} />
<Context.Consumer>
{val => {
expect(val).toBe(defaultValue);
return val;
}}
</Context.Consumer>
</React.Fragment>
)
).toBe(defaultValue);
expect(
await renderToStringWithData(
<Context.Provider value={providerValue}>
<Context.Consumer>
{val => {
expect(val).toBe(providerValue);
return val;
}}
</Context.Consumer>
</Context.Provider>
)
).toBe(providerValue);
expect(
await renderToStringWithData(
<Context.Consumer>
{val => {
expect(val).toBe(defaultValue);
return val;
}}
</Context.Consumer>
)
).toBe(defaultValue);
let ContextForUndefined = React.createContext<void | string>(defaultValue);
expect(
await renderToStringWithData(
<ContextForUndefined.Provider value={undefined}>
<ContextForUndefined.Consumer>
{val => {
expect(val).toBeUndefined();
return val === undefined ? 'works' : 'broken';
}}
</ContextForUndefined.Consumer>
</ContextForUndefined.Provider>
)
).toBe('works');
const apolloClient = new ApolloClient({
link: new ApolloLink(config => {
return new Observable(observer => {
execute({
schema: Schema,
source: print(config.query),
variableValues: config.variables,
operationName: config.operationName,
})
.then(result => {
observer.next(result);
observer.complete();
})
.catch(e => {
observer.error(e);
});
});
}),
cache: new Cache()
});
expect(
await renderToStringWithData(
<ApolloProvider client={apolloClient}>
<Context.Provider value={providerValue}>
<Query
query={gql`
query ShipIds {
allShips {
id
}
}
`}
>
{() => (
<Context.Consumer>
{val => {
expect(val).toBe(providerValue);
return val;
}}
</Context.Consumer>
)}
</Query>
</Context.Provider>
</ApolloProvider>
)
).toBe(providerValue);
});
});