Skip to content

Commit 23b57cc

Browse files
committed
moving files to typescript
1 parent c1ad3d9 commit 23b57cc

27 files changed

+1056
-41
lines changed

jest.config.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ const packageJSON = require('./package.json');
33
process.env.TZ = 'UTC';
44

55
module.exports = {
6-
verbose: true,
76
name: packageJSON.name,
8-
displayName: packageJSON.name,
7+
globals: {
8+
'ts-jest': {
9+
disableSourceMapSupport: true,
10+
},
11+
},
12+
verbose: true,
913
transform: {
10-
'\\.[jt]sx?$': 'babel-jest',
14+
'^.+\\.tsx?$': 'ts-jest',
1115
},
1216
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
1317
testURL: 'http://localhost',
1418
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
15-
testMatch: ['**/*.(spec|test).js'],
19+
testMatch: ['**/*.(spec|test).ts'],
1620
collectCoverage: true,
1721
coverageDirectory: './coverage/',
1822
};

package-lock.json

+94
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"postinstall": "npm run download && npm run build",
3030
"start": "node lib/server",
3131
"watch": "babel scripts/watch.js | node",
32-
"test": "npm run lint && npm run check && npm run test:only",
32+
"test": "npm run lint && npm run test:only",
3333
"test:only": "jest",
3434
"lint": "eslint src handler",
3535
"lint:fix": "eslint --fix src handler",
@@ -56,6 +56,7 @@
5656
},
5757
"devDependencies": {
5858
"@babel/core": "^7.12.3",
59+
"@types/graphql-relay": "^0.6.0",
5960
"babel-cli": "^6.26.0",
6061
"babel-core": "^6.26.3",
6162
"babel-eslint": "^10.0.3",
@@ -77,6 +78,8 @@
7778
"jest": "^26.6.3",
7879
"netlify-lambda": "^1.6.3",
7980
"prettier": "^1.18.2",
80-
"sane": "^4.1.0"
81+
"sane": "^4.1.0",
82+
"ts-jest": "^26.4.4",
83+
"typescript": "^4.1.2"
8184
}
8285
}
File renamed without changes.
File renamed without changes.

src/api/local.js renamed to src/api/local.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@
88
* @flow strict
99
*/
1010

11-
import swapiData from '../../cache/data';
11+
import swapiData from '../../cache/data.json';
1212

