Skip to content

Commit e45fb12

Browse files
tests: Improve type coverage (#2655)
Based on #2609
1 parent 012f9e2 commit e45fb12

13 files changed

+118
-93
lines changed

src/__testUtils__/__tests__/genFuzzStrings-test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { describe, it } from 'mocha';
55

66
import genFuzzStrings from '../genFuzzStrings';
77

8-
function expectFuzzStrings(options) {
8+
function expectFuzzStrings(options: {|
9+
allowedChars: Array<string>,
10+
maxLength: number,
11+
|}) {
912
return expect(Array.from(genFuzzStrings(options)));
1013
}
1114

src/__tests__/starWarsData.js

+45-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
// @flow strict
22

3+
/**
4+
* These are types which correspond to the schema.
5+
* They represent the shape of the data visited during field resolution.
6+
*/
7+
export type Character = {
8+
id: string,
9+
name: string,
10+
friends: Array<string>,
11+
appearsIn: Array<number>,
12+
...
13+
};
14+
15+
export type Human = {|
16+
type: 'Human',
17+
id: string,
18+
name: string,
19+
friends: Array<string>,
20+
appearsIn: Array<number>,
21+
homePlanet?: string,
22+
|};
23+
24+
export type Droid = {|
25+
type: 'Droid',
26+
id: string,
27+
name: string,
28+
friends: Array<string>,
29+
appearsIn: Array<number>,
30+
primaryFunction: string,
31+
|};
32+
333
/**
434
* This defines a basic set of data for our Star Wars Schema.
535
*
@@ -8,7 +38,7 @@
838
* JSON objects in a more complex demo.
939
*/
1040

11-
const luke = {
41+
const luke: Human = {
1242
type: 'Human',
1343
id: '1000',
1444
name: 'Luke Skywalker',
@@ -17,7 +47,7 @@ const luke = {
1747
homePlanet: 'Tatooine',
1848
};
1949

20-
const vader = {
50+
const vader: Human = {
2151
type: 'Human',
2252
id: '1001',
2353
name: 'Darth Vader',
@@ -26,15 +56,15 @@ const vader = {
2656
homePlanet: 'Tatooine',
2757
};
2858

29-
const han = {
59+
const han: Human = {
3060
type: 'Human',
3161
id: '1002',
3262
name: 'Han Solo',
3363
friends: ['1000', '1003', '2001'],
3464
appearsIn: [4, 5, 6],
3565
};
3666

37-
const leia = {
67+
const leia: Human = {
3868
type: 'Human',
3969
id: '1003',
4070
name: 'Leia Organa',
@@ -43,23 +73,23 @@ const leia = {
4373
homePlanet: 'Alderaan',
4474
};
4575

46-
const tarkin = {
76+
const tarkin: Human = {
4777
type: 'Human',
4878
id: '1004',
4979
name: 'Wilhuff Tarkin',
5080
friends: ['1001'],
5181
appearsIn: [4],
5282
};
5383

54-
const humanData = {
84+
const humanData: {| [id: string]: Human |} = {
5585
'1000': luke,
5686
'1001': vader,
5787
'1002': han,
5888
'1003': leia,
5989
'1004': tarkin,
6090
};
6191

62-
const threepio = {
92+
const threepio: Droid = {
6393
type: 'Droid',
6494
id: '2000',
6595
name: 'C-3PO',
@@ -68,7 +98,7 @@ const threepio = {
6898
primaryFunction: 'Protocol',
6999
};
70100

71-
const artoo = {
101+
const artoo: Droid = {
72102
type: 'Droid',
73103
id: '2001',
74104
name: 'R2-D2',
@@ -77,53 +107,25 @@ const artoo = {
77107
primaryFunction: 'Astromech',
78108
};
79109

80-
const droidData = {
110+
const droidData: {| [id: string]: Droid |} = {
81111
'2000': threepio,
82112
'2001': artoo,
83113
};
84114

85-
/**
86-
* These are Flow types which correspond to the schema.
87-
* They represent the shape of the data visited during field resolution.
88-
*/
89-
export type Character = {
90-
id: string,
91-
name: string,
92-
friends: Array<string>,
93-
appearsIn: Array<number>,
94-
...
95-
};
96-
97-
export type Human = {|
98-
type: 'Human',
99-
id: string,
100-
name: string,
101-
friends: Array<string>,
102-
appearsIn: Array<number>,
103-
homePlanet: string,
104-
|};
105-
106-
export type Droid = {|
107-
type: 'Droid',
108-
id: string,
109-
name: string,
110-
friends: Array<string>,
111-
appearsIn: Array<number>,
112-
primaryFunction: string,
113-
|};
114-
115115
/**
116116
* Helper function to get a character by ID.
117117
*/
118-
function getCharacter(id) {
118+
function getCharacter(id: string): Promise<Character | null> {
119119
// Returning a promise just to illustrate that GraphQL.js supports it.
120120
return Promise.resolve(humanData[id] ?? droidData[id]);
121121
}
122122

123123
/**
124124
* Allows us to query for a character's friends.
125125
*/
126-
export function getFriends(character: Character): Array<Promise<Character>> {
126+
export function getFriends(
127+
character: Character,
128+
): Array<Promise<Character | null>> {
127129
// Notice that GraphQL accepts Arrays of Promises.
128130
return character.friends.map((id) => getCharacter(id));
129131
}
@@ -143,13 +145,13 @@ export function getHero(episode: number): Character {
143145
/**
144146
* Allows us to query for the human with the given id.
145147
*/
146-
export function getHuman(id: string): Human {
148+
export function getHuman(id: string): Human | null {
147149
return humanData[id];
148150
}
149151

150152
/**
151153
* Allows us to query for the droid with the given id.
152154
*/
153-
export function getDroid(id: string): Droid {
155+
export function getDroid(id: string): Droid | null {
154156
return droidData[id];
155157
}

src/__tests__/starWarsIntrospection-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { graphqlSync } from '../graphql';
77

88
import { StarWarsSchema } from './starWarsSchema';
99

10-
function queryStarWars(source) {
10+
function queryStarWars(source: string) {
1111
const result = graphqlSync({ schema: StarWarsSchema, source });
1212
expect(Object.keys(result)).to.deep.equal(['data']);
1313
return result.data;

src/__tests__/starWarsValidation-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { StarWarsSchema } from './starWarsSchema';
1313
/**
1414
* Helper function to test a query and the expected response.
1515
*/
16-
function validationErrors(query) {
16+
function validationErrors(query: string) {
1717
const source = new Source(query, 'StarWars.graphql');
1818
const ast = parse(source);
1919
return validate(StarWarsSchema, ast);

src/execution/__tests__/abstract-promise-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Dog {
2020
name: string;
2121
woofs: boolean;
2222

23-
constructor(name, woofs) {
23+
constructor(name: string, woofs: boolean) {
2424
this.name = name;
2525
this.woofs = woofs;
2626
}
@@ -30,7 +30,7 @@ class Cat {
3030
name: string;
3131
meows: boolean;
3232

33-
constructor(name, meows) {
33+
constructor(name: string, meows: boolean) {
3434
this.name = name;
3535
this.meows = meows;
3636
}
@@ -39,7 +39,7 @@ class Cat {
3939
class Human {
4040
name: string;
4141

42-
constructor(name) {
42+
constructor(name: string) {
4343
this.name = name;
4444
}
4545
}

src/execution/__tests__/abstract-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Dog {
2020
name: string;
2121
woofs: boolean;
2222

23-
constructor(name, woofs) {
23+
constructor(name: string, woofs: boolean) {
2424
this.name = name;
2525
this.woofs = woofs;
2626
}
@@ -30,7 +30,7 @@ class Cat {
3030
name: string;
3131
meows: boolean;
3232

33-
constructor(name, meows) {
33+
constructor(name: string, meows: boolean) {
3434
this.name = name;
3535
this.meows = meows;
3636
}
@@ -39,7 +39,7 @@ class Cat {
3939
class Human {
4040
name: string;
4141

42-
constructor(name) {
42+
constructor(name: string) {
4343
this.name = name;
4444
}
4545
}

src/execution/__tests__/directives-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const rootValue = {
3030
},
3131
};
3232

33-
function executeTestQuery(query) {
33+
function executeTestQuery(query: string) {
3434
const document = parse(query);
3535
return execute({ schema, document, rootValue });
3636
}

src/execution/__tests__/executor-test.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('Execute: Handles basic execution tasks', () => {
103103
e: () => 'Egg',
104104
f: 'Fish',
105105
// Called only by DataType::pic static resolver
106-
pic: (size) => 'Pic of size: ' + size,
106+
pic: (size: number) => 'Pic of size: ' + size,
107107
deep: () => deepData,
108108
promise: promiseData,
109109
};
@@ -1006,15 +1006,15 @@ describe('Execute: Handles basic execution tasks', () => {
10061006
class Special {
10071007
value: string;
10081008

1009-
constructor(value) {
1009+
constructor(value: string) {
10101010
this.value = value;
10111011
}
10121012
}
10131013

10141014
class NotSpecial {
10151015
value: string;
10161016

1017-
constructor(value) {
1017+
constructor(value: string) {
10181018
this.value = value;
10191019
}
10201020
}
@@ -1131,12 +1131,15 @@ describe('Execute: Handles basic execution tasks', () => {
11311131
});
11321132
const document = parse('{ foo }');
11331133

1134-
function fieldResolver(_source, _args, _context, info) {
1135-
// For the purposes of test, just return the name of the field!
1136-
return info.fieldName;
1137-
}
1134+
const result = execute({
1135+
schema,
1136+
document,
1137+
fieldResolver(_source, _args, _context, info) {
1138+
// For the purposes of test, just return the name of the field!
1139+
return info.fieldName;
1140+
},
1141+
});
11381142

1139-
const result = execute({ schema, document, fieldResolver });
11401143
expect(result).to.deep.equal({ data: { foo: 'foo' } });
11411144
});
11421145

@@ -1168,16 +1171,20 @@ describe('Execute: Handles basic execution tasks', () => {
11681171
types: [fooObject],
11691172
});
11701173

1171-
let possibleTypes;
1172-
function typeResolver(_source, _context, info, abstractType) {
1173-
// Resolver should be able to figure out all possible types on its own
1174-
possibleTypes = info.schema.getPossibleTypes(abstractType);
1174+
const rootValue = { foo: { bar: 'bar' } };
11751175

1176-
return 'FooObject';
1177-
}
1176+
let possibleTypes;
1177+
const result = execute({
1178+
schema,
1179+
document,
1180+
rootValue,
1181+
typeResolver(_source, _context, info, abstractType) {
1182+
// Resolver should be able to figure out all possible types on its own
1183+
possibleTypes = info.schema.getPossibleTypes(abstractType);
11781184

1179-
const rootValue = { foo: { bar: 'bar' } };
1180-
const result = execute({ schema, document, rootValue, typeResolver });
1185+
return 'FooObject';
1186+
},
1187+
});
11811188

11821189
expect(result).to.deep.equal({ data: { foo: { bar: 'bar' } } });
11831190
expect(possibleTypes).to.deep.equal([fooObject]);

src/execution/__tests__/lists-test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
GraphQLList,
1212
GraphQLNonNull,
1313
GraphQLObjectType,
14+
type GraphQLOutputType,
1415
} from '../../type/definition';
1516

1617
import { execute } from '../execute';
@@ -27,7 +28,7 @@ const rejected = Promise.reject.bind(Promise);
2728
* contains a rejection, testData should be a function that returns that
2829
* rejection so as not to trigger the "unhandled rejection" error watcher.
2930
*/
30-
function check(testType, testData, expected) {
31+
function check(testType: GraphQLOutputType, testData: mixed, expected: mixed) {
3132
return async () => {
3233
const data = { test: testData };
3334

@@ -71,7 +72,7 @@ describe('Execute: Accepts any iterable as list value', () => {
7172
}),
7273
);
7374

74-
function getArgs(...args) {
75+
function getArgs(...args: Array<string>) {
7576
return args;
7677
}
7778

0 commit comments

Comments
 (0)