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

Commit 18698e8

Browse files
committed
feat: list Pokémons
1 parent 6a80a2b commit 18698e8

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/controllers/pokemon.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { Request, Response, Router } from 'express';
2+
import axios from 'axios';
23

34
const router = Router();
4-
router.get('/', (_req: Request, res: Response) => {
5-
res.send('Hello World');
5+
router.get('/', async (req: Request, res: Response) => {
6+
const page = parseInt(req.query.page as string) || 1;
7+
const limit = 20;
8+
const offset = page - 1;
9+
10+
const u = `https://pokeapi.co/api/v2/pokemon?offset=${offset}&limit=${limit}`;
11+
const headers = {
12+
Authorization:
13+
'eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI'
14+
};
15+
16+
const r = await axios.get(u, { headers });
17+
res.json(r.data.results);
618
});
719

820
export default router;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { server } from "../../../src";
2+
import axios from "axios";
3+
import MockAdapter from "axios-mock-adapter"
4+
import request from "supertest";
5+
6+
describe("test", () => {
7+
afterAll((done) => {
8+
server.close(done)
9+
})
10+
11+
12+
it("scenario 1", async() => {
13+
let mock = new MockAdapter(axios);
14+
mock.onGet().reply(200, {});
15+
16+
await request(server).get("/?page=1");
17+
18+
expect(mock.history.get[0].url).toBe("https://pokeapi.co/api/v2/pokemon?offset=0&limit=20")
19+
})
20+
21+
it("scenario 2", async() => {
22+
let mock = new MockAdapter(axios);
23+
mock.onGet().reply(200, {});
24+
25+
await request(server).get("/?page=2");
26+
27+
expect(mock.history.get[0].url).toBe("https://pokeapi.co/api/v2/pokemon?offset=21&limit=20")
28+
})
29+
})

0 commit comments

Comments
 (0)