Skip to content
Draft
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
47 changes: 47 additions & 0 deletions tests/category.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const config = require('../server/config/config');
const request = require('supertest');
const app = require('../server/server');
const mongoose = require('mongoose');
const categoryModel = require('../server/models/category');

describe('Category API Tests... ', () => {

beforeAll(async (done) => {
await mongoose.connect(config.urlDB, { useNewUrlParser: true, useCreateIndex: true }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
done();
});
});

afterAll(() => {
mongoose.connection.close();
});

let newCategoryId;

it('should create a new category and return it', async () => {
const response = await request(app)
.post('/category')
.send({ description: 'Test Category' });

expect(response.status).toBe(201);
expect(response.body.category).toBeDefined();
expect(response.body.category.description).toBe('Test Category');
newCategoryId = response.body.category._id;
});

it('should get all categories', async () => {
const response = await request(app).get('/category');
expect(response.status).toBe(200);
expect(Array.isArray(response.body.categories)).toBe(true);
});

it('should delete the created category', async () => {
const response = await request(app)
.delete(`/category/${newCategoryId}`);
expect(response.status).toBe(200);
});
});
163 changes: 0 additions & 163 deletions tests/devarc.test.js

This file was deleted.

81 changes: 81 additions & 0 deletions tests/product.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require('dotenv').config({ path: 'tests/.env' });
const config = require('../server/config/config');
const request = require('supertest');
const app = require('../server/server');
const mongoose = require('mongoose');
const categoryModel = require('../server/models/category');

let token;
let testCategoryId;

describe('Devarc Test... ', () => {

beforeAll(async (done) => {
await mongoose.connect(config.urlDB, { useNewUrlParser: true, useCreateIndex: true }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
done();
});
});

beforeAll((done) => {
request(app)
.post('/login')
.send({
email: process.env.TEST_EMAIL,
password: process.env.TEST_PASSWORD,
})
.end((err, response) => {
token = response.body.token; // save the token!
done();
});
});

beforeAll(async () => {
const category = new categoryModel({ description: 'Test Category' });
const savedCategory = await category.save();
testCategoryId = savedCategory._id;
});

afterAll(async () => {
await categoryModel.findByIdAndRemove(testCategoryId);
mongoose.connection.close();
});


test('should return status 200 and text message on the initial page', async () => {
const res = await request(app).get('/');
expect(res.statusCode).toEqual(200);
expect(res.text).toBe('Devarc is online');
});

test('should return status 200 and return products', async () => {
const res = await request(app).get('/products/');
expect(res.statusCode).toEqual(200);
expect(res.body.numProductos).toBeGreaterThan(0);
});

test('should return status 400 for not found product', async () => {
const res = await request(app).get('/products/searchByID/AAA');
expect(res.statusCode).toEqual(400);
});

test('should create a new product', async () => {
const res = await request(app)
.post('/products')
.set('token', token)
.send({
name: 'Test Product',
price: 10,
priceCost: 5,
description: 'Test Description',
category: testCategoryId
});
expect(res.statusCode).toEqual(200);
expect(res.body.ok).toBe(true);
expect(res.body.productDB).toHaveProperty('_id');
});

})
Loading