Skip to content

Commit b4e6602

Browse files
committed
tests: Remove numbers as keys of object
1 parent db5c587 commit b4e6602

File tree

4 files changed

+83
-131
lines changed

4 files changed

+83
-131
lines changed

src/__tests__/starWarsData.js

Lines changed: 36 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,105 +6,66 @@
66
* JSON objects in a more complex demo.
77
*/
88

9-
const xwing = {
10-
id: '1',
11-
name: 'X-Wing',
12-
};
13-
14-
const ywing = {
15-
id: '2',
16-
name: 'Y-Wing',
17-
};
18-
19-
const awing = {
20-
id: '3',
21-
name: 'A-Wing',
22-
};
23-
24-
// Yeah, technically it's Corellian. But it flew in the service of the rebels,
25-
// so for the purposes of this demo it's a rebel ship.
26-
const falcon = {
27-
id: '4',
28-
name: 'Millenium Falcon',
29-
};
30-
31-
const homeOne = {
32-
id: '5',
33-
name: 'Home One',
34-
};
9+
type Ship = {|
10+
id: string,
11+
name: string,
12+
|};
3513

36-
const tieFighter = {
37-
id: '6',
38-
name: 'TIE Fighter',
39-
};
14+
const allShips: Array<Ship> = [
15+
{ id: '1', name: 'X-Wing' },
16+
{ id: '2', name: 'Y-Wing' },
17+
{ id: '3', name: 'A-Wing' },
4018

41-
const tieInterceptor = {
42-
id: '7',
43-
name: 'TIE Interceptor',
44-
};
19+
// Yeah, technically it's Corellian. But it flew in the service of the rebels,
20+
// so for the purposes of this demo it's a rebel ship.
21+
{ id: '4', name: 'Millenium Falcon' },
22+
{ id: '5', name: 'Home One' },
23+
{ id: '6', name: 'TIE Fighter' },
24+
{ id: '7', name: 'TIE Interceptor' },
25+
{ id: '8', name: 'Executor' },
26+
];
4527

46-
const executor = {
47-
id: '8',
48-
name: 'Executor',
49-
};
28+
type Faction = {|
29+
id: string,
30+
name: string,
31+
ships: Array<string>,
32+
|};
5033

51-
const rebels = Object.freeze({
34+
const rebels: Faction = {
5235
id: '1',
5336
name: 'Alliance to Restore the Republic',
5437
ships: ['1', '2', '3', '4', '5'],
55-
});
38+
};
5639

57-
const empire = Object.freeze({
40+
const empire: Faction = {
5841
id: '2',
5942
name: 'Galactic Empire',
6043
ships: ['6', '7', '8'],
61-
});
62-
63-
const data = Object.freeze({
64-
Faction: {
65-
'1': rebels,
66-
'2': empire,
67-
},
68-
Ship: {
69-
'1': xwing,
70-
'2': ywing,
71-
'3': awing,
72-
'4': falcon,
73-
'5': homeOne,
74-
'6': tieFighter,
75-
'7': tieInterceptor,
76-
'8': executor,
77-
},
78-
});
79-
80-
type Ship = {|
81-
id: string,
82-
name: string,
83-
|};
44+
};
8445

85-
type Faction = {|
86-
id: string,
87-
name: string,
88-
ships: $ReadOnlyArray<string>,
89-
|};
46+
const allFactions: Array<Faction> = [rebels, empire];
9047

9148
let nextShip = 9;
9249
export function createShip(shipName: string, factionId: string): Ship {
9350
const newShip = {
9451
id: String(nextShip++),
9552
name: shipName,
9653
};
97-
data.Ship[newShip.id] = newShip;
98-
data.Faction[factionId].ships.push(newShip.id);
54+
55+
allShips.push(newShip);
56+
57+
const faction = allFactions.find((obj) => obj.id === factionId);
58+
// eslint-disable-next-line no-unused-expressions
59+
faction?.ships.push(newShip.id);
9960
return newShip;
10061
}
10162

