Skip to content

Commit 8e5a2a7

Browse files
Bhuvan Hospetbhuvanh66
authored andcommitted
added tests for expenditures endpoint
1 parent cc2f503 commit 8e5a2a7

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

apps/backend/lambdas/projects/test/projects.e2e.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('POST /projects (e2e)', () => {
2727
});
2828

2929
test('201: creates project with all fields (e2e)', async () => {
30-
const res = await fetch('http://localhost:3000/projects', {
30+
const res = await fetch(`${base}`, {
3131
method: 'POST',
3232
headers: { 'Content-Type': 'application/json' },
3333
body: JSON.stringify({
@@ -73,4 +73,34 @@ describe('POST /projects (e2e)', () => {
7373
});
7474
expect(res.status).toBe(201);
7575
});
76+
});
77+
78+
describe('GET /projects/{id}/expenditures (e2e)', () => {
79+
test("get expenditures for project 1 test 🌞", async () => {
80+
let res = await fetch(`${base}/1/expenditures`);
81+
expect(res.status).toBe(200);
82+
let body = await res.json();
83+
expect(Array.isArray(body)).toBe(true);
84+
});
85+
86+
test("expenditures 404 test 🌞", async () => {
87+
let res = await fetch(`${base}/99999/expenditures`);
88+
expect(res.status).toBe(404);
89+
let body = await res.json();
90+
expect(body.message).toBe('Project not found');
91+
});
92+
93+
test("expenditures ordered by spent_on test 🌞", async () => {
94+
let res = await fetch(`${base}/1/expenditures`);
95+
expect(res.status).toBe(200);
96+
let body = await res.json();
97+
98+
if (body.length > 1) {
99+
for (let i = 0; i < body.length - 1; i++) {
100+
let current = new Date(body[i].spent_on);
101+
let next = new Date(body[i + 1].spent_on);
102+
expect(current >= next).toBe(true);
103+
}
104+
}
105+
});
76106
});

apps/backend/lambdas/projects/test/projects.unit.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,32 @@ test('400: currency empty or too long', async () => {
8686
const tooLong = await handler(event({ name: 'X', currency: 'ABCDEFGHIJK' })); // 11 chars
8787
expect(tooLong.statusCode).toBe(400);
8888
});
89+
90+
91+
function getExpendituresEvent(id: string) {
92+
return {
93+
rawPath: `/projects/${id}/expenditures`,
94+
requestContext: { http: { method: 'GET' } },
95+
} as any;
96+
}
97+
98+
test('200: returns expenditures array', async () => {
99+
const res = await handler(getExpendituresEvent('1'));
100+
expect(res.statusCode).toBe(200);
101+
const json = JSON.parse(res.body);
102+
expect(Array.isArray(json)).toBe(true);
103+
});
104+
105+
test('404: project not found', async () => {
106+
const res = await handler(getExpendituresEvent('99999'));
107+
expect(res.statusCode).toBe(404);
108+
const json = JSON.parse(res.body);
109+
expect(json.message).toBe('Project not found');
110+
});
111+
112+
test('500: invalid id causes error', async () => {
113+
const res = await handler(getExpendituresEvent('invalid'));
114+
expect(res.statusCode).toBe(500);
115+
const json = JSON.parse(res.body);
116+
expect(json.message).toContain('Failed to fetch expenditures');
117+
});

0 commit comments

Comments
 (0)