From b5501e94ba98b74bc1e333844e510e2fbe16815d Mon Sep 17 00:00:00 2001 From: celesca Date: Wed, 20 Nov 2024 19:50:50 +0700 Subject: [PATCH] add should return user --- src/controllers/userController.ts | 8 ++++++-- test/userController.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index 4848caa..33bc4a9 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -6,7 +6,7 @@ export const userController = new Elysia({ prefix: "/user" }) // Create a new user (sign-up) .post( "/create", - async ({ body, error }) => { + async ({ body, error, set }) => { const { userName, email, password, workingStyle, profileImage, bio } = body; const existingUser = await prisma.user.findUnique({ @@ -35,7 +35,11 @@ export const userController = new Elysia({ prefix: "/user" }) }, }); - return newUser; // Return the newly created user + // return newUser and status 201 + set.status = 201; + return newUser; + + }, { body: t.Object({ diff --git a/test/userController.test.ts b/test/userController.test.ts index e69de29..77eef4c 100644 --- a/test/userController.test.ts +++ b/test/userController.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'bun:test' +import { Elysia } from 'elysia' + +describe('Create new user', () => { + it('should return a new user', async () => { + const app = new Elysia().post('/user/create', ({ body }) => { + return body + }) + + const response = await app + .handle(new Request('http://localhost:3000/user/create', { + method: 'POST', + body: JSON.stringify({ + name: 'John Doe', + email: 'john.doe@gmail.com', + password: '1234', + })})) + + //Expect response status to be 201 Created + expect(response.status).toBe(200) +}) +}) \ No newline at end of file