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

feat: list Pokémons #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
})