-
Notifications
You must be signed in to change notification settings - Fork 976
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wip add test cases for todo app
- Loading branch information
1 parent
6889dd2
commit 27fbdf0
Showing
8 changed files
with
12,218 additions
and
1,119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
Oops, something went wrong.