Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend/#085 movie routes tests #88

Merged
merged 11 commits into from
Mar 14, 2021
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NODE_ENV=development
SUPPRESS_NO_CONFIG_WARNING=true
9 changes: 9 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ jobs:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
mongodb-version: [4.4]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.3.0
with:
mongodb-version: ${{ matrix.mongodb-version }}

- run: npm ci
- run: npm run build --if-present
- run: npm test
env:
CI: true
deploy-on-github-pages:
name: 'Deploy app to GitHub Pages from main branch source code'
if: github.ref == 'refs/heads/master'
Expand Down
69 changes: 63 additions & 6 deletions api/movie/movie.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
describe('JavaScript sample test', () => {
describe('given sample', () => {
describe('when sample', () => {
it('then sample', () => {
expect(true).toBeTruthy();
});
/* eslint-disable no-underscore-dangle */
import mongoose from 'mongoose';
import supertest from 'supertest';

// TODO: CHECK SUPERTEST DEPENDENCY
import app from '../../test/mockserver.js';
import Movie from './Movie.js';

beforeEach((done) => {
mongoose.connect(
'mongodb://localhost:27017/JestDB',
{ useNewUrlParser: true, useUnifiedTopology: true },
() => done(),
);
});

afterEach((done) => {
mongoose.connection.db.dropDatabase(() => {
mongoose.connection.close(() => done());
});
});

test('GET /api/movies', async () => {
const movie = await Movie.create({
title: 'Movie1',
duration: 100,
releaseDate: '01-02-2020',
description: 'Description',
poster: '...jpg',
genre: 'thriller',
});

await supertest(app)
.get('/api/movies')
.expect(200)
.then((response) => {
// Check type and length
expect(Array.isArray(response.body)).toBeTruthy();
expect(response.body.length).toEqual(1);

// Check data
expect(response.body[0]._id).toBe(movie.id);
expect(response.body[0].title).toBe(movie.title);
expect(response.body[0].description).toBe(movie.description);
});
});

test('GET /api/movies/:id', async () => {
const movie = await Movie.create({
title: 'Movie1',
duration: 100,
releaseDate: '01-02-2020',
description: 'Description',
poster: '...jpg',
genre: 'thriller',
});

await supertest(app)
.get(`/api/movies/${movie.id}`)
.expect(200)
.then((response) => {
expect(response.body._id).toBe(movie.id);
expect(response.body.title).toBe(movie.title);
expect(response.body.poster).toBe(movie.poster);
});
});
3 changes: 2 additions & 1 deletion api/movie/movieRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ router
// @route GET api/movies/id
// @description get all movies
// @access user
.get(authMiddleware, async (req, res) => {
.get(async (req, res) => {
const movie = await Movie.findById(req.params.id);
try {
if (movie === undefined) {
Expand Down Expand Up @@ -111,6 +111,7 @@ router
res.status(400).send(e);
}
})

// @route DELETE api/movies/id
// @description Delete a movie
// @access admin
Expand Down
9 changes: 9 additions & 0 deletions api/user/user.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('JavaScript sample test', () => {
describe('given sample', () => {
describe('when sample', () => {
it('then sample', () => {
expect(true).toBeTruthy();
});
});
});
});
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
testEnvironment: 'node',
moduleFileExtensions: ['js', 'json'],
testRegex: '(spec|test)[.]js',
testPathIgnorePatterns: ['/node_modules/', '/client'],
coverageDirectory: '../coverage',
setupFiles: ['./test/setupJest.js'],
collectCoverageFrom: ['./features/**'],
};
107 changes: 107 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 2 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,8 @@
"postcss-scss": "^3.0.4",
"prettier": "^2.2.1",
"prettier-stylelint": "^0.4.2",
"rimraf": "^3.0.2"
},
"jest": {
"moduleFileExtensions": [
"js",
"json"
],
"testRegex": "(spec|test)[.]js",
"testPathIgnorePatterns": [
"/node_modules/",
"/client"
],
"coverageDirectory": "../coverage",
"setupFiles": [
"./test/setupJest.js"
],
"collectCoverageFrom": [
"./features/**"
]
"rimraf": "^3.0.2",
"supertest": "^6.1.3"
},
"staticFiles": {
"staticPath": [
Expand Down
5 changes: 4 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express';
import dotenv from 'dotenv';
import cookieParser from 'cookie-parser';
import connectDB from './config/mongodb.js';
import userRoute from './api/user/userRoute.js';
Expand All @@ -10,6 +11,8 @@ import cinemaRoute from './api/cinema/cinemaRoute.js';
import cinemaHallRoute from './api/cinemaHall/cinemaHallRoute.js';
import seatRoute from './api/seat/seatRoute.js';

dotenv.config();

const app = express();

app.get('/', (req, res) => res.send('API Runnin'));
Expand Down Expand Up @@ -39,7 +42,7 @@ app.use('/api/seats', seatRoute);
// app.use(express.static('client/build'))
// TODO: finish deployment config
// }

console.log(process.env.NODE_ENV);
const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
12 changes: 12 additions & 0 deletions test/mockserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import express from 'express';
import movieRoute from '../api/movie/movieRoute.js';

const app = express();

// Init Middleware (not body-parser anymore)
app.use(express.json());

// Define Routes
app.use('/api/movies', movieRoute);

export default app;