Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ANSWERS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Sprint Challenge - Answers

**1. In Jest, what are the differences between describe() and it() globals, and what are good uses for them?**
describe() will contain all of the it() methods to keep each category seperate. it() methods are where the actual test will be contained.

**2. What is the point of Test Driven Development? What do you think about this approach?**
Test Driven Development helps to catch errors as you code in order to make sure you don't have errors that snowball as time goes on.

**3. Mention three types of automated tests.**
Unit testing, integration testing, snapshot testing.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ Demonstrate your understanding of this week's concepts by answering the followin

## Project Set Up

- [ ] Fork and clone this repository.
- [ ] **CD into the folder** where you downloaded the repository.
- [ ] Run `yarn` or `npm i` to download all dependencies.
- [ ] Type `yarn test` or `npm test` to run the tests. The `test` script is already configured.
- [x] Fork and clone this repository.
- [x] **CD into the folder** where you downloaded the repository.
- [x] Run `yarn` or `npm i` to download all dependencies.
- [x] Type `yarn test` or `npm test` to run the tests. The `test` script is already configured.

## Minimum Viable Product

Your finished project must include all of the following requirements:

- [ ] Use `jest` and `supertest` to write the tests.
- [ ] Write the **tests BEFORE** writing the route handlers.
- [ ] Your API must have both `POST` and `GET` endpoints for `/games`.
- [ ] Write a **minimum** of three tests per endpoint.
- [x] Use `jest` and `supertest` to write the tests.
- [x] Write the **tests BEFORE** writing the route handlers.
- [x] Your API must have both `POST` and `GET` endpoints for `/games`.
- [x] Write a **minimum** of three tests per endpoint.

Below is a product specification covering the requirements for your endpoints.

### POST /games

- [ ] The `POST /games` endpoint should take in an object that looks like this
- [x] The `POST /games` endpoint should take in an object that looks like this

```js
{
Expand All @@ -58,13 +58,13 @@ Below is a product specification covering the requirements for your endpoints.
}
```

- [ ] In the route handler, validate that the required fields are included inside the body. If the information is incomplete, return a `422` status code.
- [ ] Write tests to verify that the endpoint returns the correct HTTP status code when receiving correct and incorrect game data.
- [x] In the route handler, validate that the required fields are included inside the body. If the information is incomplete, return a `422` status code.
- [x] Write tests to verify that the endpoint returns the correct HTTP status code when receiving correct and incorrect game data.

### GET /games

- [ ] The `GET /games` endpoint should return the list of games and HTTP status code 200.
- [ ] Write a test to make sure this endpoint always returns an array, even if there are no games stored. If there are no games to return, the endpoint should return an empty array.
- [x] The `GET /games` endpoint should return the list of games and HTTP status code 200.
- [x] Write a test to make sure this endpoint always returns an array, even if there are no games stored. If there are no games to return, the endpoint should return an empty array.

## Stretch Problems

Expand Down
25 changes: 25 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const express = require('express');
const server = express();

const db = [];

server.use(express.json());

server.get('/games', async (req, res) => {
await res.status(200).json(db);
});

server.post('/games', (req, res) => {
const game = {
title: req.body.title,
genre: req.body.genre,
releaseYear: req.body.releaseYear
};
if (!req.body.title || !req.body.genre) {
res.status(422).json({ message: 'Please provide game title and genre' });
} else {
res.status(201).json(game);
}
});

module.exports = server;
50 changes: 50 additions & 0 deletions api/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const request = require('supertest');
const server = require('./server');

describe('GET /games tests', () => {
it('should return status 200', async () => {
const res = await request(server).get('/games');
expect(res.status).toBe(200);
});
it('should return an array', async () => {
const res = await request(server).get('/games');
expect(Array.isArray(res.body)).toBe(true);
});
it('should return an array if empty', async () => {
const res = await request(server).get('/games');
expect(res.body).toEqual([]);
});
});

describe('POST /games tests', () => {
it('should return status 201', async () => {
const res = await request(server)
.post('/games')
.send({
title: 'Pacman',
genre: 'Arcade',
releaseYear: 1980
});
expect(res.status).toBe(201);
});
it('should return status 422 if missing game title', async () => {
const res = await request(server)
.post('/games')
.send({
title: '',
genre: 'Arcade',
releaseYear: 1980
});
expect(res.status).toBe(422);
});
it('should return status 422 if missing game genre', async () => {
const res = await request(server)
.post('/games')
.send({
title: 'Pacman',
genre: '',
releaseYear: 1980
});
expect(res.status).toBe(422);
});
});
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const server = require('./api/server.js');

const port = process.env.PORT || 5000;
server.listen(port, () => console.log(`\nAPI running on port ${port}\n`));
Loading