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
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ In this challenge use `Test Driven Development` to build a RESTful API using Nod
Demonstrate your understanding of this week's concepts by answering the following free-form questions. Edit this document to include your answers after each question. Make sure to leave a blank line above and below your answer so it is clear and easy to read by your project manager.

1. In Jest, what are the differences between `describe()` and `it()` globals, and what are good uses for them?
1. What is the point of `Test Driven Development`? What do you think about this approach?
1. Mention three types of automated tests.
> In Jest, the `describe()` global is used to break down a test suite into a sepcific function and describe what it should accomplish; meanwhile, an `it()` global in Jest serves to describe an individual test.
2. What is the point of `Test Driven Development`? What do you think about this approach?
> `Test Driven Development` is a programming method which emphasizes consideration of a program's requirements or design before your before the creation of functional code; while this approach seems highly beneficial, I find it a tedious and unneccsary approach.
3. Mention three types of automated tests.
> Some common levels of testing are: `unit testing`, `integration testing`, and `end-to-end testing`.

## 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

Expand All @@ -58,12 +61,12 @@ 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.
- [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.
- [ ] 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.
- [x] 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.

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

const server = express();

server.use(express.json());

module.exports = server;

// TEST ENDPOINT FUNCTIONALITY
server.get('/', async (req, res) => {
res
.status(200)
.json({ API: 'UP AND RUNNING' });
});


// GAMES OBJECT
let games = [
{
title: 'Super Smash Bros.',
genre: 'Action',
releaseYear: 1999
},
{
title: 'Super Smash Bros. Melee',
genre: 'Perfection',
releaseYear: 2001
},
{
title: 'Super Smash Bros. Brawl',
genre: 'Party',
releaseYear: 2006
},
{
title: 'Super Smash Bros. Ultimate',
genre: 'Fighting',
releaseYear: 2018
}
];

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

// POST GAMES ENPOINT
server.post('/games', (req, res) => {
let {title, genre} = req.body;
if (title && genre){
res
.status(201).json(req.body);
}else{
res
.status(422)
.json({message : "Missing Info"});
}
});
92 changes: 92 additions & 0 deletions api/server.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const server = require('./server.js');

const request = require('supertest');

describe('testing module', () => {

describe('Tests GET/games functions', () => {
// TESTING STATUS CODE
test('should return status code 200', async () => {
const res = await request(server).get('/games')

expect(res.status).toBe(200);
});
// TESTING BODY DATA TYPE
test('should return JSON', async () => {
const res = await request(server).get('/games');

expect(res.type).toBe('application/json');
});
// TESTING RETURN DATA
test('should return the array', async () => {
const res = await request(server).get('/games');

const expected = [
{
title: 'Super Smash Bros.',
genre: 'Action',
releaseYear: 1999
},
{
title: 'Super Smash Bros. Melee',
genre: 'Perfection',
releaseYear: 2001
},
{
title: 'Super Smash Bros. Brawl',
genre: 'Party',
releaseYear: 2006
},
{
title: 'Super Smash Bros. Ultimate',
genre: 'Fighting',
releaseYear: 2018
}
];

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

describe('Tests POST/games functions', () => {
// TESTING STATUS CODE
test('should return a 422 status code if information is incomplete', async () => {
const incompleteGame =
{
title: "Super Smash Bros",
releaseYear: ""
}

const res = await request(server)
.post('/games')
.send(incompleteGame);

expect(res.status).toBe(422);
});
// TESTING DATA TYPE
test('should return JSON', async () => {
const newGame =
{
title: 'Super Smash Bros. for WiiU',
genre: 'Action',
releaseYear: 2015
}

const res = await request(server).post('/games').send(newGame);
expect(res.type).toBe('application/json');
});
// TESTING CORRECT RESPONSE
test('should return status 201 if information is right', async () => {
const body =
{
title: 'Super Smash Bros. Ultimate',
genre: 'Fighting',
releaseYear: 2018
}
const res = await request(server).post('/games').send(body);

expect(res.status).toBe(201);
});
});
});
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 = 8888;
server.listen(PORT, () => console.log(`\n** server up on port ${PORT} **\n`));
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
"description": "Testing Sprint Challenge",
"main": "index.js",
"scripts": {
"test": "jest --watch --verbose",
"start": "nodemon index.js"
"test": "cross-env DB_ENV=testing jest --watch --verbose",
"start": "node index.js",
"server": "nodemon index.js"
},
"jest": {
"testEnvironment": "node"
},
"repository": {
"type": "git",
Expand All @@ -19,9 +23,12 @@
},
"homepage": "https://github.com/LambdaSchool/Sprint-Challenge--Testing#readme",
"dependencies": {
"express": "^4.16.4"
"express": "^4.16.4",
"knex": "^0.16.5",
"sqlite3": "^4.0.6"
},
"devDependencies": {
"cross-env": "^5.2.0",
"jest": "^23.6.0",
"supertest": "^3.3.0"
}
Expand Down
Loading