-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathfilm.spec.ts
120 lines (111 loc) · 4.47 KB
/
film.spec.ts
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE-examples file in the root directory of this source tree.
*/
import { swapi } from './swapi';
function getDocument(query: string): string {
return `${query}
fragment AllFilmProperties on Film {
director
episodeID
openingCrawl
producers
releaseDate
title
characterConnection(first:1) { edges { node { name } } }
planetConnection(first:1) { edges { node { name } } }
speciesConnection(first:1) { edges { node { name } } }
starshipConnection(first:1) { edges { node { name } } }
vehicleConnection(first:1) { edges { node { name } } }
}
`;
}
describe('Film type', () => {
it('Gets an object by SWAPI ID', async () => {
const query = '{ film(filmID: 1) { title } }';
const result = await swapi(query);
expect(result.data?.film.title).toBe('A New Hope');
});
it('Gets a different object by SWAPI ID', async () => {
const query = '{ film(filmID: 2) { title } }';
const result = await swapi(query);
expect(result.data?.film.title).toBe('The Empire Strikes Back');
});
it('Gets an object by global ID', async () => {
const query = '{ film(filmID: 1) { id, title } }';
const result = await swapi(query);
const nextQuery = `{ film(id: "${result.data?.film.id}") { id, title } }`;
const nextResult = await swapi(nextQuery);
expect(result.data?.film.title).toBe('A New Hope');
expect(nextResult.data?.film.title).toBe('A New Hope');
expect(result.data?.film.id).toBe(nextResult.data?.film.id);
});
it('Gets an object by global ID with node', async () => {
const query = '{ film(filmID: 1) { id, title } }';
const result = await swapi(query);
const nextQuery = `{
node(id: "${result.data?.film.id}") {
... on Film {
id
title
}
}
}`;
const nextResult = await swapi(nextQuery);
expect(result.data?.film.title).toBe('A New Hope');
expect(nextResult.data?.node.title).toBe('A New Hope');
expect(result.data?.film.id).toBe(nextResult.data?.node.id);
});
it('Gets all properties', async () => {
const query = getDocument(
`{
film(filmID: 1) {
...AllFilmProperties
}
}`,
);
const result = await swapi(query);
const expected = {
title: 'A New Hope',
episodeID: 4,
openingCrawl:
"It is a period of civil war.\r\nRebel spaceships, striking\r\nfrom a hidden base, have won\r\ntheir first victory against\r\nthe evil Galactic Empire.\r\n\r\nDuring the battle, Rebel\r\nspies managed to steal secret\r\nplans to the Empire's\r\nultimate weapon, the DEATH\r\nSTAR, an armored space\r\nstation with enough power\r\nto destroy an entire planet.\r\n\r\nPursued by the Empire's\r\nsinister agents, Princess\r\nLeia races home aboard her\r\nstarship, custodian of the\r\nstolen plans that can save her\r\npeople and restore\r\nfreedom to the galaxy....",
director: 'George Lucas',
producers: ['Gary Kurtz', 'Rick McCallum'],
releaseDate: '1977-05-25',
speciesConnection: { edges: [{ node: { name: 'Human' } }] },
starshipConnection: { edges: [{ node: { name: 'CR90 corvette' } }] },
vehicleConnection: { edges: [{ node: { name: 'Sand Crawler' } }] },
characterConnection: { edges: [{ node: { name: 'Luke Skywalker' } }] },
planetConnection: { edges: [{ node: { name: 'Tatooine' } }] },
};
expect(result.data?.film).toMatchObject(expected);
});
it('All objects query', async () => {
const query = getDocument(
'{ allFilms { edges { cursor, node { ...AllFilmProperties } } } }',
);
const result = await swapi(query);
expect(result.data?.allFilms.edges.length).toBe(6);
});
it('Pagination query', async () => {
const query = `{
allFilms(first: 2) { edges { cursor, node { title } } }
}`;
const result = await swapi(query);
expect(
result.data?.allFilms.edges.map((e: any) => e.node.title),
).toMatchObject(['A New Hope', 'The Empire Strikes Back']);
const nextCursor = result.data?.allFilms.edges[1].cursor;
const nextQuery = `{ allFilms(first: 2, after:"${nextCursor}") {
edges { cursor, node { title } } }
}`;
const nextResult = await swapi(nextQuery);
expect(
nextResult.data?.allFilms.edges.map((e: any) => e.node.title),
).toMatchObject(['Return of the Jedi', 'The Phantom Menace']);
});
});