Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getUserById test #10

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -51,4 +52,4 @@
"pg": "^8.8.0",
"validator": "^13.7.0"
}
}
}
8 changes: 4 additions & 4 deletions src/services/users-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}

Expand Down
29 changes: 22 additions & 7 deletions src/users/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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', () => {
Expand All @@ -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' }
)

});

});