|
| 1 | +/* eslint-disable no-unused-vars */ |
1 | 2 | require('dotenv').config() |
2 | 3 | const request = require('supertest') |
| 4 | +const axios = require('axios') |
3 | 5 | const express = require('express') |
4 | 6 | const apiRouter = require('../../api') |
| 7 | +const apiResponse = require('../../apiResponse') |
| 8 | +const ciceroResponse = require('../../ciceroResponse') |
5 | 9 |
|
6 | 10 | // Create a test application |
| 11 | +/* Setting up the environment for the test. */ |
| 12 | + |
| 13 | +// Create a test application and get the application id and secret key for the test application from the test application page. |
7 | 14 | const app = express() |
8 | 15 | app.use('/api', apiRouter) |
| 16 | +//'https://cicero.azavea.com/v3.1/official' |
| 17 | +const zipCode = '84054-6013' |
9 | 18 |
|
10 | 19 | afterEach(() => { |
11 | 20 | jest.clearAllMocks() |
12 | 21 | }) |
13 | 22 |
|
| 23 | +// Mock the axios request to the Cicero API and return the mock data for the test. |
14 | 24 | afterAll(async () => { |
15 | 25 | await new Promise((resolve) => setTimeout(() => resolve(), 500)) // avoid jest open handle error |
16 | 26 | }) |
17 | 27 |
|
18 | | -describe('/api/representatives/:zipcode', () => { |
19 | | - test('returns 200 status', async () => { |
20 | | - const response = await request(app).get('/api/representatives/92107') |
| 28 | +// Mock the axios request to the Cicero API and return the mock data for the test. |
| 29 | +jest.mock('axios') |
| 30 | + |
| 31 | +// Mock the axios request to the Cicero API and return the mock data for the test. |
| 32 | +describe('GET /api/representatives/:zipCode', () => { |
| 33 | + //test that the api returns a 200 status code and a single representative object in the response |
| 34 | + it('should return a 200 status code and a single representative object in the response', async () => { |
| 35 | + const spy = jest.spyOn(axios, 'get') |
| 36 | + spy.mockImplementation(async (url, { params }) => { |
| 37 | + return { |
| 38 | + data: ciceroResponse |
| 39 | + } |
| 40 | + }) |
| 41 | + // Make a request to the API with half of the zipCode |
| 42 | + const response = await request(app).get( |
| 43 | + `/api/representatives/${zipCode.slice(0, 5)}` |
| 44 | + ) |
21 | 45 | expect(response.status).toBe(200) |
22 | | - }) |
23 | | - test('returns 200 status', async () => { |
24 | | - const response = await request(app).get('/api/representatives/92107-1234') |
| 46 | + expect(response.body).toEqual(apiResponse) |
| 47 | + expect(spy).toHaveBeenCalled() |
| 48 | + spy.mockRestore() |
| 49 | + }, 50000) |
| 50 | +}) |
| 51 | + |
| 52 | +// Mock the axios request to the Cicero API and return the mock data for the test. |
| 53 | +describe('GET /api/representatives/:zipCode', () => { |
| 54 | + //test that the api returns a 200 status code and a single representative object in the response |
| 55 | + it('should return a 200 status code and a single representative object in the response', async () => { |
| 56 | + const spy = jest.spyOn(axios, 'get') |
| 57 | + spy.mockImplementation(async (url, { params }) => { |
| 58 | + return { |
| 59 | + data: ciceroResponse |
| 60 | + } |
| 61 | + }) |
| 62 | + // Make a request to the API |
| 63 | + const response = await request(app).get(`/api/representatives/${zipCode}`) |
25 | 64 | expect(response.status).toBe(200) |
26 | | - }) |
27 | | - test('returns 400 status', async () => { |
28 | | - const response = await request(app).get('/api/representatives/cat') |
29 | | - expect(response.status).toBe(400) |
30 | | - }) |
| 65 | + expect(response.body).toEqual(apiResponse) |
| 66 | + expect(spy).toHaveBeenCalled() |
| 67 | + spy.mockRestore() |
| 68 | + }, 50000) |
31 | 69 | }) |
| 70 | + |
| 71 | +// mock implementation with an invalid zip code which return 400 status code |
| 72 | +it('should return a 400 status code and an error message in the response', async () => { |
| 73 | + const spy = jest.spyOn(axios, 'get') |
| 74 | + // Make a request to the API |
| 75 | + const response = await request(app).get(`/api/representatives/invalid`) |
| 76 | + expect(response.status).toBe(400) |
| 77 | + expect(response.body).toEqual({ |
| 78 | + error: `Invalid zip code format, valid examples are 84054-6013 or 84054. The zipcode used was invalid` |
| 79 | + }) |
| 80 | + spy.mockRestore() |
| 81 | +}, 50000) |
| 82 | + |
| 83 | +// //mock test that gives a 500 status code and an error message in the response when the cicero api is down or not responding to the request from the application server |
| 84 | +// it('should return a 500 status code and an error message in the response when the cicero api is down or not responding to the request from the application server', async () => { |
| 85 | +// const spy = jest.spyOn(axios, 'get') |
| 86 | +// spy.mockImplementation(async (url, { params }) => { |
| 87 | +// return { |
| 88 | +// data: ciceroResponse |
| 89 | +// } |
| 90 | +// }) |
| 91 | +// // Make a request to the API |
| 92 | +// const response = await request(app).get(`/api/representatives/${zipCode}`) |
| 93 | +// expect(response.status).toBe(500) |
| 94 | +// expect(response.body).toEqual({ |
| 95 | +// error: 'Internal server error' |
| 96 | +// }) |
| 97 | +// spy.mockRestore() |
| 98 | +// }) |
0 commit comments