# Integration Test

Now comes my favorite part of the test, the high level API integration test. This is where we call the API endpoint directly as if we are posing as a real client.

The idea here is to set the application up with an open port and build a new database along with some fake seed data.

Then, make the API call programmatically with an HTTP library like `fetch`.

Finally, we make an assertion and check if the response is what we would expect from the client.

Here is how we'll be setting this test up.

*File: tests/api/auth/register.test.js*

```javascript
const fetch = require('node-fetch')
const api = require('../../../src/server')

const apiPort = Math.round(Math.random() * 65535)
const baseURL = `http://localhost:${apiPort}/api/v1`

const db = require('../../../src/utils/db')
let dbConnection
const dbTestUtils = require('../../../tests/testUtils/dbTestUtil')

/**
 * 1. Arrange
 *  - setup the world
 */
beforeAll(async () => {
  await api.listen(apiPort) // start the application
  dbConnection = await db() // start the database
})

beforeEach(async () => {
  await dbTestUtils.setUpDatabase()
})

afterEach(async () => {
  await dbTestUtils.clearDatabase()
})

afterAll(async () => {
  await api.close()
  await dbConnection.disconnect()
})

/**
 * 2. Act
 *  - make the http call
 * 3. Assert
 *  - response check
 */
describe('API Test - Register User', () => {
  // Tests go here
})
```

### The Passing Test

The first test is our happy path, so no errors. This is simple, make sure all the input requests are correct and that when we call the API, the user is successfully registered.

```javascript
test('POST /api/v1/users - user successfully registered', async () => {
  const user = {
    first_name: 'Yichen',
    last_name: 'Zhu',
    email: 'yichen@yichen.com',
    password: 'password123',
    password_confirmation: 'password123',
    phone_number: '1234567890'
  }

  const response = await (
    await fetch(`${baseURL}/users`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(user)
    })
  ).json()

  delete user.password
  delete user.password_confirmation

  expect(response).toMatchObject({
    status: 'success',
    code: 201,
    message: `The email: ${user.email} has successfully registered.`,
    data: user
  })
})
```

### The Failing Test

For the second test, let's have the API throw some sort of error. One possible scenario is if the user enters passwords do not match.

```javascript
test('POST /api/v1/users - fail due to invalid form inputs', async () => {
  const user = {
    first_name: 'Yichen',
    last_name: 'Zhu',
    email: 'yichen@yichen.com',
    password: 'password',
    password_confirmation: 'password_not_same',
    phone_number: '1234567890'
  }

  const response = await (
    await fetch(`${baseURL}/users`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(user)
    })
  ).json()

  expect(response).toMatchObject({
    code: 400,
    data: null,
    message: 'There were errors with the validation.',
    status: 'error',
    errors: ['The password confirmation and password fields must match.']
  })
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.restfulnode.com/part-3/chapter-10/8-integration-test.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
