forked from graphql/swapi-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson.spec.js
141 lines (129 loc) · 4.36 KB
/
person.spec.js
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
/**
* 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) {
return `${query}
fragment AllPersonProperties on Person {
birthYear
eyeColor
gender
hairColor
height
homeworld { name }
mass
name
skinColor
species { name }
filmConnection(first:1) { edges { node { title } } }
starshipConnection(first:1) { edges { node { name } } }
vehicleConnection(first:1) { edges { node { name } } }
}
`;
}
describe('Person type', () => {
it('Gets an object by SWAPI ID', async () => {
const query = '{ person(personID: 1) { name } }';
const result = await swapi(query);
expect(result.data.person.name).toBe('Luke Skywalker');
});
it('Gets a different object by SWAPI ID', async () => {
const query = '{ person(personID: 2) { name } }';
const result = await swapi(query);
expect(result.data.person.name).toBe('C-3PO');
});
it('Gets an object by global ID', async () => {
const query = '{ person(personID: 1) { id, name } }';
const result = await swapi(query);
const nextQuery = `{ person(id: "${result.data.person.id}") { id, name } }`;
const nextResult = await swapi(nextQuery);
expect(result.data.person.name).toBe('Luke Skywalker');
expect(nextResult.data.person.name).toBe('Luke Skywalker');
expect(result.data.person.id).toBe(nextResult.data.person.id);
});
it('Gets an object by global ID with node', async () => {
const query = '{ person(personID: 1) { id, name } }';
const result = await swapi(query);
const nextQuery = `{
node(id: "${result.data.person.id}") {
... on Person {
id
name
}
}
}`;
const nextResult = await swapi(nextQuery);
expect(result.data.person.name).toBe('Luke Skywalker');
expect(nextResult.data.node.name).toBe('Luke Skywalker');
expect(result.data.person.id).toBe(nextResult.data.node.id);
});
it('Gets all properties', async () => {
const query = getDocument(
`{
person(personID: 1) {
...AllPersonProperties
}
}`,
);
const result = await swapi(query);
const expected = {
name: 'Luke Skywalker',
birthYear: '19BBY',
eyeColor: 'blue',
gender: 'male',
hairColor: 'blond',
height: 172,
mass: 77,
skinColor: 'fair',
homeworld: { name: 'Tatooine' },
filmConnection: { edges: [{ node: { title: 'A New Hope' } }] },
species: null,
starshipConnection: { edges: [{ node: { name: 'X-wing' } }] },
vehicleConnection: { edges: [{ node: { name: 'Snowspeeder' } }] },
};
expect(result.data.person).toMatchObject(expected);
});
it('All objects query', async () => {
const query = getDocument(
'{ allPeople { edges { cursor, node { ...AllPersonProperties } } } }',
);
const result = await swapi(query);
expect(result.data.allPeople.edges.length).toBe(82);
});
it('Pagination query', async () => {
const query = `{
allPeople(first: 2) { edges { cursor, node { name } } }
}`;
const result = await swapi(query);
expect(result.data.allPeople.edges.map(e => e.node.name)).toMatchObject([
'Luke Skywalker',
'C-3PO',
]);
const nextCursor = result.data.allPeople.edges[1].cursor;
const nextQuery = `{ allPeople(first: 2, after:"${nextCursor}") {
edges { cursor, node { name } } }
}`;
const nextResult = await swapi(nextQuery);
expect(
nextResult.data.allPeople.edges.map(e => e.node.name),
).toMatchObject(['R2-D2', 'Darth Vader']);
});
describe('Edge cases', () => {
it('Returns null if no species is set', async () => {
const query = '{ person(personID: 42) { name, species { name } } }';
const result = await swapi(query);
expect(result.data.person.name).toBe('Quarsh Panaka');
expect(result.data.person.species).toBe(null);
});
it('Returns correctly if a species is set', async () => {
const query = '{ person(personID: 67) { name, species { name } } }';
const result = await swapi(query);
expect(result.data.person.name).toBe('Dooku');
expect(result.data.person.species.name).toBe('Human');
});
});
});