Skip to content

Commit

Permalink
feat: wip add test cases for todo app
Browse files Browse the repository at this point in the history
  • Loading branch information
shrey-dadhaniya committed Jan 25, 2024
1 parent 6889dd2 commit 27fbdf0
Show file tree
Hide file tree
Showing 8 changed files with 12,218 additions and 1,119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ logs

/tmp
/.vscode
/.idea

# ignore the image files uploaded in the public/images folder
/public/images/*
Expand Down
16 changes: 16 additions & 0 deletions e2e/healthcheck.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test,expect } from '@playwright/test';
let apiContext;

test.beforeAll(async ({ playwright }) => {
apiContext = await playwright.request.newContext({
baseURL: 'http://localhost:8080',
});
});
test.afterAll(async ({ }) => {
// Dispose all responses.
await apiContext.dispose();
});
test('Healthcheck', async ({ page }) => {
const res = await apiContext.get(`/api/v1/healthcheck`);
expect(res.ok()).toBeTruthy();
});
65 changes: 65 additions & 0 deletions e2e/test-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import dotenv from "dotenv";
import { httpServer } from "../src/app.js";
import mongoose from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";

dotenv.config({
path: "./.env",
});

let mongoServer = null;
let dbInstance = undefined;

const connectDB = async () => {
try {
await mongoose.disconnect();
mongoServer = await MongoMemoryServer.create();
dbInstance = await mongoose.connect(`${mongoServer.getUri()}`);
await clear();
} catch (error) {
console.log("MongoDB connection error: ", error);
process.exit(1);
}
};
const clear = async () => {
const collections = mongoose.connection.collections;

for (const key in collections) {
const collection = collections[key];
await collection.deleteMany({});
}
};

/**
* Starting from Node.js v14 top-level await is available and it is only available in ES modules.
* This means you can not use it with common js modules or Node version < 14.
*/
const majorNodeVersion = +process.env.NODE_VERSION?.split(".")[0] || 0;

const startServer = () => {
httpServer.listen(process.env.PORT || 8080, () => {
console.info(
`📑 Visit the documentation at: http://localhost:${
process.env.PORT || 8080
}`
);
console.log("⚙️ Server is running on port: " + process.env.PORT);
});
};

if (majorNodeVersion >= 14) {
try {
await connectDB();
startServer();
} catch (err) {
console.log("Mongo db connect error: ", err);
}
} else {
connectDB()
.then(() => {
startServer();
})
.catch((err) => {
console.log("Mongo db connect error: ", err);
});
}
87 changes: 87 additions & 0 deletions e2e/todos.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { test, expect, request } from "@playwright/test";
let apiContext;

const createTodoBody = {
title: "test-todo-title",
description: "test-todo-description",
};
const updateTodoBody = {
title: "update-todo-title",
description: "update-todo-description",
};
let todoId = null;

test.beforeAll(async ({ playwright }) => {
apiContext = await playwright.request.newContext({
baseURL: `http://localhost:${process.env.PORT || 8080}`,
});
});

test.afterAll(async ({}) => {
await apiContext.dispose();
});

test("Get Todos", async ({ page }) => {
const res = await apiContext.get(`/api/v1/todos`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.data.length).toEqual(0);
});

test("Create todo", async ({ page }) => {
const res = await apiContext.post(`/api/v1/todos`, {
data: createTodoBody,
});
const json = await res.json();
expect(res.status()).toEqual(201);
expect(json.statusCode).toEqual(201);
todoId = json.data._id;
});

test("Check created Todo", async ({ page }) => {
const res = await apiContext.get(`/api/v1/todos/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
expect(json.data).toMatchObject(createTodoBody);
});

test("Update Todo", async ({ page }) => {
const res = await apiContext.patch(`/api/v1/todos/${todoId}`, {
data: updateTodoBody,
});
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
});

test("Check updated Todo", async ({ page }) => {
const res = await apiContext.get(`/api/v1/todos/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
expect(json.data).toMatchObject(updateTodoBody);
});

test("Toggle Todo Status", async ({ page }) => {
const res = await apiContext.patch(`/api/v1/todos/toggle/status/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
expect(json.data.isComplete).toBeTruthy();
});

test("Delete Todo", async ({ page }) => {
const res = await apiContext.delete(`/api/v1/todos/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
});

test("Check Deleted Todo", async ({ page }) => {
const res = await apiContext.delete(`/api/v1/todos/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(404);
expect(json.statusCode).toEqual(res.status());
expect(json.success).toBeFalsy();
});

0 comments on commit 27fbdf0

Please sign in to comment.