forked from gitea_admin/simple-node
backend files
This commit is contained in:
35
backend/app.test.js
Normal file
35
backend/app.test.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const request = require('supertest');
|
||||
const app = require('./index');
|
||||
|
||||
describe('Backend API', () => {
|
||||
it('should return health check message on /', async () => {
|
||||
const res = await request(app).get('/');
|
||||
expect(res.statusCode).toEqual(200);
|
||||
expect(res.text).toContain('Todo API is running');
|
||||
});
|
||||
|
||||
it('should fetch all todos', async () => {
|
||||
const res = await request(app).get('/api/todos');
|
||||
expect(res.statusCode).toEqual(200);
|
||||
expect(Array.isArray(res.body)).toBeTruthy();
|
||||
expect(res.body.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it('should add a new todo', async () => {
|
||||
const newTodo = { task: 'Test task' };
|
||||
const res = await request(app)
|
||||
.post('/api/todos')
|
||||
.send(newTodo);
|
||||
expect(res.statusCode).toEqual(201);
|
||||
expect(res.body.task).toEqual('Test task');
|
||||
expect(res.body).toHaveProperty('id');
|
||||
});
|
||||
|
||||
it('should fail to add a todo without a task', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/todos')
|
||||
.send({});
|
||||
expect(res.statusCode).toEqual(400);
|
||||
expect(res.body.error).toEqual('Task is required');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user