Skip to content

Commit

Permalink
Create user.test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 10, 2024
1 parent 4afd901 commit d186341
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// test/user.test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const app = require('../app');
const User = require('../models/user');

const expect = chai.expect;
chai.use(chaiHttp);

describe('User API', () => {
describe('POST /register', () => {
it('should create a new user', async () => {
const res = await chai.request(app).post('/register').send({
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
password: 'password123'
});

expect(res).to.have.status(201);
expect(res.body).to.be.an('object');
expect(res.body.user).to.have.property('_id');
expect(res.body.user).to.have.property('firstName', 'John');
expect(res.body.user).to.have.property('lastName', 'Doe');
expect(res.body.user).to.have.property('email', '[email protected]');
expect(res.body.user).to.have.property('password', 'password123');
});
});

describe('POST /login', () => {
it('should login a user', async () => {
const user = new User({
firstName: 'Jane',
lastName: 'Doe',
email: '[email protected]',
password: 'password123'
});

await user.save();

const res = await chai.request(app).post('/login').send({
email: '[email protected]',
password: 'password123'
});

expect(res).to.have.status(200);
expect(res.body).to.be.an('object');
expect(res.body.user).to.have.property('_id');
expect(res.body.user).to.have.property('firstName', 'Jane');
expect(res.body.user).to.have.property('lastName', 'Doe');
expect(res.body.user).to.have.property('email', '[email protected]');
expect(res.body.user).to.have.property('password', 'password123');
});
});
});

0 comments on commit d186341

Please sign in to comment.