diff --git a/api/server.spec.js b/api/server.spec.js index 699a001..25d2bea 100644 --- a/api/server.spec.js +++ b/api/server.spec.js @@ -1,15 +1,33 @@ /* eslint-disable no-undef */ const request = require('supertest'); const server = require('../api/server'); +const {jestTestOrder} = require('../consts'); -describe('server is up check', () => { - it('should return a 200', () => { - return request(server) - .get('/') - .then(res=> { - expect(res.status).toBe(200); - expect(res.body.server).toBe('server is up'); - }); +module.exports = { + serverSpecTest +}; + +function serverSpecTest() { + + describe('server is up check', () => { + + beforeAll( ()=>{ + jestTestOrder.push('serverSpecTest'); + }); + + it('should return a 200', () => { + return request(server) + .get('/') + .then(res=> { + expect(res.status).toBe(200); + expect(res.body.server).toBe('server is up'); + }); + }); }); -}); +} + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); +}); \ No newline at end of file diff --git a/auth/authRouter.spec.js b/auth/authRouter.spec.js index 061b6bb..d2e3b76 100644 --- a/auth/authRouter.spec.js +++ b/auth/authRouter.spec.js @@ -2,133 +2,148 @@ const db = require('../data/db-config'); const request = require('supertest'); const server = require('../api/server'); +const {jestTestOrder} = require('../consts'); -// ------------------- CLEARING DATABASE ---------------------- // -describe('authRouter', function() { - beforeAll(async () => { - await db('coaches').truncate(); - }); +module.exports = { + authRouterSpecTest +}; - // ------------------- REGISTER ENDPOINT ---------------------- // - describe('/auth/register', function() { - it ('should register a new coach and send back a token', () => { - return request(server) - .post('/auth/register') - .send({ first_name: 'Hello', last_name: 'World', email: 'helloworld@email.com', password: 'pass' }) - .then(res => { - expect(res.status).toBe(201); - expect(res.body.token).not.toBe(undefined); - }); - }); +function authRouterSpecTest() { - it ('should NOT register a user - first name not provided', () => { - return request(server) - .post('/auth/register') - .send({ last_name: 'World', email: 'helloworld@email.com', password: 'pass' }) - .then(res => { - expect(res.status).toBe(400); - expect(res.body.token).toBe(undefined); - }); + // ------------------- CLEARING DATABASE ---------------------- // + describe('authRouter', function() { + beforeAll(async () => { + jestTestOrder.push('authRouterSpecTest'); + await db('coaches').truncate(); }); - it ('should NOT register a user - last name not provided', () => { - return request(server) - .post('/auth/register') - .send({ first_name: 'Hello', email: 'helloworld@email.com', password: 'pass' }) - .then(res => { - expect(res.status).toBe(400); - expect(res.body.token).toBe(undefined); - }); - }); + // ------------------- REGISTER ENDPOINT ---------------------- // + describe('/auth/register', function() { + it ('should register a new coach and send back a token', () => { + return request(server) + .post('/auth/register') + .send({ first_name: 'Hello', last_name: 'World', email: 'helloworld@email.com', password: 'pass' }) + .then(res => { + expect(res.status).toBe(201); + expect(res.body.token).not.toBe(undefined); + }); + }); - it ('should NOT register a user - email not provided', () => { - return request(server) - .post('/auth/register') - .send({ first_name: 'Hello', last_name: 'World', password: 'pass' }) - .then(res => { - expect(res.status).toBe(400); - expect(res.body.token).toBe(undefined); - }); - }); + it ('should NOT register a user - first name not provided', () => { + return request(server) + .post('/auth/register') + .send({ last_name: 'World', email: 'helloworld@email.com', password: 'pass' }) + .then(res => { + expect(res.status).toBe(400); + expect(res.body.token).toBe(undefined); + }); + }); - it ('should NOT register a user - password not provided', () => { - return request(server) - .post('/auth/register') - .send({ first_name: 'Hello', last_name: 'World', email: 'helloworld@email.com' }) - .then(res => { - expect(res.status).toBe(400); - expect(res.body.token).toBe(undefined); - }); - }); + it ('should NOT register a user - last name not provided', () => { + return request(server) + .post('/auth/register') + .send({ first_name: 'Hello', email: 'helloworld@email.com', password: 'pass' }) + .then(res => { + expect(res.status).toBe(400); + expect(res.body.token).toBe(undefined); + }); + }); - it ('should NOT register a user - body not provided', () => { - return request(server) - .post('/auth/register') - .send({ }) - .then(res => { - expect(res.status).toBe(400); - expect(res.body.token).toBe(undefined); - }); - }); + it ('should NOT register a user - email not provided', () => { + return request(server) + .post('/auth/register') + .send({ first_name: 'Hello', last_name: 'World', password: 'pass' }) + .then(res => { + expect(res.status).toBe(400); + expect(res.body.token).toBe(undefined); + }); + }); - }); + it ('should NOT register a user - password not provided', () => { + return request(server) + .post('/auth/register') + .send({ first_name: 'Hello', last_name: 'World', email: 'helloworld@email.com' }) + .then(res => { + expect(res.status).toBe(400); + expect(res.body.token).toBe(undefined); + }); + }); - // ------------------- LOGIN ENDPOINT ---------------------- // - describe('/auth/login', function() { - it ('should login a user and send back a token', () => { - return request(server) - .post('/auth/login') - .send({ email: 'helloworld@email.com', password: 'pass' }) - .then(res => { - expect(res.status).toBe(200); - expect(res.body.token).not.toBe(undefined); - }); - }); + it ('should NOT register a user - body not provided', () => { + return request(server) + .post('/auth/register') + .send({ }) + .then(res => { + expect(res.status).toBe(400); + expect(res.body.token).toBe(undefined); + }); + }); - it ('should NOT login a user - wrong email', () => { - return request(server) - .post('/auth/login') - .send({ email: 'wrong@email.com', password: 'pass' }) - .expect(401); }); - it ('should NOT login a user - no email', () => { - return request(server) - .post('/auth/login') - .send({ password: 'pass' }) - .expect(400); - }); + // ------------------- LOGIN ENDPOINT ---------------------- // + describe('/auth/login', function() { + it ('should login a user and send back a token', () => { + return request(server) + .post('/auth/login') + .send({ email: 'helloworld@email.com', password: 'pass' }) + .then(res => { + expect(res.status).toBe(200); + expect(res.body.token).not.toBe(undefined); + }); + }); - it ('should NOT login a user - wrong password', () => { - return request(server) - .post('/auth/login') - .send({ email: 'helloworld@email.com', password: 'wrongpass' }) - .expect(401); - }); + it ('should NOT login a user - wrong email', () => { + return request(server) + .post('/auth/login') + .send({ email: 'wrong@email.com', password: 'pass' }) + .expect(401); + }); - it ('should NOT login a user - no password', () => { - return request(server) - .post('/auth/login') - .send({ email: 'helloworld@email.com' }) - .expect(400); + it ('should NOT login a user - no email', () => { + return request(server) + .post('/auth/login') + .send({ password: 'pass' }) + .expect(400); + }); + + it ('should NOT login a user - wrong password', () => { + return request(server) + .post('/auth/login') + .send({ email: 'helloworld@email.com', password: 'wrongpass' }) + .expect(401); + }); + + it ('should NOT login a user - no password', () => { + return request(server) + .post('/auth/login') + .send({ email: 'helloworld@email.com' }) + .expect(400); + }); + + it ('should NOT login a user - no body', () => { + return request(server) + .post('/auth/login') + .send({ }) + .expect(400); + }); }); + }); - it ('should NOT login a user - no body', () => { + // ------------------- GET ENDPOINT - AUTH GOOGLE ---------------------- // + describe('/auth/google', function() { + it ('should return 302', () => { return request(server) - .post('/auth/login') - .send({ }) - .expect(400); + .get('/auth/google') + .then(res => { + expect(res.status).toBe(302); + }); }); }); -}); - -// ------------------- GET ENDPOINT - AUTH GOOGLE ---------------------- // -describe('/auth/google', function() { - it ('should return 302', () => { - return request(server) - .get('/auth/google') - .then(res => { - expect(res.status).toBe(302); - }); - }); + +} + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/clients/clientProgramRouter.spec.js b/clients/clientProgramRouter.spec.js index 4eefa23..8538884 100644 --- a/clients/clientProgramRouter.spec.js +++ b/clients/clientProgramRouter.spec.js @@ -3,166 +3,185 @@ const request = require('supertest'); const server = require('../api/server'); const {seedForTests} = require('../seed_for_tests.spec'); +const {jestTestOrder} = require('../consts'); let token; -// ------------------- CLEARING DATABASE and Registering Account---------------------- // -describe('clientProgramRouter', function() { - beforeAll(seedForTests); +module.exports = { + clientProgramRouterSpecTest +}; + +function clientProgramRouterSpecTest() { + + // ------------------- CLEARING DATABASE and Registering Account---------------------- // + describe('clientProgramRouter', function() { + // beforeAll(seedForTests); + + beforeAll( async ()=>{ + jestTestOrder.push('clientProgramRouterSpecTest'); + await seedForTests(); + }); + + // ------------------- Post request ---------------------- // + + it ('should login', function() { + + //Login to receive token + return request(server) + .post('/auth/login') + .send({ email: 'as@mail.com', password: 'qaz' }) + .then(res => { + expect(res.status).toBe(200); + token = `Bearer ${res.body.token}`; + console.log('token', token); + }); + + }); + + it ('should add data to clients_programs db', function() { + + return request(server) + .post('/clients-programs') + .set('Authorization', token) + .send({program_id: 2, client_ids: [7, 8, 9]}) + + .then(res => { + expect(res.status).toBe(201); + }); + + }); + + it ('should not post since no token', function() { + + return request(server) + .post('/clients-programs') + .send({program_id: 2, client_ids: [7, 8, 9]}) + + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('should not add data to db because no body', function() { + + return request(server) + .post('/clients-programs') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('should not add data to db because client_id is the wrong field', function() { + + return request(server) + .post('/clients-programs') + .set('Authorization', token) + .send({program_id: 2, client_id: [7, 8, 9]}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('it should not add data to db because the clients provided do not belong to that coach', function() { + + return request(server) + .post('/clients-programs') + .set('Authorization', token) + .send({program_id: 2, client_ids: [6, 4, 5]}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('it should not add data to db because the program provided does not belong to that coach', function() { + + return request(server) + .post('/clients-programs') + .set('Authorization', token) + .send({program_id: 1, client_ids: [7, 8, 9]}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + + // ------------------- Get request for all clients for one coach ---------------------- // + + it('should get dashboard info', function() { + return request(server) + .get('/clients-programs/dashboard') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(200); + expect(res.body[0].name).toBe('progB'); + expect(res.body[5].first_name).toBe('clientFirstI'); + }); + }); + + it ('should not get any data since no token', function() { + return request(server) + .get('/clients-programs/dashboard') + .then(res => { + expect(res.status).toBe(400); + }); + }); + + // ------------------- Delete Request ---------------------- // + + it ('it should delete a client from a program or vice versa', function () { + return request(server) + .delete('/clients-programs') + .set('Authorization', token) + .send({program_id: 2, client_id: 9}) + .then(res => { + expect(res.status).toBe(200); + }); + }); + + it ('it should not delete a client from a program since no token', function () { + return request(server) + .delete('/clients-programs') + .send({program_id: 2, client_id: 9}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('it should not delete a client from a program since the client does not exist', function () { + return request(server) + .delete('/clients-programs') + .set('Authorization', token) + .send({program_id: 2, client_id: 17}) + .then(res => { + expect(res.status).toBe(404); + }); + }); + + it ('it should not delete a client from a program since the program does not exist', function () { + return request(server) + .delete('/clients-programs') + .set('Authorization', token) + .send({program_id: 5, client_id: 9}) + .then(res => { + expect(res.status).toBe(404); + }); + }); + + it ('it should not delete a client from a program since you do not have access', function () { + return request(server) + .delete('/clients-programs') + .set('Authorization', token) + .send({program_id: 1, client_id: 4}) + .then(res => { + expect(res.status).toBe(403); + }); + }); - // ------------------- Post request ---------------------- // - - it ('should login', function() { - - //Login to receive token - return request(server) - .post('/auth/login') - .send({ email: 'as@mail.com', password: 'qaz' }) - .then(res => { - expect(res.status).toBe(200); - token = `Bearer ${res.body.token}`; - console.log('token', token); - }); - - }); - - it ('should add data to clients_programs db', function() { - - return request(server) - .post('/clients-programs') - .set('Authorization', token) - .send({program_id: 2, client_ids: [7, 8, 9]}) - - .then(res => { - expect(res.status).toBe(201); - }); - - }); - - it ('should not post since no token', function() { - - return request(server) - .post('/clients-programs') - .send({program_id: 2, client_ids: [7, 8, 9]}) - - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('should not add data to db because no body', function() { - - return request(server) - .post('/clients-programs') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('should not add data to db because client_id is the wrong field', function() { - - return request(server) - .post('/clients-programs') - .set('Authorization', token) - .send({program_id: 2, client_id: [7, 8, 9]}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('it should not add data to db because the clients provided do not belong to that coach', function() { - - return request(server) - .post('/clients-programs') - .set('Authorization', token) - .send({program_id: 2, client_ids: [6, 4, 5]}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('it should not add data to db because the program provided does not belong to that coach', function() { - - return request(server) - .post('/clients-programs') - .set('Authorization', token) - .send({program_id: 1, client_ids: [7, 8, 9]}) - .then(res => { - expect(res.status).toBe(400); - }); }); - // ------------------- Get request for all clients for one coach ---------------------- // - - it('should get dashboard info', function() { - return request(server) - .get('/clients-programs/dashboard') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(200); - expect(res.body[0].name).toBe('progB'); - expect(res.body[5].first_name).toBe('clientFirstI'); - }); - }); - - it ('should not get any data since no token', function() { - return request(server) - .get('/clients-programs/dashboard') - .then(res => { - expect(res.status).toBe(400); - }); - }); - - // ------------------- Delete Request ---------------------- // - - it ('it should delete a client from a program or vice versa', function () { - return request(server) - .delete('/clients-programs') - .set('Authorization', token) - .send({program_id: 2, client_id: 9}) - .then(res => { - expect(res.status).toBe(200); - }); - }); - - it ('it should not delete a client from a program since no token', function () { - return request(server) - .delete('/clients-programs') - .send({program_id: 2, client_id: 9}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('it should not delete a client from a program since the client does not exist', function () { - return request(server) - .delete('/clients-programs') - .set('Authorization', token) - .send({program_id: 2, client_id: 17}) - .then(res => { - expect(res.status).toBe(404); - }); - }); - - it ('it should not delete a client from a program since the program does not exist', function () { - return request(server) - .delete('/clients-programs') - .set('Authorization', token) - .send({program_id: 5, client_id: 9}) - .then(res => { - expect(res.status).toBe(404); - }); - }); - - it ('it should not delete a client from a program since you do not have access', function () { - return request(server) - .delete('/clients-programs') - .set('Authorization', token) - .send({program_id: 1, client_id: 4}) - .then(res => { - expect(res.status).toBe(403); - }); - }); +} +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/clients/clients-model.spec.js b/clients/clients-model.spec.js index f75b0ad..38beac0 100644 --- a/clients/clients-model.spec.js +++ b/clients/clients-model.spec.js @@ -2,63 +2,83 @@ const db = require('../data/db-config'); const Clients = require('./clients-model'); const {seedForTests} = require('../seed_for_tests.spec'); +const {jestTestOrder} = require('../consts'); -describe('Clients model', () => { +module.exports = { + clientModelSpecTest +}; + +function clientModelSpecTest() { + + describe('Clients model', () => { // beforeAll(async () => { // await db('clients').truncate(); // }); - beforeAll(seedForTests); - describe('add client', () => { - it('add a client into the db', async () => { - let clientArray; - clientArray = await db('clients'); - expect(clientArray).toHaveLength(9); - await Clients.addClient({first_name:'client', last_name:'dude',email: 'client@mail.com',coach_id: 1}); - clientArray = await db('clients'); + // beforeAll(seedForTests); + + beforeAll(async () => { + jestTestOrder.push('clientModelSpecTest'); + await seedForTests(); + }); + + describe('add client', () => { + it('add a client into the db', async () => { + let clientArray; + clientArray = await db('clients'); + expect(clientArray).toHaveLength(9); + await Clients.addClient({first_name:'client', last_name:'dude',email: 'client@mail.com',coach_id: 1}); + clientArray = await db('clients'); - expect(clientArray).toHaveLength(10); + expect(clientArray).toHaveLength(10); + }); }); - }); - describe('find client by id', () => { - it('find client by id', async () => { - let clientObtained = await Clients.getClientById(1); - expect(clientObtained.email).toBe('clienta@mail.com'); + describe('find client by id', () => { + it('find client by id', async () => { + let clientObtained = await Clients.getClientById(1); + expect(clientObtained.email).toBe('clienta@mail.com'); - expect(clientObtained.first_name).toBe('clientFirstA'); + expect(clientObtained.first_name).toBe('clientFirstA'); + }); }); - }); - describe('find all clients for that coach', () => { - it('find all clients for that coach', async () => { - await Clients.addClient({first_name:'client2', last_name:'dude2',email: 'client2@mail.com',coach_id: 1}); - let clientObtained = await Clients.getClients(1); + describe('find all clients for that coach', () => { + it('find all clients for that coach', async () => { + await Clients.addClient({first_name:'client2', last_name:'dude2',email: 'client2@mail.com',coach_id: 1}); + let clientObtained = await Clients.getClients(1); - expect(clientObtained).not.toBe(undefined); + expect(clientObtained).not.toBe(undefined); + }); }); - }); - describe('delete client', () => { - it('delete a client into the db', async () => { - let clientArray; - clientArray = await db('clients'); - expect(clientArray).toHaveLength(11); - await Clients.deleteClient(2); - clientArray = await db('clients'); - expect(clientArray).toHaveLength(10); + describe('delete client', () => { + it('delete a client into the db', async () => { + let clientArray; + clientArray = await db('clients'); + expect(clientArray).toHaveLength(11); + await Clients.deleteClient(2); + clientArray = await db('clients'); + expect(clientArray).toHaveLength(10); + }); }); - }); - describe('update client', () => { - it('update a client into the db', async () => { - let clientArray; - clientArray = await db('clients'); + describe('update client', () => { + it('update a client into the db', async () => { + let clientArray; + clientArray = await db('clients'); - await Clients.updateClient(3, {first_name:'joe', last_name:'bob',email: 'joebob@mail.com',coach_id: 1}); - clientArray = await Clients.getClientById(3); + await Clients.updateClient(3, {first_name:'joe', last_name:'bob',email: 'joebob@mail.com',coach_id: 1}); + clientArray = await Clients.getClientById(3); - expect(clientArray.first_name).toBe('joe'); + expect(clientArray.first_name).toBe('joe'); + }); }); + }); +} + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/clients/clientsRouter.spec.js b/clients/clientsRouter.spec.js index 813f18a..2dbc257 100644 --- a/clients/clientsRouter.spec.js +++ b/clients/clientsRouter.spec.js @@ -3,265 +3,284 @@ const db = require('../data/db-config'); const request = require('supertest'); const server = require('../api/server'); const {seedForTests} = require('../seed_for_tests.spec'); +const {jestTestOrder} = require('../consts'); let token ; let token2; -// ------------------- CLEARING DATABASE and Registering Account---------------------- // -describe('clientsRouter', function() { - - beforeAll(seedForTests); - - beforeAll(async() => { - - await request(server) - .post('/auth/register') - .send({ first_name: 'Hello2', last_name: 'World2', email: 'helloworld2@email.com', password: 'pass' }) - .then(res => { - expect(res.status).toBe(201); - token = `Bearer ${res.body.token}`; - }); - await request(server) - .post('/auth/register') - .send({ first_name: 'HelloClients', last_name: 'HelloClients', email: 'helloworldClients@email.com', password: 'pas2s' }) - .then(res => { - expect(res.status).toBe(201); - token2 = `Bearer ${res.body.token}`; - }); - await request(server) - .post('/clients') - .set('Authorization', token2) - .send({first_name:'terry',last_name:'yodo',email:'ty@gmail.com'}) - .then(res => { - expect(res.status).toBe(201); - }); - - }); - afterAll(async () => { - await new Promise(resolve => setTimeout(() => resolve(), 500)); // avoid jest open handle error - }); - // ------------------- Post request ---------------------- // - - it ('it should add data to clients db', function() { - - return request(server) - .post('/clients') - .set('Authorization', token) - .send({first_name:'tod',last_name:'Smith',email:'ts@gmail.com'}) - - .then(res => { - expect(res.status).toBe(201); - }); - - }); - - it ('it should not post since no token', function() { - - return request(server) - .post('/clients') - .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) - - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('it should not add data to db because no body', function() { - - return request(server) - .post('/clients') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not add data to db because wrong field', function() { - - return request(server) - .post('/clients') - .set('Authorization', token) - .send({first_names:'jerry',last_name:'Smiths',email:'js@gmail.com'}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('it should not add data to db because missing required field', function() { - - return request(server) - .post('/clients') - .set('Authorization', token) - .send({first_name:'',last_name:'Smiths',email:'js@gmail.com'}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - // ------------------- Get request for all clients for one coach ---------------------- // - - // // seeding a 2nd client - // it('it should add another client for get all', function() { - // return request(server) - // .post('/clients') - // .set('Authorization', token) - // .send({first_name:'tod2',last_name:'Smith2',email:'ts2@gmail.com'}) - - // .then(res => { - // expect(res.status).toBe(201); - // }); - // }); - it('getting 200 and data from clients route', function() { - return request(server) - .get('/clients') - .set('Authorization', token) - .then(res => { - console.log(res.body); - expect(res.status).toBe(200); - expect(res.body).not.toBe(undefined); - - }); - }); - - it ('it should not get since no token', function() { - - return request(server) - .get('/clients') - .then(res => { - expect(res.status).toBe(400); - }); - }); - - // ------------------- Get request for one clients for one coach ---------------------- // - it ('it should get a client', function() { - - return request(server) - .get('/clients/11') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(200); - expect(res.body).toMatchObject({first_name:'tod',last_name:'Smith',email:'ts@gmail.com'}); - }); - }); - - it ('it should not get since no token', function() { - - return request(server) - .get('/clients/1') - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not get a client since it does not exist', function() { - - return request(server) - .get('/clients/55') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(404); - }); +module.exports = { + clientsRouterSpecTest +}; + +function clientsRouterSpecTest() { + + // ------------------- CLEARING DATABASE and Registering Account---------------------- // + describe('clientsRouter', function() { + + // beforeAll(seedForTests); + + beforeAll(async () => { + jestTestOrder.push('clientsRouterSpecTest'); + await seedForTests(); + }); + + beforeAll(async() => { + + await request(server) + .post('/auth/register') + .send({ first_name: 'Hello2', last_name: 'World2', email: 'helloworld2@email.com', password: 'pass' }) + .then(res => { + expect(res.status).toBe(201); + token = `Bearer ${res.body.token}`; + }); + await request(server) + .post('/auth/register') + .send({ first_name: 'HelloClients', last_name: 'HelloClients', email: 'helloworldClients@email.com', password: 'pas2s' }) + .then(res => { + expect(res.status).toBe(201); + token2 = `Bearer ${res.body.token}`; + }); + await request(server) + .post('/clients') + .set('Authorization', token2) + .send({first_name:'terry',last_name:'yodo',email:'ty@gmail.com'}) + .then(res => { + expect(res.status).toBe(201); + }); + + }); + afterAll(async () => { + await new Promise(resolve => setTimeout(() => resolve(), 500)); // avoid jest open handle error + }); + // ------------------- Post request ---------------------- // + + it ('it should add data to clients db', function() { + + return request(server) + .post('/clients') + .set('Authorization', token) + .send({first_name:'tod',last_name:'Smith',email:'ts@gmail.com'}) + + .then(res => { + expect(res.status).toBe(201); + }); + + }); + + it ('it should not post since no token', function() { + + return request(server) + .post('/clients') + .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) + + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('it should not add data to db because no body', function() { + + return request(server) + .post('/clients') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not add data to db because wrong field', function() { + + return request(server) + .post('/clients') + .set('Authorization', token) + .send({first_names:'jerry',last_name:'Smiths',email:'js@gmail.com'}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('it should not add data to db because missing required field', function() { + + return request(server) + .post('/clients') + .set('Authorization', token) + .send({first_name:'',last_name:'Smiths',email:'js@gmail.com'}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + // ------------------- Get request for all clients for one coach ---------------------- // + + // // seeding a 2nd client + // it('it should add another client for get all', function() { + // return request(server) + // .post('/clients') + // .set('Authorization', token) + // .send({first_name:'tod2',last_name:'Smith2',email:'ts2@gmail.com'}) + + // .then(res => { + // expect(res.status).toBe(201); + // }); + // }); + it('getting 200 and data from clients route', function() { + return request(server) + .get('/clients') + .set('Authorization', token) + .then(res => { + console.log(res.body); + expect(res.status).toBe(200); + expect(res.body).not.toBe(undefined); + + }); + }); + + it ('it should not get since no token', function() { + + return request(server) + .get('/clients') + .then(res => { + expect(res.status).toBe(400); + }); + }); + + // ------------------- Get request for one clients for one coach ---------------------- // + it ('it should get a client', function() { + + return request(server) + .get('/clients/11') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(200); + expect(res.body).toMatchObject({first_name:'tod',last_name:'Smith',email:'ts@gmail.com'}); + }); + }); + + it ('it should not get since no token', function() { + + return request(server) + .get('/clients/1') + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not get a client since it does not exist', function() { + + return request(server) + .get('/clients/55') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(404); + }); + }); + + it ('it should not get an exercise since you do not have access', function() { + + return request(server) + .get('/clients/1') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(403); + }); + }); + + // ------------------- Put Request ---------------------- // + it ('it should update data to clients db', function() { + + return request(server) + .put('/clients/11') + .set('Authorization', token) + .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) + + .then(res => { + expect(res.status).toBe(200); + }); + }); + it ('it should not update data to clients db since no token', function() { + + return request(server) + .put('/clients/1') + .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not update data to clients db since no body', function() { + + return request(server) + .put('/clients/1') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not update data to clients db since wrong field', function() { + + return request(server) + .put('/clients/1') + .set('Authorization', token) + .send({first_names:'jerry',last_name:'Smiths',email:'js@gmail.com'}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not update data to clients db since it does not exist', function() { + + return request(server) + .put('/clients/55') + .set('Authorization', token) + .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) + .then(res => { + expect(res.status).toBe(404); + }); + }); + + it ('it should not update data to exercises db since no access', function() { + + return request(server) + .put('/clients/1') + .set('Authorization', token) + .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) + .then(res => { + expect(res.status).toBe(403); + }); + }); + // ------------------- Delete Request ---------------------- // + it ('it should delete a client', function () { + return request(server) + .delete('/clients/11') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(200); + }); + }); + + it ('it should not delete a client since no token', function () { + return request(server) + .delete('/clients/2') + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not delete a client since the client does not exist', function () { + return request(server) + .delete('/clients/55') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(404); + }); + }); + it ('it should not delete an exercise since no access', function () { + return request(server) + .delete('/clients/1') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(403); + }); + }); }); - it ('it should not get an exercise since you do not have access', function() { +} - return request(server) - .get('/clients/1') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(403); - }); - }); - - // ------------------- Put Request ---------------------- // - it ('it should update data to clients db', function() { - - return request(server) - .put('/clients/11') - .set('Authorization', token) - .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) - - .then(res => { - expect(res.status).toBe(200); - }); - }); - it ('it should not update data to clients db since no token', function() { - - return request(server) - .put('/clients/1') - .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not update data to clients db since no body', function() { - - return request(server) - .put('/clients/1') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not update data to clients db since wrong field', function() { - - return request(server) - .put('/clients/1') - .set('Authorization', token) - .send({first_names:'jerry',last_name:'Smiths',email:'js@gmail.com'}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not update data to clients db since it does not exist', function() { - - return request(server) - .put('/clients/55') - .set('Authorization', token) - .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) - .then(res => { - expect(res.status).toBe(404); - }); - }); - - it ('it should not update data to exercises db since no access', function() { - - return request(server) - .put('/clients/1') - .set('Authorization', token) - .send({first_name:'jerry',last_name:'Smiths',email:'js@gmail.com'}) - .then(res => { - expect(res.status).toBe(403); - }); - }); - // ------------------- Delete Request ---------------------- // - it ('it should delete a client', function () { - return request(server) - .delete('/clients/11') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(200); - }); - }); - - it ('it should not delete a client since no token', function () { - return request(server) - .delete('/clients/2') - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not delete a client since the client does not exist', function () { - return request(server) - .delete('/clients/55') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(404); - }); - }); - it ('it should not delete an exercise since no access', function () { - return request(server) - .delete('/clients/1') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(403); - }); - }); +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/coaches/coaches-model.spec.js b/coaches/coaches-model.spec.js index 4cd22f4..48a41e0 100644 --- a/coaches/coaches-model.spec.js +++ b/coaches/coaches-model.spec.js @@ -1,38 +1,52 @@ /* eslint-disable no-undef */ const db = require('../data/db-config'); const Coaches = require('./coaches-model'); +const {jestTestOrder} = require('../consts'); +module.exports = { + coachesModelSpecTest +}; -describe ('coaches model', () => { - beforeAll(async () => { - await db('coaches').truncate(); - }); +function coachesModelSpecTest() { + + describe ('coaches model', () => { + beforeAll(async () => { + jestTestOrder.push('coachesModelSpecTest'); + await db('coaches').truncate(); + }); - describe('add coach', () => { - it('add a coach into the db', async () => { - let coachesArray; - coachesArray = await db('coaches'); - expect(coachesArray).toHaveLength(0); - await Coaches.addCoach({email: 'test@mail.com', password:'Hello'}); - coachesArray = await db('coaches'); - expect(coachesArray).toHaveLength(1); + describe('add coach', () => { + it('add a coach into the db', async () => { + let coachesArray; + coachesArray = await db('coaches'); + expect(coachesArray).toHaveLength(0); + await Coaches.addCoach({email: 'test@mail.com', password:'Hello'}); + coachesArray = await db('coaches'); + expect(coachesArray).toHaveLength(1); + }); }); - }); - describe('find coach by email', () => { - it('find a coach by email', async () => { - let coachObtained = await Coaches.findCoachBy('test@mail.com'); - expect(coachObtained.password).toBe('Hello'); + describe('find coach by email', () => { + it('find a coach by email', async () => { + let coachObtained = await Coaches.findCoachBy('test@mail.com'); + expect(coachObtained.password).toBe('Hello'); + }); }); - }); - describe('find coach by id', () => { - it('find coach by id', async () => { - let coachObtained = await Coaches.getCoachById(1); - expect(coachObtained.email).toBe('test@mail.com'); - expect(coachObtained.password).toBe('Hello'); + describe('find coach by id', () => { + it('find coach by id', async () => { + let coachObtained = await Coaches.getCoachById(1); + expect(coachObtained.email).toBe('test@mail.com'); + expect(coachObtained.password).toBe('Hello'); + }); }); + }); +} + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/config/passport-setup.spec.js b/config/passport-setup.spec.js index 980b3c4..63dd9d3 100644 --- a/config/passport-setup.spec.js +++ b/config/passport-setup.spec.js @@ -2,65 +2,80 @@ const db = require('../data/db-config'); const Coaches = require('../coaches/coaches-model'); const googleStrt = require('./passport-setup'); +const {jestTestOrder} = require('../consts'); -describe ('test google strategy function', () => { - beforeAll(async () => { - await db('coaches').truncate(); - }); +module.exports = { + PassportSetupSpecTest +}; - describe('New google login will add in a new coach', () => { - it('a new google login', async () => { +function PassportSetupSpecTest() { - const usersMockA = { - name: {givenName:'HelloNew', familyName:'WorldNew'}, - _json: {email:'testGoog@mail.com'} - }; + describe ('test google strategy function', () => { + beforeAll(async () => { + jestTestOrder.push('PassportSetupSpecTest'); + await db('coaches').truncate(); + }); - let coachesObj = await - new Promise(function(resolve) { - function doneMock(a,b) { - resolve(b); - } - googleStrt(null,null,usersMockA,doneMock); - }); + describe('New google login will add in a new coach', () => { + it('a new google login', async () => { - expect(coachesObj.first_name).toBe('HelloNew'); - expect(coachesObj.last_name).toBe('WorldNew'); + const usersMockA = { + name: {givenName:'HelloNew', familyName:'WorldNew'}, + _json: {email:'testGoog@mail.com'} + }; + let coachesObj = await + new Promise(function(resolve) { + function doneMock(a,b) { + resolve(b); + } + googleStrt(null,null,usersMockA,doneMock); + }); + + expect(coachesObj.first_name).toBe('HelloNew'); + expect(coachesObj.last_name).toBe('WorldNew'); + + }); }); - }); - describe('Put in a new coach for the next test', () => { - it('add a new coach', async ()=> { - await Coaches.addCoach({ - email: 'abc@def.com', - password:'1234', - first_name: 'Sunny', - last_name: 'Day' + describe('Put in a new coach for the next test', () => { + it('add a new coach', async ()=> { + await Coaches.addCoach({ + email: 'abc@def.com', + password:'1234', + first_name: 'Sunny', + last_name: 'Day' + }); }); }); - }); - describe('Subsequent google login will find previously added coach', () => { - it('a repeat google login', async () => { + describe('Subsequent google login will find previously added coach', () => { + it('a repeat google login', async () => { - const usersMockB = { - _json: {email:'abc@def.com'} - }; + const usersMockB = { + _json: {email:'abc@def.com'} + }; - let coachesObj = await - new Promise(function(resolve) { - function doneMock(a,b) { - resolve(b); - } - googleStrt(null,null,usersMockB,doneMock); - }); + let coachesObj = await + new Promise(function(resolve) { + function doneMock(a,b) { + resolve(b); + } + googleStrt(null,null,usersMockB,doneMock); + }); - expect(coachesObj.first_name).toBe('Sunny'); - expect(coachesObj.last_name).toBe('Day'); - expect(coachesObj.password).toBe('1234'); + expect(coachesObj.first_name).toBe('Sunny'); + expect(coachesObj.last_name).toBe('Day'); + expect(coachesObj.password).toBe('1234'); + }); }); + }); +} + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/consts.js b/consts.js index a036299..0991025 100644 --- a/consts.js +++ b/consts.js @@ -1,6 +1,7 @@ module.exports = { hashRounds: 14, - jwtSecret: process.env.DB_JWTSECRET + jwtSecret: process.env.DB_JWTSECRET, + jestTestOrder: [] }; \ No newline at end of file diff --git a/data/workoutbuilder.db3 b/data/workoutbuilder.db3 index a1804ec..329454b 100644 Binary files a/data/workoutbuilder.db3 and b/data/workoutbuilder.db3 differ diff --git a/exercises/exercises-model.spec.js b/exercises/exercises-model.spec.js index 24f614e..eb0fb46 100644 --- a/exercises/exercises-model.spec.js +++ b/exercises/exercises-model.spec.js @@ -2,61 +2,82 @@ const db = require('../data/db-config'); const Exercises = require('./exercises-model'); const {seedForTests} = require('../seed_for_tests.spec'); -describe('exercises model', () => { +const {jestTestOrder} = require('../consts'); + +module.exports = { + ExercisesModelSpecTest +}; + +function ExercisesModelSpecTest() { + + describe('exercises model', () => { // beforeAll(async () => { // await db('exercises').truncate(); // }); - beforeAll(seedForTests); - describe('add exercise', () => { - it('add a exercise into the db', async () => { - let exerciseArray; - exerciseArray = await db('exercises'); - expect(exerciseArray).toHaveLength(7); - await Exercises.addExercise({name:'pull ups', coach_id: 1}); - exerciseArray = await db('exercises'); + // beforeAll(seedForTests); + + beforeAll(async () => { + jestTestOrder.push('ExercisesModelSpecTest'); + await seedForTests(); + }); + + describe('add exercise', () => { + it('add a exercise into the db', async () => { + let exerciseArray; + exerciseArray = await db('exercises'); + expect(exerciseArray).toHaveLength(7); + await Exercises.addExercise({name:'pull ups', coach_id: 1}); + exerciseArray = await db('exercises'); - expect(exerciseArray).toHaveLength(8); + expect(exerciseArray).toHaveLength(8); + }); }); - }); - describe('find exercise by id', () => { - it('find exercise by id', async () => { - let exerciseObtained = await Exercises.getExerciseById(8); - expect(exerciseObtained.name).toBe('pull ups'); + describe('find exercise by id', () => { + it('find exercise by id', async () => { + let exerciseObtained = await Exercises.getExerciseById(8); + expect(exerciseObtained.name).toBe('pull ups'); + }); }); - }); - describe('find all exercises for that coach', () => { - it('find all exercises for that coach', async () => { - await Exercises.addExercise({name:'push ups',coach_id: 1}); - let exerciseObtained = await Exercises.getExercises(1); + describe('find all exercises for that coach', () => { + it('find all exercises for that coach', async () => { + await Exercises.addExercise({name:'push ups',coach_id: 1}); + let exerciseObtained = await Exercises.getExercises(1); - expect(exerciseObtained).not.toBe(undefined); + expect(exerciseObtained).not.toBe(undefined); + }); }); - }); - describe('delete exercise', () => { - it('delete a exercise into the db', async () => { - let exerciseArray; - exerciseArray = await db('exercises'); - expect(exerciseArray).toHaveLength(9); - await Exercises.deleteExercise(2); - exerciseArray = await db('exercises'); - expect(exerciseArray).toHaveLength(8); + describe('delete exercise', () => { + it('delete a exercise into the db', async () => { + let exerciseArray; + exerciseArray = await db('exercises'); + expect(exerciseArray).toHaveLength(9); + await Exercises.deleteExercise(2); + exerciseArray = await db('exercises'); + expect(exerciseArray).toHaveLength(8); + }); }); - }); - describe('update exercise', () => { - it('update a exercise into the db', async () => { - let exerciseArray; - exerciseArray = await db('exercises'); + describe('update exercise', () => { + it('update a exercise into the db', async () => { + let exerciseArray; + exerciseArray = await db('exercises'); - await Exercises.updateExercise(1, {name:'sit ups',coach_id: 1}); - exerciseArray = await Exercises.getExerciseById(1); - console.log(exerciseArray); - expect(exerciseArray.name).toBe('sit ups'); + await Exercises.updateExercise(1, {name:'sit ups',coach_id: 1}); + exerciseArray = await Exercises.getExerciseById(1); + console.log(exerciseArray); + expect(exerciseArray.name).toBe('sit ups'); + }); }); + }); -}); \ No newline at end of file +} + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); +}); diff --git a/exercises/exercisesRouter.spec.js b/exercises/exercisesRouter.spec.js index 3c91454..a5873d0 100644 --- a/exercises/exercisesRouter.spec.js +++ b/exercises/exercisesRouter.spec.js @@ -1,269 +1,287 @@ /* eslint-disable no-undef */ -const db = require('../data/db-config'); +// const db = require('../data/db-config'); const request = require('supertest'); const server = require('../api/server'); const {seedForTests} = require('../seed_for_tests.spec'); +const {jestTestOrder} = require('../consts'); + let token ; let token2; -// ------------------- CLEARING DATABASE And Registering Account---------------------- // -describe('exercisesRouter', function() { - - beforeAll(seedForTests); - - beforeAll(async() => { - - await request(server) - .post('/auth/register') - .send({ first_name: 'Hello3', last_name: 'World3', email: 'helloworld3@email.com', password: 'pass' }) - .then(res => { - expect(res.status).toBe(201); - token = `Bearer ${res.body.token}`; - }); - await request(server) - .post('/auth/register') - .send({ first_name: 'HelloExercises', last_name: 'HelloExercises', email: 'helloworldExercises@email.com', password: 'pas2s' }) - .then(res => { - expect(res.status).toBe(201); - token2 = `Bearer ${res.body.token}`; - }); - await request(server) - .post('/exercises') - .set('Authorization', token2) - .send({name:'jumping jacks'}) - - .then(res => { - expect(res.status).toBe(201); - }); - - }); - // afterAll(async () => { - // await new Promise(resolve => setTimeout(() => resolve(), 500)); // avoid jest open handle error - // }); - // afterAll(()=>{ - // db.destroy(); - // }); - // ------------------- Post request ---------------------- // - - it ('it should add data to exercises db', function() { - - return request(server) - .post('/exercises') - .set('Authorization', token) - .send({name:'Burpees'}) - - .then(res => { - expect(res.status).toBe(201); - }); - - }); - - it ('it should not post since no token', function() { - - return request(server) - .post('/exercises') - .send({name:'Burpees'}) - - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('it should not add data to db because no body', function() { - - return request(server) - .post('/exercises') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not add data to db because wrong field', function() { - - return request(server) - .post('/exercises') - .set('Authorization', token) - .send({names:'Burpees'}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - - it ('it should not add data to db because missing required field', function() { +module.exports = { + ExerciseRouterSpecTest +}; + +function ExerciseRouterSpecTest() { + + // ------------------- CLEARING DATABASE And Registering Account---------------------- // + describe('exercisesRouter', function() { + + // beforeAll(seedForTests); + + beforeAll(async() => { + + jestTestOrder.push('ExerciseRouterSpecTest'); + await seedForTests(); + + await request(server) + .post('/auth/register') + .send({ first_name: 'Hello3', last_name: 'World3', email: 'helloworld3@email.com', password: 'pass' }) + .then(res => { + expect(res.status).toBe(201); + token = `Bearer ${res.body.token}`; + }); + await request(server) + .post('/auth/register') + .send({ first_name: 'HelloExercises', last_name: 'HelloExercises', email: 'helloworldExercises@email.com', password: 'pas2s' }) + .then(res => { + expect(res.status).toBe(201); + token2 = `Bearer ${res.body.token}`; + }); + await request(server) + .post('/exercises') + .set('Authorization', token2) + .send({name:'jumping jacks'}) + + .then(res => { + expect(res.status).toBe(201); + }); + + }); + // afterAll(async () => { + // await new Promise(resolve => setTimeout(() => resolve(), 500)); // avoid jest open handle error + // }); + // afterAll(()=>{ + // db.destroy(); + // }); + // ------------------- Post request ---------------------- // + + it ('it should add data to exercises db', function() { + + return request(server) + .post('/exercises') + .set('Authorization', token) + .send({name:'Burpees'}) + + .then(res => { + expect(res.status).toBe(201); + }); + + }); + + it ('it should not post since no token', function() { + + return request(server) + .post('/exercises') + .send({name:'Burpees'}) + + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('it should not add data to db because no body', function() { + + return request(server) + .post('/exercises') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not add data to db because wrong field', function() { + + return request(server) + .post('/exercises') + .set('Authorization', token) + .send({names:'Burpees'}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + + it ('it should not add data to db because missing required field', function() { + + return request(server) + .post('/exercises') + .set('Authorization', token) + .send({name:''}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + // ------------------- Get request for all exercises for one coach ---------------------- // + + // seeding a 2nd workout + // it('it should add another workout for get all', function() { + // return request(server) + // .post('/exercises') + // .set('Authorization', token) + // .send({name:'Burpees2'}) + + // .then(res => { + // expect(res.status).toBe(201); + // }); + // }); + it('getting 200 and data from exercises route', function() { + return request(server) + .get('/exercises') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(200); + expect(res.body).not.toBe(undefined); + + }); + }); + + it ('it should not get since no token', function() { + + return request(server) + .get('/exercises') + .then(res => { + expect(res.status).toBe(400); + }); + }); + + // ------------------- Get request for one exercises for one coach ---------------------- // + it ('it should get an exercise', function() { + + return request(server) + .get('/exercises/9') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(200); + expect(res.body).toMatchObject({ 'focal_points': null, 'id': 9, 'name': 'Burpees', 'thumbnail_url': null, 'type': null, 'video_url': null}); + }); + }); + + it ('it should not get since no token', function() { + + return request(server) + .get('/exercises/1') + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not get an exercise since it does not exist', function() { + + return request(server) + .get('/exercises/55') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(404); + }); + }); + it ('it should not get an exercise since you do not have access', function() { + + return request(server) + .get('/exercises/1') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(403); + }); + }); + + // ------------------- Put Request ---------------------- // + it ('it should update data to exercises db', function() { + + return request(server) + .put('/exercises/9') + .set('Authorization', token) + .send({name:'pushup'}) + + .then(res => { + expect(res.status).toBe(200); + }); + }); + it ('it should not update data to exercises db since no token', function() { + + return request(server) + .put('/exercises/1') + .send({name:'pushup'}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not update data to exercises db since no body', function() { + + return request(server) + .put('/exercises/1') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not update data to exercises db since wrong field', function() { + + return request(server) + .put('/exercises/1') + .set('Authorization', token) + .send({names:'pushup'}) + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not update data to exercises db since it does not exist', function() { + + return request(server) + .put('/exercises/55') + .set('Authorization', token) + .send({name:'pushup'}) + .then(res => { + expect(res.status).toBe(404); + }); + }); + it ('it should not update data to exercises db since no access', function() { + + return request(server) + .put('/exercises/1') + .set('Authorization', token) + .send({name:'pushup'}) + .then(res => { + expect(res.status).toBe(403); + }); + }); + + // ------------------- Delete Request ---------------------- // + it ('it should delete an exercise', function () { + return request(server) + .delete('/exercises/9') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(200); + }); + }); + + it ('it should not delete an exercise since no token', function () { + return request(server) + .delete('/exercises/2') + .then(res => { + expect(res.status).toBe(400); + }); + }); + it ('it should not delete an exercise since the exercise does not exist', function () { + return request(server) + .delete('/exercises/55') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(404); + }); + }); + it ('it should not delete an exercise since no access', function () { + return request(server) + .delete('/exercises/1') + .set('Authorization', token) + .then(res => { + expect(res.status).toBe(403); + }); + }); - return request(server) - .post('/exercises') - .set('Authorization', token) - .send({name:''}) - .then(res => { - expect(res.status).toBe(400); - }); }); - // ------------------- Get request for all exercises for one coach ---------------------- // - - // seeding a 2nd workout - // it('it should add another workout for get all', function() { - // return request(server) - // .post('/exercises') - // .set('Authorization', token) - // .send({name:'Burpees2'}) - - // .then(res => { - // expect(res.status).toBe(201); - // }); - // }); - it('getting 200 and data from exercises route', function() { - return request(server) - .get('/exercises') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(200); - expect(res.body).not.toBe(undefined); - - }); - }); - - it ('it should not get since no token', function() { - - return request(server) - .get('/exercises') - .then(res => { - expect(res.status).toBe(400); - }); - }); - - // ------------------- Get request for one exercises for one coach ---------------------- // - it ('it should get an exercise', function() { - return request(server) - .get('/exercises/9') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(200); - expect(res.body).toMatchObject({ 'focal_points': null, 'id': 9, 'name': 'Burpees', 'thumbnail_url': null, 'type': null, 'video_url': null}); - }); - }); - - it ('it should not get since no token', function() { - - return request(server) - .get('/exercises/1') - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not get an exercise since it does not exist', function() { - - return request(server) - .get('/exercises/55') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(404); - }); - }); - it ('it should not get an exercise since you do not have access', function() { - - return request(server) - .get('/exercises/1') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(403); - }); - }); - - // ------------------- Put Request ---------------------- // - it ('it should update data to exercises db', function() { - - return request(server) - .put('/exercises/9') - .set('Authorization', token) - .send({name:'pushup'}) - - .then(res => { - expect(res.status).toBe(200); - }); - }); - it ('it should not update data to exercises db since no token', function() { - - return request(server) - .put('/exercises/1') - .send({name:'pushup'}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not update data to exercises db since no body', function() { - - return request(server) - .put('/exercises/1') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not update data to exercises db since wrong field', function() { - - return request(server) - .put('/exercises/1') - .set('Authorization', token) - .send({names:'pushup'}) - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not update data to exercises db since it does not exist', function() { - - return request(server) - .put('/exercises/55') - .set('Authorization', token) - .send({name:'pushup'}) - .then(res => { - expect(res.status).toBe(404); - }); - }); - it ('it should not update data to exercises db since no access', function() { - - return request(server) - .put('/exercises/1') - .set('Authorization', token) - .send({name:'pushup'}) - .then(res => { - expect(res.status).toBe(403); - }); - }); - - // ------------------- Delete Request ---------------------- // - it ('it should delete an exercise', function () { - return request(server) - .delete('/exercises/9') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(200); - }); - }); - - it ('it should not delete an exercise since no token', function () { - return request(server) - .delete('/exercises/2') - .then(res => { - expect(res.status).toBe(400); - }); - }); - it ('it should not delete an exercise since the exercise does not exist', function () { - return request(server) - .delete('/exercises/55') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(404); - }); - }); - it ('it should not delete an exercise since no access', function () { - return request(server) - .delete('/exercises/1') - .set('Authorization', token) - .then(res => { - expect(res.status).toBe(403); - }); - }); +} +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/index.spec.js b/index.spec.js new file mode 100644 index 0000000..303db3c --- /dev/null +++ b/index.spec.js @@ -0,0 +1,43 @@ +/* eslint-disable no-undef */ +/* eslint-disable no-multiple-empty-lines */ +const {serverSpecTest} = require('./api/server.spec'); +const {authRouterSpecTest} = require('./auth/authRouter.spec'); +const {clientProgramRouterSpecTest} = require('./clients/clientProgramRouter.spec'); +const {clientModelSpecTest} = require('./clients/clients-model.spec'); +const {clientsRouterSpecTest} = require('./clients/clientsRouter.spec'); +const {coachesModelSpecTest} = require('./coaches/coaches-model.spec'); +const {PassportSetupSpecTest} = require('./config/passport-setup.spec'); +const {ExercisesModelSpecTest} = require('./exercises/exercises-model.spec'); +const {ExerciseRouterSpecTest} = require('./exercises/exercisesRouter.spec'); +const {ProgramsModelSpecTest} = require('./programs/programs-model.spec'); +const {ProgramsRouterSpecTest} = require('./programs/programsRouter.spec'); +const {WorkoutsModelSpecTest} = require('./workouts/workouts-model.spec'); + + +// Run the tests in this order +serverSpecTest(); +authRouterSpecTest(); +clientProgramRouterSpecTest(); +clientModelSpecTest(); +clientsRouterSpecTest(); +coachesModelSpecTest(); +PassportSetupSpecTest(); +ExercisesModelSpecTest(); +ExerciseRouterSpecTest(); +ProgramsModelSpecTest(); +ProgramsRouterSpecTest(); + +//The last test must have an afterAll that destroys the knex connection +// to prevent jest from not terminating properly +WorkoutsModelSpecTest(); + + + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); +}); + + + +// const {} = require('.'); \ No newline at end of file diff --git a/package.json b/package.json index 80c3bd9..03eae7f 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "scripts": { "server": "nodemon index.js", "start": "node index.js", - "test": "jest --detectOpenHandles --forceExit ", - "coverage": "jest --coverage --detectOpenHandles --forceExit || true" + "test": "jest", + "coverage": "jest --coverage" }, "jest": { "testEnvironment": "node", @@ -51,4 +51,4 @@ "pg": "^7.17.1", "sqlite3": "^4.1.1" } -} +} \ No newline at end of file diff --git a/programs/programs-model.spec.js b/programs/programs-model.spec.js index 94c4f85..1d639a4 100644 --- a/programs/programs-model.spec.js +++ b/programs/programs-model.spec.js @@ -1,90 +1,105 @@ /* eslint-disable no-multiple-empty-lines */ /* eslint-disable no-undef */ -const db = require('../data/db-config'); +// const db = require('../data/db-config'); const Programs = require('./programs-model'); const {seedForTests} = require('../seed_for_tests.spec'); +const {jestTestOrder} = require('../consts'); + let newProgramID; +module.exports = { + ProgramsModelSpecTest +}; -describe('programs models tests', ()=>{ - beforeAll( async ()=> { - await seedForTests(); - }); +function ProgramsModelSpecTest() { - test('get programs by coach id', async ()=> { - const programsArray = await Programs.getPrograms(1); - expect(programsArray).toEqual([ - { - id: 2, - name: 'progB', - phase: 'progB phase', - description: 'progB desc', - length: 6, - coach_id: 1 - } - ]); - }); + describe('programs models tests', ()=>{ + beforeAll( async ()=> { + jestTestOrder.push('ProgramsModelSpecTest'); + await seedForTests(); + }); - test('get programs by id', async ()=> { - const programsArray = await Programs.getProgramById(1); - expect(programsArray).toEqual( - { - id: 1, - name: 'progA', - phase: 'progA phase', - description: 'progA desc', - length: 5, - coach_id: 2 - } - ); - }); + test('get programs by coach id', async ()=> { + const programsArray = await Programs.getPrograms(1); + expect(programsArray).toEqual([ + { + id: 2, + name: 'progB', + phase: 'progB phase', + description: 'progB desc', + length: 6, + coach_id: 1 + } + ]); + }); - test('add a program', async ()=>{ - const newProgram = {name: 'progFromTest', phase:'progFT phase', description:'progFT desc', length: 22, coach_id:1}; - const programsArray = await Programs.addProgram(newProgram); - newProgramID = programsArray.id; - expect(programsArray).toEqual( - { - id: 3, - name: 'progFromTest', - phase: 'progFT phase', - description: 'progFT desc', - length: 22, - coach_id: 1 - } - ); - }); + test('get programs by id', async ()=> { + const programsArray = await Programs.getProgramById(1); + expect(programsArray).toEqual( + { + id: 1, + name: 'progA', + phase: 'progA phase', + description: 'progA desc', + length: 5, + coach_id: 2 + } + ); + }); - test('update a program', async()=>{ - const updatedProgram = {name: 'progFromTest-new', phase:'progFT phaseNew', description:'progFT descNew', length: 23, coach_id:1}; - const programsArray = await Programs.updateProgram(newProgramID,updatedProgram); - // console.log('this is in programs',programsArray); - expect(programsArray).toEqual( - { - id: 3, - name: 'progFromTest-new', - phase: 'progFT phaseNew', - description: 'progFT descNew', - length: 23, - coach_id: 1 - } - ); - }); + test('add a program', async ()=>{ + const newProgram = {name: 'progFromTest', phase:'progFT phase', description:'progFT desc', length: 22, coach_id:1}; + const programsArray = await Programs.addProgram(newProgram); + newProgramID = programsArray.id; + expect(programsArray).toEqual( + { + id: 3, + name: 'progFromTest', + phase: 'progFT phase', + description: 'progFT desc', + length: 22, + coach_id: 1 + } + ); + }); + + test('update a program', async()=>{ + const updatedProgram = {name: 'progFromTest-new', phase:'progFT phaseNew', description:'progFT descNew', length: 23, coach_id:1}; + const programsArray = await Programs.updateProgram(newProgramID,updatedProgram); + // console.log('this is in programs',programsArray); + expect(programsArray).toEqual( + { + id: 3, + name: 'progFromTest-new', + phase: 'progFT phaseNew', + description: 'progFT descNew', + length: 23, + coach_id: 1 + } + ); + }); - test('delete a program', async ()=>{ - const programsArray = await Programs.deleteProgram(newProgramID); - expect(programsArray).toEqual( - { - id: 3, - name: 'progFromTest-new', - phase: 'progFT phaseNew', - description: 'progFT descNew', - length: 23, - coach_id: 1 - } - ); + test('delete a program', async ()=>{ + const programsArray = await Programs.deleteProgram(newProgramID); + expect(programsArray).toEqual( + { + id: 3, + name: 'progFromTest-new', + phase: 'progFT phaseNew', + description: 'progFT descNew', + length: 23, + coach_id: 1 + } + ); + }); + }); +} + +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file diff --git a/programs/programsRouter.spec.js b/programs/programsRouter.spec.js index 438ce54..eed7df9 100644 --- a/programs/programsRouter.spec.js +++ b/programs/programsRouter.spec.js @@ -4,282 +4,297 @@ const db = require('../data/db-config'); const request = require('supertest'); const server = require('../api/server'); const {seedForTests} = require('../seed_for_tests.spec'); +const {jestTestOrder} = require('../consts'); let token; -describe('programs router tests', ()=>{ - beforeAll( async ()=> { - await seedForTests(); - }); +module.exports = { + ProgramsRouterSpecTest +}; +function ProgramsRouterSpecTest() { - test('login a coach to get a token', async ()=>{ - const res = await request(server).post('/auth/login').send({ email: 'as@mail.com', password: 'qaz' }); - expect(res.status).toBe(200); - token = `Bearer ${res.body.token}`; - }); + describe('programs router tests', ()=>{ + beforeAll( async ()=> { + jestTestOrder.push('ProgramsRouterSpecTest'); + await seedForTests(); + }); - test('get programs by coach id', async ()=>{ - const res = await request(server).get('/programs').set('Authorization', token); - expect(res.status).toBe(200); - expect(res.body).toEqual([ - { - 'id': 2, - 'name': 'progB', - 'phase': 'progB phase', - 'description': 'progB desc', - 'length': 6, - 'coach_id': 1, - 'workouts': [ - { - 'id': 1, - 'name': 'workA', - 'description': 'workA desc', - 'day': 1, - 'exercises': [ - { - 'exercise_id': 4, - 'order': 1, - 'exercise_details': 'ex details1' - }, - { - 'exercise_id': 3, - 'order': 2, - 'exercise_details': 'ex details2' - } - ] - }, - { - 'id': 2, - 'name': 'workB', - 'description': 'workB desc', - 'day': 2, - 'exercises': [ - { - 'exercise_id': 1, - 'order': 1, - 'exercise_details': 'ex details3' - }, - { - 'exercise_id': 2, - 'order': 2, - 'exercise_details': 'ex details4', - } - ] - } - ], - 'assigned_clients': [ - 3, - 1, - 2 - ] - } - ]); - }); - test('get programs by coach id', async ()=>{ - const res = await request(server).post('/programs').set('Authorization', token) - .send({ - 'name': 'progC', - 'description': 'progC desc', - 'length': 7, - 'phase': 'progC phase', - 'workouts': [ - { - 'name': 'work1', - 'description': 'work1 desc', - 'day': 1, - 'exercises': [ - {'id': 1, 'order':1, 'exercise_details': 'exD1_work1'}, - {'id': 2, 'order':2, 'exercise_details': 'exD2_work1'}, - {'id': 3, 'order':3, 'exercise_details': 'exD3_work1'} - ] - }, - - { - 'name': 'work2', - 'description': 'work2 desc', - 'day': 2, - 'exercises': [ - {'id': 4, 'order':1, 'exercise_details': 'exD4_work2'}, - {'id': 1, 'order':2, 'exercise_details': 'exD1_work2'}, - {'id': 2, 'order':3, 'exercise_details': 'exD2_work2'} - ] - }, - - { - 'name': 'work3', - 'description': 'work3 desc', - 'day': 3, - 'exercises': [ - {'id': 3, 'order':1, 'exercise_details': 'exD3_work3'}, - {'id': 1, 'order':2, 'exercise_details': 'exD1_work3'} - ] - } - ] - }); - expect(res.status).toBe(201); - // console.log('This is point 2 res.body:',JSON.stringify(res.body, undefined, 2)); - expect(res.body).toEqual([ - { - 'id': 2, - 'name': 'progB', - 'phase': 'progB phase', - 'description': 'progB desc', - 'length': 6, - 'coach_id': 1, - 'workouts': [ - { - 'id': 1, - 'name': 'workA', - 'description': 'workA desc', - 'day': 1, - 'exercises': [ - { - 'exercise_id': 4, - 'order': 1, - 'exercise_details': 'ex details1' - }, - { - 'exercise_id': 3, - 'order': 2, - 'exercise_details': 'ex details2' - } - ] - }, - { - 'id': 2, - 'name': 'workB', - 'description': 'workB desc', - 'day': 2, - 'exercises': [ - { - 'exercise_id': 1, - 'order': 1, - 'exercise_details': 'ex details3' - }, - { - 'exercise_id': 2, - 'order': 2, - 'exercise_details': 'ex details4' - } - ] - } - ], - 'assigned_clients': [ - 3, - 1, - 2 - ] - }, - { + test('login a coach to get a token', async ()=>{ + const res = await request(server).post('/auth/login').send({ email: 'as@mail.com', password: 'qaz' }); + expect(res.status).toBe(200); + token = `Bearer ${res.body.token}`; + }); + + + test('get programs by coach id', async ()=>{ + const res = await request(server).get('/programs').set('Authorization', token); + expect(res.status).toBe(200); + expect(res.body).toEqual([ + { + 'id': 2, + 'name': 'progB', + 'phase': 'progB phase', + 'description': 'progB desc', + 'length': 6, + 'coach_id': 1, + 'workouts': [ + { + 'id': 1, + 'name': 'workA', + 'description': 'workA desc', + 'day': 1, + 'exercises': [ + { + 'exercise_id': 4, + 'order': 1, + 'exercise_details': 'ex details1' + }, + { + 'exercise_id': 3, + 'order': 2, + 'exercise_details': 'ex details2' + } + ] + }, + { + 'id': 2, + 'name': 'workB', + 'description': 'workB desc', + 'day': 2, + 'exercises': [ + { + 'exercise_id': 1, + 'order': 1, + 'exercise_details': 'ex details3' + }, + { + 'exercise_id': 2, + 'order': 2, + 'exercise_details': 'ex details4', + } + ] + } + ], + 'assigned_clients': [ + 3, + 1, + 2 + ] + } + ]); + }); + + + test('get programs by coach id', async ()=>{ + const res = await request(server).post('/programs').set('Authorization', token) + .send({ + 'name': 'progC', + 'description': 'progC desc', + 'length': 7, + 'phase': 'progC phase', + 'workouts': [ + { + 'name': 'work1', + 'description': 'work1 desc', + 'day': 1, + 'exercises': [ + {'id': 1, 'order':1, 'exercise_details': 'exD1_work1'}, + {'id': 2, 'order':2, 'exercise_details': 'exD2_work1'}, + {'id': 3, 'order':3, 'exercise_details': 'exD3_work1'} + ] + }, + + { + 'name': 'work2', + 'description': 'work2 desc', + 'day': 2, + 'exercises': [ + {'id': 4, 'order':1, 'exercise_details': 'exD4_work2'}, + {'id': 1, 'order':2, 'exercise_details': 'exD1_work2'}, + {'id': 2, 'order':3, 'exercise_details': 'exD2_work2'} + ] + }, + + { + 'name': 'work3', + 'description': 'work3 desc', + 'day': 3, + 'exercises': [ + {'id': 3, 'order':1, 'exercise_details': 'exD3_work3'}, + {'id': 1, 'order':2, 'exercise_details': 'exD1_work3'} + ] + } + ] + }); + expect(res.status).toBe(201); + // console.log('This is point 2 res.body:',JSON.stringify(res.body, undefined, 2)); + expect(res.body).toEqual([ + { + 'id': 2, + 'name': 'progB', + 'phase': 'progB phase', + 'description': 'progB desc', + 'length': 6, + 'coach_id': 1, + 'workouts': [ + { + 'id': 1, + 'name': 'workA', + 'description': 'workA desc', + 'day': 1, + 'exercises': [ + { + 'exercise_id': 4, + 'order': 1, + 'exercise_details': 'ex details1' + }, + { + 'exercise_id': 3, + 'order': 2, + 'exercise_details': 'ex details2' + } + ] + }, + { + 'id': 2, + 'name': 'workB', + 'description': 'workB desc', + 'day': 2, + 'exercises': [ + { + 'exercise_id': 1, + 'order': 1, + 'exercise_details': 'ex details3' + }, + { + 'exercise_id': 2, + 'order': 2, + 'exercise_details': 'ex details4' + } + ] + } + ], + 'assigned_clients': [ + 3, + 1, + 2 + ] + }, + { + 'id': 3, + 'name': 'progC', + 'phase': 'progC phase', + 'description': 'progC desc', + 'length': 7, + 'coach_id': 1, + 'workouts': [ + { + 'id': 5, + 'name': 'work1', + 'description': 'work1 desc', + 'day': 1, + 'exercises': [ + { + 'exercise_id': 1, + 'order': 1, + 'exercise_details': 'exD1_work1' + }, + { + 'exercise_id': 2, + 'order': 2, + 'exercise_details': 'exD2_work1' + }, + { + 'exercise_id': 3, + 'order': 3, + 'exercise_details': 'exD3_work1' + } + ] + }, + { + 'id': 6, + 'name': 'work2', + 'description': 'work2 desc', + 'day': 2, + 'exercises': [ + { + 'exercise_id': 4, + 'order': 1, + 'exercise_details': 'exD4_work2' + }, + { + 'exercise_id': 1, + 'order': 2, + 'exercise_details': 'exD1_work2' + }, + { + 'exercise_id': 2, + 'order': 3, + 'exercise_details': 'exD2_work2' + } + ] + }, + { + 'id': 7, + 'name': 'work3', + 'description': 'work3 desc', + 'day': 3, + 'exercises': [ + { + 'exercise_id': 3, + 'order': 1, + 'exercise_details': 'exD3_work3' + }, + { + 'exercise_id': 1, + 'order': 2, + 'exercise_details': 'exD1_work3' + } + ] + } + ], + 'assigned_clients': [] + } + ]); + }); + + test('get programs by coach id', async ()=>{ + const res = await request(server).delete('/programs/3').set('Authorization', token); + expect(res.status).toBe(200); + expect(res.body).toEqual({ 'id': 3, 'name': 'progC', 'phase': 'progC phase', 'description': 'progC desc', 'length': 7, - 'coach_id': 1, - 'workouts': [ - { - 'id': 5, - 'name': 'work1', - 'description': 'work1 desc', - 'day': 1, - 'exercises': [ - { - 'exercise_id': 1, - 'order': 1, - 'exercise_details': 'exD1_work1' - }, - { - 'exercise_id': 2, - 'order': 2, - 'exercise_details': 'exD2_work1' - }, - { - 'exercise_id': 3, - 'order': 3, - 'exercise_details': 'exD3_work1' - } - ] - }, - { - 'id': 6, - 'name': 'work2', - 'description': 'work2 desc', - 'day': 2, - 'exercises': [ - { - 'exercise_id': 4, - 'order': 1, - 'exercise_details': 'exD4_work2' - }, - { - 'exercise_id': 1, - 'order': 2, - 'exercise_details': 'exD1_work2' - }, - { - 'exercise_id': 2, - 'order': 3, - 'exercise_details': 'exD2_work2' - } - ] - }, - { - 'id': 7, - 'name': 'work3', - 'description': 'work3 desc', - 'day': 3, - 'exercises': [ - { - 'exercise_id': 3, - 'order': 1, - 'exercise_details': 'exD3_work3' - }, - { - 'exercise_id': 1, - 'order': 2, - 'exercise_details': 'exD1_work3' - } - ] - } - ], - 'assigned_clients': [] - } - ]); - }); - - test('get programs by coach id', async ()=>{ - const res = await request(server).delete('/programs/3').set('Authorization', token); - expect(res.status).toBe(200); - expect(res.body).toEqual({ - 'id': 3, - 'name': 'progC', - 'phase': 'progC phase', - 'description': 'progC desc', - 'length': 7, - 'coach_id': 1 + 'coach_id': 1 + }); }); - }); - // console.log('This is point 1 res.body:',res.body); - // console.log('This is point 2 res.body:',JSON.stringify(res.body, undefined, 2)); + // console.log('This is point 1 res.body:',res.body); + // console.log('This is point 2 res.body:',JSON.stringify(res.body, undefined, 2)); -}); + }); +} +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); +}); diff --git a/workouts/workouts-model.spec.js b/workouts/workouts-model.spec.js index 5af1417..9ef02db 100644 --- a/workouts/workouts-model.spec.js +++ b/workouts/workouts-model.spec.js @@ -3,92 +3,112 @@ const db = require('../data/db-config'); const Workouts = require('./workouts-model'); const {seedForTests} = require('../seed_for_tests.spec'); +const {jestTestOrder} = require('../consts'); -describe('workouts model', () => { - beforeAll( async ()=>{ - await seedForTests(); - }); +module.exports = { + WorkoutsModelSpecTest +}; - describe('add workout', () => { - it('add a workout into the db', async () => { - let workoutArray; - workoutArray = await db('workouts'); - expect(workoutArray).toHaveLength(4); - await Workouts.addWorkout({name:'chest and back', description:'lots of push ups and pull ups',day: 1,coach_id: 1, program_id:1}); - workoutArray = await db('workouts'); +function WorkoutsModelSpecTest() { - expect(workoutArray).toHaveLength(5); + describe('workouts model', () => { + beforeAll( async ()=>{ + jestTestOrder.push('WorkoutsModelSpecTest'); + await seedForTests(); + }); + afterAll(()=>{ + db.destroy(); + console.log('The jestTestOrder array:',jestTestOrder); }); - }); - describe('find workouts by id', () => { - it('find workouts by id', async () => { - let workoutArray = await Workouts.getWorkoutById([5]); - - expect(workoutArray).toMatchObject([{ id: 5, - name: 'chest and back', - description: 'lots of push ups and pull ups', - day: 1, - coach_id: 1, - program_id: 1 }]); + + describe('add workout', () => { + it('add a workout into the db', async () => { + let workoutArray; + workoutArray = await db('workouts'); + expect(workoutArray).toHaveLength(4); + await Workouts.addWorkout({name:'chest and back', description:'lots of push ups and pull ups',day: 1,coach_id: 1, program_id:1}); + workoutArray = await db('workouts'); + + expect(workoutArray).toHaveLength(5); + + }); }); - }); - describe('find all workouts for that coach', () => { - it('find all workouts for that coach', async () => { + describe('find workouts by id', () => { + it('find workouts by id', async () => { + let workoutArray = await Workouts.getWorkoutById([5]); + + expect(workoutArray).toMatchObject([{ id: 5, + name: 'chest and back', + description: 'lots of push ups and pull ups', + day: 1, + coach_id: 1, + program_id: 1 }]); + }); + }); + describe('find all workouts for that coach', () => { + it('find all workouts for that coach', async () => { - let workoutsObtained = await Workouts.getWorkouts(1); + let workoutsObtained = await Workouts.getWorkouts(1); - expect(workoutsObtained).not.toBe(undefined); + expect(workoutsObtained).not.toBe(undefined); + }); }); - }); - describe('find all workouts for that Program ID', () => { - it('find all workouts for that Program ID', async () => { + describe('find all workouts for that Program ID', () => { + it('find all workouts for that Program ID', async () => { - let workoutsObtained = await Workouts.getWorkoutByProgramId(1); + let workoutsObtained = await Workouts.getWorkoutByProgramId(1); - expect(workoutsObtained).not.toBe(undefined); + expect(workoutsObtained).not.toBe(undefined); + }); }); - }); - describe('update workout', () => { - it('update a workout into the db', async () => { - let workoutsObtained; + describe('update workout', () => { + it('update a workout into the db', async () => { + let workoutsObtained; - await Workouts.updateWorkout(5, {name:'Yoga', description:'lots of yoga stuff',day: 1,coach_id: 1, program_id:1}); - workoutsObtained = await Workouts.getWorkoutById([5]); + await Workouts.updateWorkout(5, {name:'Yoga', description:'lots of yoga stuff',day: 1,coach_id: 1, program_id:1}); + workoutsObtained = await Workouts.getWorkoutById([5]); - expect(workoutsObtained).toMatchObject([{ id: 5, - name: 'Yoga', - description: 'lots of yoga stuff', - day: 1, - coach_id: 1, - program_id: 1 }]); + expect(workoutsObtained).toMatchObject([{ id: 5, + name: 'Yoga', + description: 'lots of yoga stuff', + day: 1, + coach_id: 1, + program_id: 1 }]); + }); }); - }); - describe('delete workout', () => { - it('delete a workouts into the db', async () => { - let workoutsArray; - workoutsArray = await db('workouts'); - expect(workoutsArray).toHaveLength(5); - await Workouts.deleteWorkout(5); - workoutsArray = await db('workouts'); - expect(workoutsArray).toHaveLength(4); + describe('delete workout', () => { + it('delete a workouts into the db', async () => { + let workoutsArray; + workoutsArray = await db('workouts'); + expect(workoutsArray).toHaveLength(5); + await Workouts.deleteWorkout(5); + workoutsArray = await db('workouts'); + expect(workoutsArray).toHaveLength(4); + }); }); - }); - test('get exercise_workout record by workout id', async ()=>{ - const receivedData = await Workouts.getExercisesByWorkoutId(1); - expect(receivedData).toEqual([ - { exercise_id: 4, order: 1, exercise_details: 'ex details1' }, - { exercise_id: 3, order: 2, exercise_details: 'ex details2' } - ]); - }); + test('get exercise_workout record by workout id', async ()=>{ + const receivedData = await Workouts.getExercisesByWorkoutId(1); + expect(receivedData).toEqual([ + { exercise_id: 4, order: 1, exercise_details: 'ex details1' }, + { exercise_id: 3, order: 2, exercise_details: 'ex details2' } + ]); + }); + + + test('delete exercise_workout record by exercise id & workout_id', async ()=>{ + const receivedData = await Workouts.deleteExerciseInWorkout([{exercise_id:6,workout_id:4}]); + expect(receivedData).toEqual(1); + }); - test('delete exercise_workout record by exercise id & workout_id', async ()=>{ - const receivedData = await Workouts.deleteExerciseInWorkout([{exercise_id:6,workout_id:4}]); - expect(receivedData).toEqual(1); }); +} +test('dummy',()=>{ + let dummy = true; + expect(dummy).toBe(true); }); \ No newline at end of file