Implement integration tests

This commit is contained in:
Elliot DeNolf
2019-03-19 23:56:24 -04:00
parent 486da9870a
commit e5b9601f76
10 changed files with 122 additions and 17 deletions

View File

@@ -10,7 +10,8 @@
]
},
"scripts": {
"test": "node ./node_modules/jest/bin/jest.js src/tests",
"test": "jest --config src/tests/jest.config.js",
"test:int": "jest --config src/tests/jest.config.integration.js",
"cov": "npm run core:build && node ./node_modules/jest/bin/jest.js src/tests --coverage",
"webpack": "webpack-dev-server --mode development --config config/webpack.dev.config.js --open --hot --history-api-fallback",
"demo-server": "nodemon demo/init.js",
@@ -56,6 +57,7 @@
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"autoprefixer": "^9.0.1",
"axios": "^0.18.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.4",
@@ -66,6 +68,7 @@
"eslint-plugin-react": "^7.11.1",
"express": "^4.16.4",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"faker": "^4.1.0",
"html-webpack-plugin": "^3.2.0",
"jest": "^23.6.0",
"jest-mock-express": "^0.1.1",
@@ -85,13 +88,5 @@
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"webpack-merge": "^4.1.3"
},
"jest": {
"verbose": true,
"testURL": "http://localhost/",
"transform": {
"^.+\\.(j|t)s$": "babel-jest"
},
"testEnvironment": "node"
}
}

View File

@@ -0,0 +1,91 @@
import axios from 'axios';
import * as faker from 'faker';
describe('API', () => {
let token = null;
beforeAll(async () => {
const loginResponse = await axios.post('http://localhost:3000/login', {
email: 'test@test.com',
password: 'test123'
});
token = loginResponse.data.token;
expect(loginResponse.status).toBe(200);
});
it('should allow create user', async () => {
const email = `${faker.name.firstName()}@test.com`;
const createResponse = await axios.post('http://localhost:3000/users', {
email: `${email}`,
password: 'test123'
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(createResponse.status).toBe(200);
expect(createResponse.data).toHaveProperty('email');
expect(createResponse.data).toHaveProperty('role');
expect(createResponse.data).toHaveProperty('createdAt');
});
it('should allow create page', async () => {
const createResponse = await axios.post('http://localhost:3000/pages', {
title: faker.name.firstName(),
content: faker.lorem.lines(20)
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(createResponse.status).toBe(201);
expect(createResponse.data.result.title).not.toBeNull();
});
it('should allow create page - locale', async () => {
const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
title: `English-${faker.name.firstName()}`,
content: faker.lorem.lines(20)
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(englishCreateResponse.status).toBe(201);
const id = englishCreateResponse.data.result.id;
const spanishCreateResponse = await axios.put(`http://localhost:3000/pages/${id}?locale=es`, {
title: `Spanish-${faker.name.firstName()}`,
content: faker.lorem.lines(20)
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(spanishCreateResponse.status).toBe(200);
});
it('should allow querying of page', async () => {
const title = `English-${faker.name.firstName()}`;
const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
title: title,
content: faker.lorem.lines(20)
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(englishCreateResponse.status).toBe(201);
const getResponse = await axios.get(`http://localhost:3000/pages?title=${title}`,
{ headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(getResponse.data.totalDocs).toEqual(1);
expect(getResponse.data.docs[0].title).toEqual(title);
});
it('should allow querying of page - locale', async () => {
const englishTitle = `English-${faker.name.firstName()}`;
const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
title: englishTitle,
content: faker.lorem.lines(20)
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(englishCreateResponse.status).toBe(201);
const id = englishCreateResponse.data.result.id;
const spanishTitle = `Spanish-${faker.name.firstName()}`;
const spanishCreateResponse = await axios.put(`http://localhost:3000/pages/${id}?locale=es`, {
title: spanishTitle,
content: faker.lorem.lines(20)
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(spanishCreateResponse.status).toBe(200);
const getResponse1 = await axios.get(`http://localhost:3000/pages?title=${spanishTitle}`,
{ headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(getResponse1.data.totalDocs).toEqual(0);
const getResponse2 = await axios.get(`http://localhost:3000/pages?title=${spanishTitle}&locale=es`,
{ headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(getResponse2.data.totalDocs).toEqual(1);
expect(getResponse2.data.docs[0].title).toEqual(spanishTitle);
});
});

View File

@@ -0,0 +1,8 @@
const { defaults } = require('./jest.config');
module.exports = {
...defaults,
roots: [
'./integration'
],
};

11
src/tests/jest.config.js Normal file
View File

@@ -0,0 +1,11 @@
module.exports = {
verbose: true,
testURL: 'http://localhost/',
roots: [
'./unit'
],
transform: {
'^.+\\.(j|t)s$': 'babel-jest'
},
testEnvironment: 'node'
};

View File

@@ -1,6 +1,6 @@
import middleware from '../middleware';
import middleware from '../../middleware';
import mockExpress from 'jest-mock-express';
import locale from '../middleware/locale';
import locale from '../../middleware/locale';
let res = null;
let next = null;

View File

@@ -1,6 +1,6 @@
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
import mongooseApiQuery from '../plugins/buildQuery';
import mongooseApiQuery from '../../plugins/buildQuery';
const TestUserSchema = new Schema({
name: {type: String},

View File

@@ -3,7 +3,7 @@ import mongoose from 'mongoose';
const Schema = mongoose.Schema;
import { intlModel } from './testModels/IntlModel';
import { paramParser } from '../plugins/buildQuery';
import { paramParser } from '../../plugins/buildQuery';
const AuthorSchema = new Schema({
name: String,

View File

@@ -1,8 +1,8 @@
import mongoose from 'mongoose';
import { schemaBaseFields } from '../../helpers/mongoose/schemaBaseFields';
import paginate from '../../plugins/paginate';
import buildQuery from '../../plugins/buildQuery';
import internationalization from '../../plugins/internationalization';
import { schemaBaseFields } from '../../../helpers/mongoose/schemaBaseFields';
import paginate from '../../../plugins/paginate';
import buildQuery from '../../../plugins/buildQuery';
import internationalization from '../../../plugins/internationalization';
const IntlSchema = new mongoose.Schema({
...schemaBaseFields,