102-
export function getShip(id: string): Ship {
103-
return data.Ship[id];
63+
export function getShip(id: string): Ship | void {
64+
return allShips.find((ship) => ship.id === id);
10465
}
10566

106-
export function getFaction(id: string): Faction {
107-
return data.Faction[id];
67+
export function getFaction(id: string): Faction | void {
68+
return allFactions.find((faction) => faction.id === id);
10869
}
10970

11071
export function getRebels(): Faction {

src/node/__tests__/global.js

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,49 @@ import {
1212

1313
import { fromGlobalId, globalIdField, nodeDefinitions } from '../node';
1414

15-
const userData = {
16-
'1': {
17-
id: 1,
15+
const userData = [
16+
{
17+
id: '1',
1818
name: 'John Doe',
1919
},
20-
'2': {
21-
id: 2,
20+
{
21+
id: '2',
2222
name: 'Jane Smith',
2323
},
24-
};
24+
];
2525

26-
const photoData = {
27-
'1': {
28-
photoId: 1,
26+
const photoData = [
27+
{
28+
photoId: '1',
2929
width: 300,
3030
},
31-
'2': {
32-
photoId: 2,
31+
{
32+
photoId: '2',
3333
width: 400,
3434
},
35-
};
35+
];
3636

37-
const postData = {
38-
'1': {
39-
id: 1,
37+
const postData = [
38+
{
39+
id: '1',
4040
text: 'lorem',
4141
},
42-
'2': {
43-
id: 2,
42+
{
43+
id: '2',
4444
text: 'ipsum',
4545
},
46-
};
46+
];
4747

4848
const { nodeField, nodeInterface } = nodeDefinitions(
4949
(globalId) => {
5050
const { type, id } = fromGlobalId(globalId);
5151
switch (type) {
5252
case 'User':
53-
return userData[id];
53+
return userData.find((obj) => obj.id === id);
5454
case 'Photo':
55-
return photoData[id];
55+
return photoData.find((obj) => obj.photoId === id);
5656
case 'Post':
57-
return postData[id];
57+
return postData.find((obj) => obj.id === id);
5858
}
5959
},
6060
(obj) => {
@@ -111,14 +111,7 @@ const queryType = new GraphQLObjectType({
111111
node: nodeField,
112112
allObjects: {
113113
type: new GraphQLList(nodeInterface),
114-
resolve: () => [
115-
userData[1],
116-
userData[2],
117-
photoData[1],
118-
photoData[2],
119-
postData[1],
120-
postData[2],
121-
],
114+
resolve: () => [...userData, ...photoData, ...postData],
122115
},
123116
}),
124117
});

src/node/__tests__/node.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,42 @@ import {
1313

1414
import { nodeDefinitions } from '../node';
1515

16-
const userData = {
17-
'1': {
18-
id: 1,
16+
const userData = [
17+
{
18+
id: '1',
1919
name: 'John Doe',
2020
},
21-
'2': {
22-
id: 2,
21+
{
22+
id: '2',
2323
name: 'Jane Smith',
2424
},
25-
};
25+
];
2626

27-
const photoData = {
28-
'3': {
29-
id: 3,
27+
const photoData = [
28+
{
29+
id: '3',
3030
width: 300,
3131
},
32-
'4': {
33-
id: 4,
32+
{
33+
id: '4',
3434
width: 400,
3535
},
36-
};
36+
];
3737

3838
const { nodeField, nodesField, nodeInterface } = nodeDefinitions(
3939
(id, _context, info) => {
4040
expect(info.schema).to.equal(schema);
41-
if (userData[id]) {
42-
return userData[id];
43-
}
44-
if (photoData[id]) {
45-
return photoData[id];
46-
}
41+
return (
42+
userData.find((obj) => obj.id === id) ??
43+
photoData.find((obj) => obj.id === id)
44+
);
4745
},
4846
(obj) => {
49-
if (userData[obj.id]) {
47+
if (userData.includes(obj)) {
5048
return userType;
5149
}
5250
// istanbul ignore else (Can't be reached)
53-
if (photoData[obj.id]) {
51+
if (photoData.includes(obj)) {
5452
return photoType;
5553
}
5654
},

src/node/__tests__/nodeasync.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ import {
1212

1313
import { nodeDefinitions } from '../node';
1414

15-
const userData = {
16-
'1': {
17-
id: 1,
15+
const userData = [
16+
{
17+
id: '1',
1818
name: 'John Doe',
1919
},
20-
'2': {
21-
id: 2,
20+
{
21+
id: '2',
2222
name: 'Jane Smith',
2323
},
24-
};
24+
];
2525

2626
const { nodeField, nodeInterface } = nodeDefinitions(
27-
(id) => userData[id],
27+
(id) => userData.find((obj) => obj.id === id),
2828
() => userType,
2929
);
3030

0 commit comments

Comments
 (0)