1313
/**
1414
* Given a URL of an object in the SWAPI, return the data
1515
* from our local cache.
1616
*/
17+
// casting json file to a Record with Index.
18+
const typedSwapiData = swapiData as typeof swapiData & { [key: string]: any }
1719
export async function getFromLocalUrl(
18-
url: string,
19-
): Promise<{ [key: string]: any }> {
20-
const text = swapiData[url];
20+
url: unknown,
21+
): Promise<Record<string, any>> {
22+
23+
if (!(typeof url === 'string')) {
24+
throw new Error('Url provided is not a string');
25+
}
26+
27+
const text = typedSwapiData[url];
2128
if (!text) {
2229
throw new Error(`No entry in local cache for ${url}`);
2330
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the license found in the
6+
* LICENSE-examples file in the root directory of this source tree.
7+
*/
8+
9+
import {
10+
getObjectFromUrl,
11+
getObjectsByType,
12+
getObjectFromTypeAndId,
13+
} from '../apiHelper';
14+
15+
describe('API Helper', () => {
16+
it('Gets a person', async () => {
17+
const luke = await getObjectFromUrl('https://swapi.dev/api/people/1/');
18+
expect(luke.name).toBe('Luke Skywalker');
19+
const threePO = await getObjectFromUrl('https://swapi.dev/api/people/2/');
20+
expect(threePO.name).toBe('C-3PO');
21+
});
22+
23+
it('Gets all pages at once', async () => {
24+
const { objects, totalCount } = await getObjectsByType('people');
25+
expect(objects.length).toBe(82);
26+
expect(totalCount).toBe(82);
27+
expect(objects[0].name).toBe('Luke Skywalker');
28+
});
29+
30+
it('Gets a person by ID', async () => {
31+
const luke = await getObjectFromTypeAndId('people', 1);
32+
expect(luke.name).toBe('Luke Skywalker');
33+
const threePO = await getObjectFromTypeAndId('people', 2);
34+
expect(threePO.name).toBe('C-3PO');
35+
});
36+
});

src/schema/__tests__/film.spec.ts

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the license found in the
6+
* LICENSE-examples file in the root directory of this source tree.
7+
*/
8+
9+
import { swapi } from './swapi';
10+
11+
function getDocument(query: string): string {
12+
return `${query}
13+
fragment AllFilmProperties on Film {
14+
director
15+
episodeID
16+
openingCrawl
17+
producers
18+
releaseDate
19+
title
20+
characterConnection(first:1) { edges { node { name } } }
21+
planetConnection(first:1) { edges { node { name } } }
22+
speciesConnection(first:1) { edges { node { name } } }
23+
starshipConnection(first:1) { edges { node { name } } }
24+
vehicleConnection(first:1) { edges { node { name } } }
25+
}
26+
`;
27+
}
28+
29+
describe('Film type', () => {
30+
it('Gets an object by SWAPI ID', async () => {
31+
const query = '{ film(filmID: 1) { title } }';
32+
const result = await swapi(query);
33+
expect(result.data?.film.title).toBe('A New Hope');
34+
});
35+
36+
it('Gets a different object by SWAPI ID', async () => {
37+
const query = '{ film(filmID: 2) { title } }';
38+
const result = await swapi(query);
39+
expect(result.data?.film.title).toBe('The Empire Strikes Back');
40+
});
41+
42+
it('Gets an object by global ID', async () => {
43+
const query = '{ film(filmID: 1) { id, title } }';
44+
const result = await swapi(query);
45+
const nextQuery = `{ film(id: "${result.data?.film.id}") { id, title } }`;
46+
const nextResult = await swapi(nextQuery);
47+
expect(result.data?.film.title).toBe('A New Hope');
48+
expect(nextResult.data?.film.title).toBe('A New Hope');
49+
expect(result.data?.film.id).toBe(nextResult.data?.film.id);
50+
});
51+
52+
it('Gets an object by global ID with node', async () => {
53+
const query = '{ film(filmID: 1) { id, title } }';
54+
const result = await swapi(query);
55+
const nextQuery = `{
56+
node(id: "${result.data?.film.id}") {
57+
... on Film {
58+
id
59+
title
60+
}
61+
}
62+
}`;
63+
const nextResult = await swapi(nextQuery);
64+
expect(result.data?.film.title).toBe('A New Hope');
65+
expect(nextResult.data?.node.title).toBe('A New Hope');
66+
expect(result.data?.film.id).toBe(nextResult.data?.node.id);
67+
});
68+
69+
it('Gets all properties', async () => {
70+
const query = getDocument(
71+
`{
72+
film(filmID: 1) {
73+
...AllFilmProperties
74+
}
75+
}`,
76+
);
77+
const result = await swapi(query);
78+
const expected = {
79+
title: 'A New Hope',
80+
episodeID: 4,
81+
openingCrawl:
82+
"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....",
83+
director: 'George Lucas',
84+
producers: ['Gary Kurtz', 'Rick McCallum'],
85+
releaseDate: '1977-05-25',
86+
speciesConnection: { edges: [{ node: { name: 'Human' } }] },
87+
starshipConnection: { edges: [{ node: { name: 'CR90 corvette' } }] },
88+
vehicleConnection: { edges: [{ node: { name: 'Sand Crawler' } }] },
89+
characterConnection: { edges: [{ node: { name: 'Luke Skywalker' } }] },
90+
planetConnection: { edges: [{ node: { name: 'Tatooine' } }] },
91+
};
92+
expect(result.data?.film).toMatchObject(expected);
93+
});
94+
95+
it('All objects query', async () => {
96+
const query = getDocument(
97+
'{ allFilms { edges { cursor, node { ...AllFilmProperties } } } }',
98+
);
99+
const result = await swapi(query);
100+
expect(result.data?.allFilms.edges.length).toBe(6);
101+
});
102+
103+
it('Pagination query', async () => {
104+
const query = `{
105+
allFilms(first: 2) { edges { cursor, node { title } } }
106+
}`;
107+
const result = await swapi(query);
108+
expect(
109+
result.data?.allFilms.edges.map((e: any) => e.node.title),
110+
).toMatchObject(['A New Hope', 'The Empire Strikes Back']);
111+
const nextCursor = result.data?.allFilms.edges[1].cursor;
112+
const nextQuery = `{ allFilms(first: 2, after:"${nextCursor}") {
113+
edges { cursor, node { title } } }
114+
}`;
115+
const nextResult = await swapi(nextQuery);
116+
expect(
117+
nextResult.data?.allFilms.edges.map((e: any) => e.node.title),
118+
).toMatchObject(['Return of the Jedi', 'The Phantom Menace']);
119+
});
120+
});

0 commit comments

Comments
 (0)