Creating tests for the missing routes

Following the same approach as in Chapter 5Building Your First API, where we create some tests first, let's do the same thing here and create some tests from the missing routes, user and order.

You may remember that there is a folder called test/routes. So, in that folder, create two files, one called user.spec.ts and another called order.spec.ts

The user.spec.ts file will contain the tests related to the user itself. Operations such as create a new user, update, retrieve, and delete will be included there. The content of the user.spec.ts file is as follows:

'use strict'

import * as chai from 'chai'
import chaiHttp = require('chai-http')
import 'mocha'
import app from '../../src/app'
import User from '../../src/model/User'

chai.use(chaiHttp)

const expect = chai.expect

const user: User = {
// generic random value from 1 to 100 only for tests so far
id: Math.floor(Math.random() * 100) + 1,
username: 'John',
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
password: 'password',
phone: '5555555',
userStatus: 1,
}

describe('userRoute', () => {
it('should respond with HTTP 404 status because there is no user', async () => {
return chai
.request(app)
.get(`/users/${user.username}`)
.then(res => {
expect(res.status).to.be.equal(404)
})
})
it('should create a new user and retrieve it back', async () => {
return chai
.request(app)
.post('/users/')
.send(user)
.then(res => {
expect(res.status).to.be.equal(201)
expect(res.body.username).to.be.equal(user.username)
})
})
it('should return the user created on the step before', async () => {
return chai
.request(app)
.get(`/users/${user.username}`)
.then(res => {
expect(res.status).to.be.equal(200)
expect(res.body.username).to.be.equal(user.username)
})
})
it('should updated the user Jhon', async () => {
user.username = 'Jhon Updated'
user.firstName = 'Jhon Updated'
user.lastName = 'Doe Updated'
user.email = 'jhon@myemail_updated.com'
user.password = 'password Updated'
user.phone = '3333333'
user.userStatus = 12

return chai
.request(app)
.patch(`/users/Jhon`)
.send(user)
.then(res => {
expect(res.status).to.be.equal(204)
})
})
it('should return the user updated on the step before', async () => {
return chai
.request(app)
.get(`/users/${user.username}`)
.then(res => {
expect(res.status).to.be.equal(200)
expect(res.body.username).to.be.equal(user.username)
expect(res.body.firstName).to.be.equal(user.firstName)
expect(res.body.lastName).to.be.equal(user.lastName)
expect(res.body.email).to.be.equal(user.email)
expect(res.body.password).to.be.equal(user.password)
expect(res.body.phone).to.be.equal(user.phone)
expect(res.body.userStatus).to.be.equal(user.userStatus)
})
})
it('should return 404 because the user does not exist', async () => {
user.firstName = 'Mary Jane'

return chai
.request(app)
.patch(`/users/Mary`)
.send(user)
.then(res => {
expect(res.status).to.be.equal(404)
})
})
it('should remove an existent user', async () => {
return chai
.request(app)
.del(`/users/${user.username}`)
.then(res => {
expect(res.status).to.be.equal(204)
})
})
it('should return 404 when it is trying to remove an user because the user does not exist', async () => {
return chai
.request(app)
.del(`/users/Mary`)
.then(res => {
expect(res.status).to.be.equal(404)
})
})
})

As you can see, we created a test for each operation.

Imagine that the data store is empty when the test starts. This is not the best approach, but we will evolve these tests throughout this book so that we can create something that is state-of-the-art.

The scenarios that we will cover are as follows:

  • Respond with a HTTP 404 status because there is no user
  • Create a new user and retrieve them
  • Return the user that was created in the previous step
  • Update the user John
  • Return the user that was updated in the previous step
  • Return 404 because the user does not exist
  • Remove an existing user
  • Return 404 when you are trying to remove a user because the user does not exist

Of course, if we run these tests, they will fail because there is no implementation in place. This is exactly what we want.

We will use the same approach that we used here to order scenarios. The order.spec.ts file's content is as follows:

'use strict'

import * as chai from 'chai'
import chaiHttp = require('chai-http')
import 'mocha'
import app from '../../src/app'
import Order from '../../src/model/order'
import { OrderStatus } from '../../src/model/orderStatus'

chai.use(chaiHttp)

const expect = chai.expect

const order: Order = {
// generic random value from 1 to 100 only for tests so far
id: 1,
userId: 20,
quantity: 1,
shipDate: new Date(),
status: OrderStatus.Placed,
complete: false,
}

describe('userRoute', () => {
it('should respond with HTTP 404 status because there is no order', async () => {
return chai
.request(app)
.get(`/store/orders/${order.id}`)
.then(res => {
expect(res.status).to.be.equal(404)
})
})
it('should create a new order and retrieve it back', async () => {
return chai
.request(app)
.post('/store/orders')
.send(order)
.then(res => {
expect(res.status).to.be.equal(201)
expect(res.body.userId).to.be.equal(order.userId)
expect(res.body.complete).to.be.equal(false)
order.id = res.body.id
})
})
it('should return the order created on the step before', async () => {
return chai
.request(app)
.get(`/store/orders/${order.id}`)
.then(res => {
expect(res.status).to.be.equal(200)
expect(res.body.id).to.be.equal(order.id)
expect(res.body.status).to.be.equal(order.status)
})
})
it('should return the inventory for all users', async () => {
return chai
.request(app)
.get(`/store/inventory`)
.then(res => {
expect(res.status).to.be.equal(200)
expect(res.body[20].length).to.be.equal(1)
})
})
it('should remove an existing order', async () => {
return chai
.request(app)
.del(`/store/orders/${order.id}`)
.then(res => {
expect(res.status).to.be.equal(204)
})
})
it('should return 404 when it is trying to remove an order because the order does not exist', async () => {
return chai
.request(app)
.del(`/store/orders/${order.id}`)
.then(res => {
expect(res.status).to.be.equal(404)
})
})
})

The same idea as before will be used for the order scenarios:

  • Respond with a HTTP 404 status because there is no order
  • Create a new order and retrieve them
  • Return the order that was created in the previous step
  • Return the inventory for all users
  • Remove an existing order
  • Return 404 when it is trying to remove an order because the order does not exist

Notice that, for order, there is a different test for inventory. Basically, we want the orders to be grouped by user, and that test will verify that.

For the purpose of this chapter, we will consider the data to be persistent in memory.

We would also like to create a route as an index for the order-api application so that we can also create a test file for it, called api.ts, which just retrieves title as a GET operation:

import { NextFunction, Request, Response } from 'express'

export let getApi = (req: Request, res: Response, next: NextFunction) => {
return res.status(200).send({ title: 'Order API' })
}

Up until now, we have tests that are failing. This is happening because there is no implementation for them. The next section is going to walk you through the code for the controllers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.209