Skip to content

Commit

Permalink
feat: add pokeapi test
Browse files Browse the repository at this point in the history
  • Loading branch information
a2937 committed Oct 24, 2024
1 parent 5be11a9 commit a9c49ca
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
16 changes: 11 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
},
"extends": ["eslint:recommended", "prettier"],
"rules": {
"no-unused-vars": ["warn", {
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}]
"no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
},
"parserOptions": {
"sourceType": "module"
},
"overrides": [
{
Expand Down
2 changes: 1 addition & 1 deletion apps/pokeapi-proxy/api/pokemon/pokemon.handlers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getPokemonEndpointResources = async (req, res, next) => {
'Fetching all resources for the Pokémon endpoint from PokéAPI'
);
const host = req.get('host');
const protocol = host.match(/localhost\:\d+/) ? 'http' : 'https';
const protocol = host.match(/localhost:\d+/) ? 'http' : 'https';
const { data } = await axios.get(
`https://pokeapi.co/api/v2/pokemon/?limit=9000`
);
Expand Down
64 changes: 64 additions & 0 deletions test/pokeapi-proxy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable no-undef */
const portMap = require('../port-map.json');
const pokeapiProxyPort = portMap['pokeapi-proxy'];
const { baseUrl } = require('./jest-utils.js');

const BASE_URL = baseUrl(pokeapiProxyPort);

describe('Pokémon api', () => {
it('Should return at minimum the first 151 Pokemon', async function () {
const response = await fetch(new URL('/api/pokemon', BASE_URL));
const pokedex = await response.json();
expect(response.status).toBe(200);
expect(pokedex.results.length).toBeGreaterThanOrEqual(151);
expect(pokedex.count).toBeGreaterThanOrEqual(151);
});

it('Should return Pikachu by name', async function () {
const response = await fetch(new URL('/api/pokemon/pikachu', BASE_URL));
const pokemon = await response.json();
expect(response.status).toBe(200);
expect(pokemon.name).toBe('pikachu');
expect(pokemon.id).toBe(25);
});

it('Should return Pikachu by id', async function () {
const response = await fetch(new URL('/api/pokemon/25', BASE_URL));
const pokemon = await response.json();
expect(response.status).toBe(200);
expect(pokemon.name).toBe('pikachu');
expect(pokemon.id).toBe(25);
});

it('Should return the female Nidoran by name', async function () {
const response = await fetch(new URL('/api/pokemon/nidoran-f', BASE_URL));
const pokemon = await response.json();
expect(response.status).toBe(200);
expect(pokemon.name).toBe('nidoran-f');
expect(pokemon.id).toBe(29);
});

it('Should return the female Nidoran by id', async function () {
const response = await fetch(new URL('/api/pokemon/29', BASE_URL));
const pokemon = await response.json();
expect(response.status).toBe(200);
expect(pokemon.name).toBe('nidoran-f');
expect(pokemon.id).toBe(29);
});

it('Should return Mr. Mime by name', async function () {
const response = await fetch(new URL('/api/pokemon/mr-mime', BASE_URL));
const pokemon = await response.json();
expect(response.status).toBe(200);
expect(pokemon.name).toBe('mr-mime');
expect(pokemon.id).toBe(122);
});

it('Should return Mr. Mime by id', async function () {
const response = await fetch(new URL('/api/pokemon/122', BASE_URL));
const pokemon = await response.json();
expect(response.status).toBe(200);
expect(pokemon.name).toBe('mr-mime');
expect(pokemon.id).toBe(122);
});
});
1 change: 0 additions & 1 deletion test/ports.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: Remove eslint-disable-next-line no-undef the minute expect can be labelled as defined
const { readdirSync } = require('fs');
const { join } = require('path');
const portMap = require('../port-map.json');
Expand Down

0 comments on commit a9c49ca

Please sign in to comment.