Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
feat: list Pokémons
Browse files Browse the repository at this point in the history
  • Loading branch information
higfonseca committed Sep 23, 2022
1 parent 6a80a2b commit 18698e8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/controllers/pokemon.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { Request, Response, Router } from 'express';
import axios from 'axios';

const router = Router();
router.get('/', (_req: Request, res: Response) => {
res.send('Hello World');
router.get('/', async (req: Request, res: Response) => {
const page = parseInt(req.query.page as string) || 1;
const limit = 20;
const offset = page - 1;

const u = `https://pokeapi.co/api/v2/pokemon?offset=${offset}&limit=${limit}`;
const headers = {
Authorization:
'eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI'
};

const r = await axios.get(u, { headers });
res.json(r.data.results);
});

export default router;
29 changes: 29 additions & 0 deletions tests/integration/controllers/pokemon.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { server } from "../../../src";
import axios from "axios";
import MockAdapter from "axios-mock-adapter"
import request from "supertest";

describe("test", () => {
afterAll((done) => {
server.close(done)
})


it("scenario 1", async() => {
let mock = new MockAdapter(axios);
mock.onGet().reply(200, {});

await request(server).get("/?page=1");

expect(mock.history.get[0].url).toBe("https://pokeapi.co/api/v2/pokemon?offset=0&limit=20")
})

it("scenario 2", async() => {
let mock = new MockAdapter(axios);
mock.onGet().reply(200, {});

await request(server).get("/?page=2");

expect(mock.history.get[0].url).toBe("https://pokeapi.co/api/v2/pokemon?offset=21&limit=20")
})
})

0 comments on commit 18698e8

Please sign in to comment.