This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 786
/
index.test.tsx
604 lines (502 loc) · 20.5 KB
/
index.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import * as React from 'react';
import * as ReactDOM from 'react-dom/server';
import ApolloClient, { createNetworkInterface, createFragment } from 'apollo-client';
import { graphql, ApolloProvider } from '../../../src';
import { walkTree, getDataFromTree, renderToStringWithData } from '../../../src/server';
import 'isomorphic-fetch';
import gql from 'graphql-tag';
import * as _ from 'lodash';
import { mockNetworkInterface } from '../../../src/test-utils';
describe('SSR', () => {
// it('should render the expected markup', (done) => {
// const query = gql`query ssr { allPeople(first: 1) { people { name } } }`;
// const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
// const networkInterface = mockNetworkInterface({ request: { query }, result: { data } });
// const client = new ApolloClient({ networkInterface });
// const Element = ({ ssr }) => (<div>{ssr.loading ? 'loading' : 'loaded'}</div>);
// const WrappedElement = graphql(query)(Element);
// const component = (<ApolloProvider client={client}><WrappedElement /></ApolloProvider>);
// try {
// const markup = ReactDOM.renderToString(component);
// expect(markup).to.match(/loading/);
// // We do a timeout to ensure the rest of the application does not fail
// // after the render
// setTimeout(() => done(), 10);
// } catch (e) {
// done(e);
// }
// });
describe('`walkTree`', () => {
describe('traversal', () => {
it('basic element trees', () => {
let elementCount = 0;
const rootElement = <div><span>Foo</span><span>Bar</span></div>;
walkTree(rootElement, {}, (element) => { elementCount += 1 });
expect(elementCount).toEqual(5);
});
it('functional stateless components', () => {
let elementCount = 0;
const MyComponent = ({ n }) => <div>{_.times(n, (i) => <span key={i} />)}</div>;
walkTree(<MyComponent n={5}/>, {}, (element) => { elementCount += 1 });
expect(elementCount).toEqual(7);
});
it('functional stateless components with children', () => {
let elementCount = 0;
const MyComponent = ({ n, children=null }) =>
<div>{_.times(n, (i) => <span key={i} />)}{children}</div>;
walkTree(<MyComponent n={5}><span>Foo</span></MyComponent>, {},
(element) => { elementCount += 1 });
expect(elementCount).toEqual(9);
});
it('basic classes', () => {
let elementCount = 0;
class MyComponent extends React.Component<any, any> {
render() {
return <div>{_.times(this.props.n, (i) => <span key={i} />)}</div>;
}
}
walkTree(<MyComponent n={5}/>, {}, (element) => { elementCount += 1 });
expect(elementCount).toEqual(7);
});
it('basic classes with children', () => {
let elementCount = 0;
class MyComponent extends React.Component<any, any> {
render() {
return (
<div>
{_.times(this.props.n, (i) => <span key={i} />)}
{this.props.children}
</div>
);
}
}
walkTree(<MyComponent n={5}><span>Foo</span></MyComponent>, {},
(element) => { elementCount += 1 });
expect(elementCount).toEqual(9);
});
});
});
describe('`getDataFromTree`', () => {
it('should run through all of the queries that want SSR', () => {
const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const WrappedElement = graphql(query)(({ data }) => (
<div>{data.loading ? 'loading' : data.currentUser.firstName}</div>
));
const app = (<ApolloProvider client={apolloClient}><WrappedElement /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/James/);
});
});
it('should allow forceFetch as an option and still render prefetched data', () => {
const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const WrappedElement = graphql(query, { options: { forceFetch: true }})(({ data }) => (
<div>{data.loading ? 'loading' : data.currentUser.firstName}</div>
));
const app = (<ApolloProvider client={apolloClient}><WrappedElement /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/James/);
});
});
it('should pick up queries deep in the render tree', () => {
const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const WrappedElement = graphql(query)(({ data }) => (
<div>{data.loading ? 'loading' : data.currentUser.firstName}</div>
));
const Page = () => (<div><span>Hi</span><div><WrappedElement /></div></div>);
const app = (<ApolloProvider client={apolloClient}><Page/></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/James/);
});
});
it('should handle nested queries that depend on each other', () => {
const idQuery = gql`{ currentUser { id } }`;
const idData = { currentUser: { id: '1234' } };
const userQuery = gql`query getUser($id: String) { user(id: $id) { firstName } }`;
const variables = { id: '1234' };
const userData = { user: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query: idQuery }, result: { data: idData }, delay: 50 },
{ request: { query: userQuery, variables }, result: { data: userData }, delay: 50 },
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const withId = graphql(idQuery);
const withUser = graphql(userQuery, {
skip: ({ data: { loading } }) => loading,
options: ({ data }) => ({ variables: { id: data.currentUser.id } }),
});
const Component = ({ data }) => (
<div>{data.loading ? 'loading' : data.user.firstName}</div>
);
const WrappedComponent = withId(withUser(Component));
const app = (<ApolloProvider client={apolloClient}><WrappedComponent /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/James/);
});
});
it('should correctly skip queries (deprecated)', () => {
const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const WrappedElement = graphql(query, { options: { skip: true }})(({ data }) => (
<div>{data.loading ? 'loading' : 'skipped'}</div>
));
const app = (<ApolloProvider client={apolloClient}><WrappedElement /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/skipped/);
})
;
});
it('should correctly skip queries (deprecated)', () => {
const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const WrappedElement = graphql(query, { skip: true })(({ data }) => (
<div>{!data ? 'skipped' : 'dang'}</div>
));
const app = (<ApolloProvider client={apolloClient}><WrappedElement /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/skipped/);
})
;
});
it('should use the correct default props for a query', () => {
const query = gql`query user($id: ID) { currentUser(id: $id){ firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const variables = { id: 1 };
const networkInterface = mockNetworkInterface(
{ request: { query, variables }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const Element = graphql(query, { name: 'user' })(({ user }) => (
<div>{user.loading ? 'loading' : user.currentUser.firstName}</div>
));
const app = (<ApolloProvider client={apolloClient}><Element id={1} /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const initialState = apolloClient.store.getState();
expect(initialState.apollo.data).toBeTruthy();
expect(initialState.apollo.data['$ROOT_QUERY.currentUser({"id":1})']).toBeTruthy();
})
;
});
it('should allow for setting state in a component', (done) => {
const query = gql`query user($id: ID) { currentUser(id: $id){ firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const variables = { id: 1 };
const networkInterface = mockNetworkInterface(
{ request: { query, variables }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
@graphql(query, { name: 'user' })
class Element extends React.Component<any, any> {
state = { thing: 1 };
componentWillMount() {
this.setState({ thing: this.state.thing + 1 });
}
render(){
const { user } = this.props;
expect(this.state.thing).toBe(2);
return <div>{user.loading ? 'loading' : user.currentUser.firstName}</div>
}
}
const app = (<ApolloProvider client={apolloClient}><Element id={1} /></ApolloProvider>);
getDataFromTree(app)
.then(() => {
const initialState = apolloClient.store.getState();
expect(initialState.apollo.data).toBeTruthy();
expect(initialState.apollo.data['$ROOT_QUERY.currentUser({"id":1})']).toBeTruthy();
done();
})
.catch(console.error)
;
});
it('shouldn\'t run queries if ssr is turned to off', () => {
const query = gql`query user($id: ID) { currentUser(id: $id){ firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const variables = { id: 1 };
const networkInterface = mockNetworkInterface(
{ request: { query, variables }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const Element = graphql(query, {
name: 'user',
options: (props) => ({ variables: props, ssr: false })
})(({ user }) => (
<div>{user.loading ? 'loading' : user.currentUser.firstName}</div>
));
const app = (<ApolloProvider client={apolloClient}><Element id={1} /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const initialState = apolloClient.store.getState();
expect(initialState.apollo.queries).toEqual({});
expect(initialState.apollo.data).toEqual({});
})
;
});
it('should correctly handle SSR mutations', () => {
const query = gql`{ currentUser { firstName } }`;
const data1 = { currentUser: { firstName: 'James' } };
const mutation = gql`mutation { logRoutes { id } }`;
const mutationData= { logRoutes: { id: 'foo' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data: data1 }, delay: 5 },
{ request: { query: mutation }, result: { data: mutationData }, delay: 5 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const withQuery = graphql(query, {
options: (ownProps) => ({ ssr: true }),
props: ({ data }) => {
expect(data.refetch).toBeTruthy();
return {
refetchQuery: data.refetch,
data,
};
},
});
const withMutation = graphql(mutation, {
props: ({ ownProps, mutate }) => {
expect(ownProps.refetchQuery).toBeTruthy();
return {
action(variables) {
return mutate({ variables }).then(() => ownProps.refetchQuery());
},
};
},
});
const Element = (({ data }) => (
<div>{data.loading ? 'loading' : data.currentUser.firstName}</div>
));
const WrappedElement = withQuery(withMutation(Element));
const app = (<ApolloProvider client={apolloClient}><WrappedElement /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/James/);
})
;
});
it('should correctly handle SSR mutations, reverse order', () => {
const query = gql`{ currentUser { firstName } }`;
const data1 = { currentUser: { firstName: 'James' } };
const mutation = gql`mutation { logRoutes { id } }`;
const mutationData= { logRoutes: { id: 'foo' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data: data1 }, delay: 5 },
{ request: { query: mutation }, result: { data: mutationData }, delay: 5 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const withQuery = graphql(query, {
props: ({ ownProps, data }) => {
expect(ownProps.mutate).toBeTruthy();
return {
data,
};
},
});
const withMutation = graphql(mutation);
const Element = (({ data }) => (
<div>{data.loading ? 'loading' : data.currentUser.firstName}</div>
));
const WrappedElement = withMutation(withQuery(Element));
const app = (<ApolloProvider client={apolloClient}><WrappedElement /></ApolloProvider>);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/James/);
})
;
});
it('should not require `ApolloProvider` to be the root component', () => {
const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const WrappedElement = graphql(query)(({ data }) => (
<div>{data.loading ? 'loading' : data.currentUser.firstName}</div>
));
class MyRootContainer extends React.Component<any, any> {
constructor(props) {
super(props);
this.state = { color: 'purple' };
}
getChildContext() {
return { color: this.state.color };
}
render() {
return <div>{this.props.children}</div>;
}
}
(MyRootContainer as any).childContextTypes = {
color: React.PropTypes.string,
};
const app = (
<MyRootContainer>
<ApolloProvider client={apolloClient}>
<WrappedElement />
</ApolloProvider>
</MyRootContainer>
);
return getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).toMatch(/James/);
})
;
});
});
describe('`renderToStringWithData`', () => {
// XXX break into smaller tests
// XXX mock all queries
it('should work on a non trivial example', function() {
// this.timeout(10000);
const networkInterface = createNetworkInterface({
uri: 'http://graphql-swapi.parseapp.com/',
});
const apolloClient = new ApolloClient({ networkInterface });
@graphql(gql`
query data($id: ID!) { film: node(id: $id) { ... on Film { title } } }
`)
class Film extends React.Component<any, any> {
render() {
const { data } = this.props;
if (data.loading) return null;
const { film } = data;
return <h6>{film.title}</h6>;
}
};
@graphql(gql`
query data($id: ID!) {
ship: node(id: $id) { ... on Starship { name, filmConnection { films { id } } } }
}
`)
class Starship extends React.Component<any, any> {
render() {
const { data } = this.props;
if (data.loading) return null;
const { ship } = data;
return (
<div>
<h4>{ship.name} appeared in the following flims:</h4>
<br/>
<ul>
{ship.filmConnection.films.map((film, key) => (
<li key={key}>
<Film id={film.id} />
</li>
))}
</ul>
</div>
);
}
};
@graphql(gql`query data { allStarships(first: 2) { starships { id } } }`)
class AllShips extends React.Component<any, any> {
render() {
const { data } = this.props;
return (
<ul>
{!data.loading && data.allStarships && data.allStarships.starships.map((ship, key) => (
<li key={key}><Starship id={ship.id} /></li>
))}
</ul>
);
}
}
@graphql(gql`query data { allPlanets(first: 1) { planets { name } } }`)
class AllPlanets extends React.Component<any, any> {
render() {
const { data } = this.props;
if (data.loading) return null;
const { planets } = data.allPlanets;
return (
<div>
<h1>Planets</h1>
{planets.map((planet, key) => (
<div key={key}>{planet.name}</div>
))}
</div>
);
}
}
const Bar = () => (<div><h2>Bar</h2><AllPlanets /></div>)
const Foo = () => (<div><h1>Foo</h1><Bar /></div>);
const app = (
<ApolloProvider client={apolloClient}>
<div>
<Foo />
<hr />
<AllShips />
</div>
</ApolloProvider>
);
return renderToStringWithData(app)
.then((markup) => {
expect(markup).toMatch(/CR90 corvette/);
expect(markup).toMatch(/Return of the Jedi/);
expect(markup).toMatch(/A New Hope/);
expect(markup).toMatch(/Planets/);
expect(markup).toMatch(/Tatooine/);
});
});
it('should work with queries that use fragments', function() {
const query = gql`{ currentUser { __typename, ...userInfo } }`;
const userInfoFragment = createFragment(gql`fragment userInfo on User { firstName, lastName }`);
const data = { currentUser: { __typename: 'User', firstName: 'John', lastName: 'Smith' } };
const networkInterface = {
query: () => Promise.resolve({ data }),
};
const apolloClient = new ApolloClient({ networkInterface, addTypename: false });
const UserPage = graphql(query, {
options: {
fragments: userInfoFragment
}
})(({ data }) => (
<div>{data.loading ? 'Loading...' : `${data.currentUser.firstName} ${data.currentUser.lastName}`}</div>
));
const app = (
<ApolloProvider client={apolloClient}>
<UserPage />
</ApolloProvider>
);
return renderToStringWithData(app)
.then((markup) => {
expect(markup).toMatch(/John Smith/);
});
});
});
});