Skip to content

Commit

Permalink
feat: Add unit tests for user registration
Browse files Browse the repository at this point in the history
  • Loading branch information
maxCastro1 committed May 1, 2024
1 parent 4c98039 commit bf2f29b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@types/jsend": "^1.0.32",
"@types/morgan": "^1.9.9",
"@types/node": "^20.12.7",
"@types/reflect-metadata": "^0.1.0",
"@types/supertest": "^6.0.2",
"@types/winston": "^2.4.4",
"@typescript-eslint/eslint-plugin": "^7.7.1",
Expand All @@ -67,6 +68,7 @@
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"jest-mock-extended": "^3.0.6",
"prettier": "^3.2.5",
"supertest": "^7.0.0",
"ts-jest": "^29.1.2",
Expand Down
3 changes: 3 additions & 0 deletions src/__test__/Decorators/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function myDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) {

Check warning on line 1 in src/__test__/Decorators/decorators.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function

Check warning on line 1 in src/__test__/Decorators/decorators.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing space before function parentheses

Check warning on line 1 in src/__test__/Decorators/decorators.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function

Check warning on line 1 in src/__test__/Decorators/decorators.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing space before function parentheses
console.log("myDecorator called on: ", target, propertyKey, descriptor);
}
58 changes: 53 additions & 5 deletions src/__test__/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,72 @@
import request from 'supertest';
import { app, server } from '../index'; // update this with the path to your app file


import { createConnection, getConnection, getConnectionOptions } from 'typeorm';
import { User } from '../entities/User';
import { getRepository, Repository } from 'typeorm';

Check warning on line 7 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'Repository' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 7 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'Repository' is defined but never used

Check warning on line 7 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'Repository' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 7 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'Repository' is defined but never used
import { mock, MockProxy } from 'jest-mock-extended';

Check warning on line 8 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'mock' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 8 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'mock' is defined but never used

Check warning on line 8 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'mock' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 8 in src/__test__/route.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'mock' is defined but never used



beforeAll(async () => {
// Connect to the test database
const connectionOptions = await getConnectionOptions();
await createConnection({ ...connectionOptions, name: 'testConnection' });
});

afterAll(async () => {
await getConnection('testConnection').close();
server.close();
});



describe('GET /', () => {
// afterAll(done => {
// server.close(done);
// });
it('This is a testing route that returns', done => {
request(app)
.get('/api/v1/status')
.expect(200)
.expect('Content-Type', /json/)
.expect({
status: 'success',
data: {
code: 202,
message: 'This is a testing route that returns: 201'
}
}, done);
});
});
describe('POST /user/register', () => {
it('should register a new user and then delete it', async () => {
// Arrange
const newUser = {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
password: 'password',
gender: 'Male',
phoneNumber: '1234567890',
userType: 'Buyer',
status: 'active',
verified: true,
photoUrl: 'https://example.com/photo.jpg',
};

// Act
const res = await request(app)
.post('/user/register')
.send(newUser);

// Assert
expect(res.status).toBe(201);
expect(res.body).toEqual({ message: 'User registered successfully' });

it('responds with "Knights Ecommerce API"', done => {
request(app).get('/').expect(200, 'Knights Ecommerce API', done);
// Clean up: delete the test user
const userRepository = getRepository(User);
const user = await userRepository.findOne({ where: { email: newUser.email } });
if (user) {
await userRepository.remove(user);
}
});
});
2 changes: 0 additions & 2 deletions src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class UserController {
static registerUser = async (req: Request, res: Response) => {
const { firstName, lastName, email, password, gender, phoneNumber, userType, status, verified, photoUrl } = req.body;



// Validate user input
if (!(firstName && lastName && email && password && gender && phoneNumber && verified && photoUrl)) {
return res.status(400).json({ error: 'Please fill all the fields' });
Expand Down
25 changes: 25 additions & 0 deletions testOrmconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
"type": "postgres",
"host": `${process.env.TEST_DB_HOST}`,
"port": `${process.env.TEST_DB_PORT}`,
"username": `${process.env.TEST_DB_USER}`,
"password": `${process.env.TEST_DB_PASS}`,
"database": `${process.env.TEST_DB_NAME}`,
"synchronize": true,
"logging": false,
"dropSchema": true,
"entities": [
"src/entities/**/*.ts"
],
"migrations": [
"src/migrations/**/*.ts"
],
"subscribers": [
"src/subscribers/**/*.ts"
],
"cli": {
"entitiesDir": "src/entities",
"migrationsDir": "src/migrations",
"subscribersDir": "src/subscribers"
}
};

0 comments on commit bf2f29b

Please sign in to comment.