-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,056 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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 { | ||
getObjectFromUrl, | ||
getObjectsByType, | ||
getObjectFromTypeAndId, | ||
} from '../apiHelper'; | ||
|
||
describe('API Helper', () => { | ||
it('Gets a person', async () => { | ||
const luke = await getObjectFromUrl('https://swapi.dev/api/people/1/'); | ||
expect(luke.name).toBe('Luke Skywalker'); | ||
const threePO = await getObjectFromUrl('https://swapi.dev/api/people/2/'); | ||
expect(threePO.name).toBe('C-3PO'); | ||
}); | ||
|
||
it('Gets all pages at once', async () => { | ||
const { objects, totalCount } = await getObjectsByType('people'); | ||
expect(objects.length).toBe(82); | ||
expect(totalCount).toBe(82); | ||
expect(objects[0].name).toBe('Luke Skywalker'); | ||
}); | ||
|
||
it('Gets a person by ID', async () => { | ||
const luke = await getObjectFromTypeAndId('people', 1); | ||
expect(luke.name).toBe('Luke Skywalker'); | ||
const threePO = await getObjectFromTypeAndId('people', 2); | ||
expect(threePO.name).toBe('C-3PO'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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']); | ||
}); | ||
}); |
Oops, something went wrong.