diff --git a/package.json b/package.json index 0f75c4e..b55eb31 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "test": "mocha -r ts-node/register src/**/*.test.ts", + "test-r": "mocha -r ts-node/register src/users/index.test.ts", "dev": "tsnd --respawn src/index.ts", "build": "tsc", "start": "npm run build && node dist/index.js", @@ -51,4 +52,4 @@ "pg": "^8.8.0", "validator": "^13.7.0" } -} +} \ No newline at end of file diff --git a/src/services/users-service.ts b/src/services/users-service.ts index f0eaa97..81faef0 100644 --- a/src/services/users-service.ts +++ b/src/services/users-service.ts @@ -35,7 +35,7 @@ export class UserService { return result.rows[0]; } - async save({email, fname, lname, password}: User) { + async save({ email, fname, lname, password }: User) { // NOTE: // this asserts make sure there is no another user with the same email // we comment this section out since we defined the "email" field in the database as unique @@ -49,15 +49,15 @@ export class UserService { } // todo missing tests - async update({id, email, fname, lname, password}: User) { + async update({ id, email, fname, lname, password }: User) { const results = await this.client.query(UPDATE_ONE, [id, email, fname, lname, password]); return results.rows[0]; } // todo missing tests async remove(id: number | string) { - await this.client.query(REMOVE_ONE, [id]); - return id; + await this.client.query(REMOVE_ONE, [id]); + return id; } } diff --git a/src/users/index.test.ts b/src/users/index.test.ts index 4cbfc53..f701c06 100644 --- a/src/users/index.test.ts +++ b/src/users/index.test.ts @@ -13,17 +13,17 @@ ImportMock.mockFunction(dbModule, 'default', MockClient); import app from '../app'; import request from 'supertest'; +import { User } from './user'; // E2E (end to end) testing // Integration test (integration of some modules instead of testing single unit) describe('user feature', () => { - it('should return all users', () => { // return Promise of "PG Result object" MockClient.query = () => Promise.resolve({ rows: [ - {id: 1, password: 'A'}, - {id: 2, password: 'C'} + { id: 1, password: 'A' }, + { id: 2, password: 'C' } ] }); // routing to /user @@ -34,22 +34,22 @@ describe('user feature', () => { .get('/user') .expect(200) .expect([ - {id: 1}, - {id: 2} + { id: 1 }, + { id: 2 } ]); }); it('should return user by user Id', () => { // return Promise of "PG Result object" MockClient.query = () => Promise.resolve( - {rows:[{id: 1, password: 'A'}]} + { rows: [{ id: 1, password: 'A' }] } ); return request(app) .get('/user/1') .expect(200) .expect( - {id: 1} + { id: 1 } ); }); it ('should delete user by its id and send back the id', () => { @@ -65,4 +65,19 @@ MockClient.query = () => Promise.resolve( ); }); + + it('should return id of deleted user', () => { + MockClient.query = () => Promise.resolve( + { rows: [{ id: 1 }] } + ); + + return request(app) + .delete('/user/1') + .expect(200) + .expect( + { id: '1' } + ) + + }); + });