This repository was archived by the owner on Jun 19, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +43
-2
lines changed
tests/integration/controllers Expand file tree Collapse file tree 2 files changed +43
-2
lines changed Original file line number Diff line number Diff line change 11import { Request , Response , Router } from 'express' ;
2+ import axios from 'axios' ;
23
34const 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
820export default router ;
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments