diff --git a/src/controllers/pokemon.ts b/src/controllers/pokemon.ts index a318c37..737f462 100644 --- a/src/controllers/pokemon.ts +++ b/src/controllers/pokemon.ts @@ -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; diff --git a/tests/integration/controllers/pokemon.spec.ts b/tests/integration/controllers/pokemon.spec.ts new file mode 100644 index 0000000..3a7e1e8 --- /dev/null +++ b/tests/integration/controllers/pokemon.spec.ts @@ -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") + }) +})