Skip to content

Commit

Permalink
feat(user-Test): implementing sign up using jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuyisenge2 committed Apr 22, 2024
1 parent 5cec93e commit f617513
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
1 change: 0 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ jobs:
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Run tests
run: npm run test

Expand Down
34 changes: 13 additions & 21 deletions src/__test__/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import {
login_user_invalid_email,
login_user_wrong_credentials,
NewUser,
user_bad_request,
User_without_email,
exist_user,
user_bad_request
} from "../mock/static";


import { createUser } from "../services/user.services";
jest.setTimeout(30000);

function logErrors(
Expand All @@ -32,13 +29,19 @@ const Jest_request = request(app.use(logErrors));
describe("USER API TEST", () => {
beforeAll(async () => {
await connectionToDatabase();
await createUser(exist_user);
});

afterAll(async () => {
await deleteTableData(User, "users");
});

it("it should register a user and return 201", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(NewUser)
.expect(201);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Account Created successfully!");
expect(body.token).toBeDefined();
});
it("it should return a user not found and status 400", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(user_bad_request)
Expand All @@ -47,20 +50,12 @@ describe("USER API TEST", () => {

it("it should return a user exist and status 409 when Email is already used in database", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(exist_user)
.send(NewUser)
.expect(409);
expect(body.status).toStrictEqual("CONFLICT");
expect(body.message).toStrictEqual("User already exist!");
});
});
it("it should register a user and return 201", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(NewUser)
.expect(201);
expect(body.status).toStrictEqual("SUCCESS");
expect(body.message).toStrictEqual("Account Created successfully!");
expect(body.token).toBeDefined();
});


/**
* ---------------------------- LOGIN --------------------------------------------
Expand Down Expand Up @@ -100,9 +95,6 @@ describe("USER API TEST", () => {
expect(body.status).toStrictEqual("BAD REQUEST");
expect(body.message).toBeDefined();
})
/**
* ----------------------------register new user --------------------------------------------
*/
describe("FAILED SIGN UP", () => {



});
4 changes: 2 additions & 2 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const registerUser = async (
} catch (error) {
res
.status(500)
.json(new HttpException("SERVER FAIL", "Something went wrong!"));
.json(new HttpException("SERVER FAILS", "Something went wrong!"));
}
};

Expand Down Expand Up @@ -76,6 +76,6 @@ const login = async (req: Request, res: Response, next: NextFunction) => {
};

export default {
login,
registerUser,
login
};
2 changes: 1 addition & 1 deletion src/mock/static.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

export const login_user = {
email:"travis@gmail.com",
email:"peter234565@gmail.com",
password:"passwordQWE123",
};

Expand Down
2 changes: 2 additions & 0 deletions src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ userRoutes.post(
userMiddleware.userValid,
userController.registerUser
);

userRoutes.post("/login", userMiddleware.logInValidated, userController.login);


export default userRoutes;

0 comments on commit f617513

Please sign in to comment.