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
13 changes: 13 additions & 0 deletions Review.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Please answer the following questions

1. In Jest, what are the differences between `describe()` and `it()` globals, and what are good uses for them?

`describe()` is used as a kind of container for different tests. Describe is useful for creating divisions for tests that are performing on different endpoints or components. `it()` is where individual tests are performed, and contain all of the actual testing logic. `it()` operators are not able to be nested, as they must run one test at a time.

2. What is the point of `Test Driven Development`? What do you think about this approach?

Test Driven Development is useful for creating a testing architecture that can be utilized for future product releases to prevent regressions in code. By building out unit tests, you can make sure all tests are passing before adding new features to the application, without having to do as much debugging after the features are implemented.

3. What are `mocks`? What are a good use cases for them?

A mock is a kind of pseudo-API or lightweight data model that can allow us to run tests on functions or data. A good use case would be to create a mock database entry that can be tested without having to bother with excessive calls to a database.

4. Mention three types of automated tests.

Unit testing
GUI-based testing
API driven testing
63 changes: 63 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const express = require('express');
const server = express();

server.use(express.json());

server.get('/', (req, res) => {
res.status(200).json({ message: 'server is working' });
});


function logger(req, res, next){
console.log(`${req.method} to ${req.url}`);
next();
}

server.use(logger);

const games = [
{
title: 'Pacman',
genre: 'Arcade',
releaseYear: 1980
},
{
title: 'Final Fantasy VII',
genre: 'RPG',
releaseYear: 1997
},
{
title: 'Parasite Eve',
genre: 'Horror',
releaseYear: 1995
}
];

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

server.post('/games', (req, res) => {
let {title, genre, releaseYear} = req.body;
// check for required fields
if (!title || !genre){
return res.status(422).json({ error: 'You must include a title and genre.' })
}

// STRETCH
//send error 419 if game title is not unique
// collect all game titles
let allTitles = [];
for (i = 0; i < games.length; i++) {
allTitles.push(games[i].title);
}
// compare given title to current titles
if (allTitles.includes(req.body.title)) {
return res.status(419).json({ error: 'That game is already added.' })
}

return res.status(201).json({ message: `${title} added to games database.` })
})


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

server.listen(9000, () => console.log('\nserver up on port 9000\n'));
91 changes: 91 additions & 0 deletions index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const server = require('./api/server.js');

const request = require('supertest');

describe('testing for GET and POST server endpoints', () => {
describe('GET /games endpoint tests', () => {
it('should return status code 200(OK)', async () => {
const response = await request(server).get('/games')

expect(response.status).toBe(200);
});

it('should return JSON', async () => {
const response = await request(server).get('/games');

expect(response.type).toBe('application/json');
});

it('should return the correct array of game objects', async () => {
const response = await request(server).get('/games');

const expected = [
{
title: 'Pacman',
genre: 'Arcade',
releaseYear: 1980
},
{
title: 'Final Fantasy VII',
genre: 'RPG',
releaseYear: 1997
},
{
title: 'Parasite Eve',
genre: 'Horror',
releaseYear: 1995
}
];

expect(response.body).toEqual(expected);
expect(Array.isArray(response.body)).toBe(true);
});
});

describe('POST tests for /games', () => {
it('should return a 422 status code if the object being sent is incomplete', async () => {
const newGame = {
title: "Metal Gear Solid",
releaseYear: 1998
}
const response = await request(server).post('/games').send(newGame);

expect(response.status).toBe(422);
});

it('should return status 201 if the object has required fields', async () => {
const newGame = {
title: 'Metal Gear Solid',
genre: 'Stealth',
releaseYear: 1998
}

const response = await request(server).post('/games').send(newGame);

expect(response.status).toBe(201);
});

it('should return JSON', async () => {
const newGame = {
title: 'Metal Gear Solid',
genre: 'Stealth',
releaseYear: 1998
}
const response = await request(server).post('/games').send(newGame);

expect(response.type).toBe('application/json');
});

it('should return a message with the game title indicating the post was successful', async () => {
const newGame = {
title: 'Metal Gear Solid',
genre: 'Stealth',
releaseYear: 1998
}

const response = await request(server).post('/games').send(newGame);

expect(response.body).toEqual({message: `Metal Gear Solid added to games database.`})
});
});
});
Loading