From a8e5b3f7fff6b3be19907f6f131132c3fb245df6 Mon Sep 17 00:00:00 2001 From: Roy Palkovitch Date: Tue, 10 Jan 2023 10:34:58 +0200 Subject: [PATCH 1/4] Merge branch 'main' of https://github.com/grunitech/nodejs-ts-express-lecture --- package.json | 3 ++- src/users/index.test.ts | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) 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/users/index.test.ts b/src/users/index.test.ts index a017746..a85f50e 100644 --- a/src/users/index.test.ts +++ b/src/users/index.test.ts @@ -17,22 +17,33 @@ import request from 'supertest'; // 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' } ] }); return request(app) .get('/user') .expect(200) .expect([ - {id: 1}, - {id: 2} + { id: 1 }, + { id: 2 } ]); }); + it('should return one user', () => { + // return Promise of "PG Result object" + MockClient.query = () => Promise.resolve({ + rows: [{ id: '1', password: 'A' }] + }); + return request(app) + .get('/user/1') + .expect(200) + .expect( + { id: '1' } + ); + }); }); From 31dc961bfd42a6da5263a0d9afa0d6941bcde0c5 Mon Sep 17 00:00:00 2001 From: Roy Palkovitch Date: Tue, 10 Jan 2023 10:39:19 +0200 Subject: [PATCH 2/4] "changed string to number" --- src/users/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/users/index.test.ts b/src/users/index.test.ts index a85f50e..7b96283 100644 --- a/src/users/index.test.ts +++ b/src/users/index.test.ts @@ -36,13 +36,13 @@ describe('user feature', () => { it('should return one user', () => { // 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 } ); }); From 613a0d2c4250010525fb99768c4e6f7daf43fc2d Mon Sep 17 00:00:00 2001 From: Roy Palkovitch Date: Tue, 10 Jan 2023 11:00:21 +0200 Subject: [PATCH 3/4] "bla" --- src/users/index.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/users/index.test.ts b/src/users/index.test.ts index 7b96283..1c83158 100644 --- a/src/users/index.test.ts +++ b/src/users/index.test.ts @@ -34,7 +34,6 @@ describe('user feature', () => { ]); }); it('should return one user', () => { - // return Promise of "PG Result object" MockClient.query = () => Promise.resolve({ rows: [{ id: 1, password: 'A' }] }); From 2605087c87f8a3a0551eb50a5b2ef87dbb3cd9c2 Mon Sep 17 00:00:00 2001 From: Roy Palkovitch Date: Tue, 10 Jan 2023 11:23:32 +0200 Subject: [PATCH 4/4] add delete test --- src/services/users-service.ts | 8 ++++---- src/users/index.test.ts | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 14 deletions(-) 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 a4a85a6..fd84952 100644 --- a/src/users/index.test.ts +++ b/src/users/index.test.ts @@ -13,6 +13,7 @@ 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) @@ -37,10 +38,13 @@ describe('user feature', () => { { id: 2 } ]); }); - it('should return one user', () => { - MockClient.query = () => Promise.resolve({ - rows: [{ id: 1, password: 'A' }] - }); + + it('should return user by user Id', () => { + // return Promise of "PG Result object" + MockClient.query = () => Promise.resolve( + { rows: [{ id: 1, password: 'A' }] } + ); + return request(app) .get('/user/1') .expect(200) @@ -49,18 +53,19 @@ describe('user feature', () => { ); }); - it('should return user by user Id', () => { - // return Promise of "PG Result object" + + it('should return id of deleted user', () => { MockClient.query = () => Promise.resolve( - {rows:[{id: 1, password: 'A'}]} + { rows: [{ id: 1 }] } ); return request(app) - .get('/user/1') + .delete('/user/1') .expect(200) .expect( - {id: 1} - ); + { id: '1' } + ) + }); });