Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanvasic85 committed Jan 19, 2025
1 parent de71fa4 commit 3223de4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 58 deletions.
51 changes: 27 additions & 24 deletions src/routes/api/notes/[id]/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ describe('GET', () => {
mockIsNoteOwner.mockReturnValue(TE.right(mockNote) as any);

const locals = { user: { id: 'uid_123' } };
const result = await GET({
locals,
params: { id: 'nid_123' }
} as any);

expect(result.status).toBe(404);
const data = await result.json();
expect(data).toStrictEqual({ message: 'User not found' });
expect(
GET({
locals,
params: { id: 'nid_123' }
} as any)
).rejects.toEqual({
status: 404,
body: { message: 'User not found' }
});
});

it('should return a 404 when note is not found', async () => {
Expand All @@ -75,14 +76,15 @@ describe('GET', () => {
mockIsNoteOwner.mockReturnValue(TE.right(mockNote) as any);

const locals = { user: { id: 'uid_123' } };
const result = await GET({
locals,
params: { id: 'nid_123' }
} as any);

expect(result.status).toBe(404);
const data = await result.json();
expect(data).toStrictEqual({ message: 'Note not found' });
expect(
GET({
locals,
params: { id: 'nid_123' }
} as any)
).rejects.toEqual({
status: 404,
body: { message: 'Note not found' }
});
});

it('should return a 403 when note does belong to the user', async () => {
Expand All @@ -92,14 +94,15 @@ describe('GET', () => {
mockGetNoteById.mockReturnValue(TE.right(mockNote) as any);

const locals = { user: { id: 'uid_123' } };
const result = await GET({
locals,
params: { id: 'nid_123' }
} as any);

expect(result.status).toBe(403);
const data = await result.json();
expect(data).toStrictEqual({ message: 'Unauthorized' });
await expect(
GET({
locals,
params: { id: 'nid_123' }
} as any)
).rejects.toEqual({
status: 403,
body: { message: 'Unauthorized' }
});
});
});

Expand Down
69 changes: 35 additions & 34 deletions src/routes/api/notes/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,30 @@ describe('POST', () => {
json: vi.fn().mockResolvedValue({ bad: 'data' })
};

const resp = await POST({
locals: { user: { id: 'uid_123' } },
request
} as any);

expect(resp.status).toBe(400);
const data = await resp.json();
expect(data).toEqual({ message: 'Unable to parse Note', status: 400 });
await expect(
POST({
locals: { user: { id: 'uid_123' } },
request
} as any)
).rejects.toEqual({
status: 400,
body: { message: 'Unable to parse Note' }
});
});

it('should return a 403 when the user is not the owner of the note', async () => {
const request = {
json: vi.fn().mockResolvedValue({ ...mockNoteInput, boardId: 'board_456' })
};

const resp = await POST({
locals: { user: { id: 'uid_123' } },
request
} as any);

expect(resp.status).toBe(403);
const data = await resp.json();
expect(data).toEqual({
message: 'User uid_hello is not the owner of note note_123',
status: 403
await expect(
POST({
locals: { user: { id: 'uid_123' } },
request
} as any)
).rejects.toEqual({
status: 403,
body: { message: 'User uid_hello is not the owner of note note_123' }
});
});

Expand All @@ -105,14 +104,15 @@ describe('POST', () => {
TE.left(createError('DatabaseError', 'Failed to update board'))
);

const resp = await POST({
locals: { user: { id: 'uid_123' } },
request
} as any);

expect(resp.status).toBe(500);
const data = await resp.json();
expect(data).toEqual({ message: 'Failed to update board', status: 500 });
await expect(
POST({
locals: { user: { id: 'uid_123' } },
request
} as any)
).rejects.toEqual({
status: 500,
body: { message: 'Failed to update board' }
});
});

it('should return a 500 when the user db throws an error', async () => {
Expand All @@ -122,13 +122,14 @@ describe('POST', () => {

mockGetUser.mockReturnValue(TE.left(createError('DatabaseError', 'Failed to fetch user')));

const resp = await POST({
locals: { user: { id: 'uid_123' } },
request
} as any);

expect(resp.status).toBe(500);
const data = await resp.json();
expect(data).toEqual({ message: 'Failed to fetch user', status: 500 });
await expect(
POST({
locals: { user: { id: 'uid_123' } },
request
} as any)
).rejects.toEqual({
status: 500,
body: { message: 'Failed to fetch user' }
});
});
});

0 comments on commit 3223de4

Please sign in to comment.