diff --git a/package.json b/package.json index d71299c..7988184 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/__test__/Decorators/decorators.ts b/src/__test__/Decorators/decorators.ts new file mode 100644 index 0000000..fde0585 --- /dev/null +++ b/src/__test__/Decorators/decorators.ts @@ -0,0 +1,3 @@ +export function myDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) { + console.log("myDecorator called on: ", target, propertyKey, descriptor); +} diff --git a/src/__test__/route.test.ts b/src/__test__/route.test.ts index 401f5b8..7a4ceda 100644 --- a/src/__test__/route.test.ts +++ b/src/__test__/route.test.ts @@ -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'; +import { mock, MockProxy } from 'jest-mock-extended'; + + 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: 'john.doe@example.com', + 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); + } }); }); diff --git a/src/controllers/authController.ts b/src/controllers/authController.ts index 47e39ba..209a350 100644 --- a/src/controllers/authController.ts +++ b/src/controllers/authController.ts @@ -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' }); diff --git a/testOrmconfig.js b/testOrmconfig.js new file mode 100644 index 0000000..04681d0 --- /dev/null +++ b/testOrmconfig.js @@ -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" + } +};