Skip to content

Cicero API Integration

David Huang edited this page Nov 1, 2023 · 4 revisions

Overview

The representatives.test.js file contains tests for the /api/representatives/:zipCode endpoint of an Express.js application. The tests use the Jest testing framework and Supertest library to simulate HTTP requests and responses. The tests mock the Axios library to simulate requests to the Cicero API, which returns data about elected representatives based on a zip code.

Purpose

The purpose of conducting these tests is to ensure that the /api/representatives/:zipCode endpoint of the Express.js application is functioning correctly and returning the expected data. The tests check that the API returns a 200 status code and a single representative object in the response when a valid zip code is provided, and that it returns a 400 status code and an error message in the response when an invalid zip code is provided.

By running these tests, the programmer can catch any bugs or errors in the code before deploying it to production. This helps to ensure that the application is reliable and performs as expected for users. Additionally, these tests can be run automatically as part of a continuous integration and deployment (CI/CD) pipeline, which can help to catch issues early and streamline the development process.

Test Breakdown

The first test checks that the API returns a 200 status code and a single representative object in the response when a valid zip code is provided. The test mocks the Axios library to return a pre-defined response from the Cicero API.

The second test is similar to the first, but it uses the full zip code instead of just the first five digits.

The third test checks that the API returns a 400 status code and an error message in the response when an invalid zip code is provided. The test sets an invalid zip code and expects the API to return an error message that includes the invalid zip code.

The fourth test is commented out, but it would check that the API returns a 500 status code and an error message in the response when the Cicero API is down or not responding to requests from the application server.

Dependencies

The 'representatives.js' function requires various JavaScript libraries to perform as designed. You can install these dependencies by using package managers such as NPM or Yarn.

To access the Cicero API, you will need an API Key, from Cicero, which is required in the HTTP request headers. The API Key can be found in the "dotenv" package.

Clone this wiki locally