Skip to content

Commit

Permalink
fix: fix unit test (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
berviantoleo authored Sep 22, 2024
1 parent deddc81 commit c581909
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: corepack enable && yarn --immutable
- run: yarn build
- run: yarn test
- name: Test
run: yarn test
env:
MONGO_CONNECTION_STRING: 'mongodb://root:example@localhost:27017/invitation?authSource=admin'
6 changes: 3 additions & 3 deletions src/models/invitation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import mongoose, { Schema, PopulatedDoc, Types } from "mongoose";
import mongoose, { Schema, PopulatedDoc, Types, Document } from "mongoose";
import { IName } from "./name";

interface IInvitation {
inviter: Types.ObjectId;
invitee: Types.ObjectId;
inviter: PopulatedDoc<Document<Types.ObjectId> & IName>;
invitee: PopulatedDoc<Document<Types.ObjectId> & IName>;
message: String;
createdAt: Date;
}
Expand Down
32 changes: 19 additions & 13 deletions test/invitation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,59 @@ import request from "supertest";
import mongoose from "mongoose";
import app from "../src/app";
import Invitation from "../src/models/invitation";
import Name from "../src/models/name";

describe("Test the invitation path", () => {
let createdName: any;
beforeAll(async () => {
await mongoose.connect(process.env.MONGO_CONNECTION_STRING || '');
createdName = await Name.create({
username: 'invitationleo',
name: 'Bervianto',
createdAt: new Date()
});
});

beforeEach(async () => {
await Invitation.deleteMany();
})

afterAll(async () => {
await Name.deleteMany();
await mongoose.disconnect();
});

test("It should response the GET method", async () => {
let dateNow = Date.now();
let objectItem = {
invitee: 'every where',
inviter: 'berviantoleo',
invitee: createdName._id,
inviter: createdName._id,
message: 'Hello!',
createdAt: dateNow
};
await Invitation.create(objectItem);
const response = await request(app).get("/invitation/berviantoleo");
const response = await request(app).get("/invitation/invitationleo");
expect(response.statusCode).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body.length).toBe(1)
let responseItem = response.body[0];
expect(responseItem.invitee).toBe(objectItem.invitee);
expect(responseItem.inviter).toBe(objectItem.inviter);
expect(responseItem.invitee.username).toBe(createdName.username);
expect(responseItem.inviter.username).toBe(createdName.username);
expect(responseItem.message).toBe(objectItem.message);
expect(responseItem.createdAt).toBe(new Date(objectItem.createdAt).toISOString());
});

test("It should response the POST method", async () => {
let objectItem = {
invitee: 'every where',
inviter: 'berviantoleo',
invitee: 'invitationleo',
inviter: 'invitationleo',
message: 'Hello!'
};
const response = await request(app).post("/invitation").send(objectItem);
expect(response.statusCode).toBe(200);
expect(response.body).toBeTruthy();
let responseItem = response.body;
expect(responseItem.invitee).toBe(objectItem.invitee);
expect(responseItem.inviter).toBe(objectItem.inviter);
expect(responseItem.invitee).toBe(createdName._id.toString());
expect(responseItem.inviter).toBe(createdName._id.toString());
expect(responseItem.message).toBe(objectItem.message);
expect(responseItem.createdAt).toBeTruthy();
expect(responseItem._id).toBeTruthy();
Expand All @@ -56,10 +63,9 @@ describe("Test the invitation path", () => {
const dataDb = await Invitation.findById(responseItem._id);

expect(dataDb).toBeTruthy();
expect(dataDb?.invitee).toBe(objectItem.invitee);
expect(dataDb?.inviter).toBe(objectItem.inviter);
expect(dataDb?.invitee?.toString()).toBe(createdName._id.toString());
expect(dataDb?.inviter?.toString()).toBe(createdName._id.toString());
expect(dataDb?.message).toBe(objectItem.message);
expect(dataDb?.createdAt?.toISOString()).toBe(responseItem.createdAt);
expect(dataDb?._id?.toString()).toBe(responseItem._id);
expect(dataDb?.__v).toBe(0);
});
Expand Down
29 changes: 29 additions & 0 deletions test/name.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import request from "supertest";
import mongoose from "mongoose";
import app from "../src/app";
import Name from "../src/models/name";

describe("Test the name path", () => {
beforeAll(async () => {
await mongoose.connect(process.env.MONGO_CONNECTION_STRING || '');
});

afterAll(async () => {
await mongoose.disconnect();
});

test("It should response the GET method", async () => {
let dateNow = Date.now();
const objectItem = {
username: 'berviantoleo',
name: 'Bervianto',
createdAt: dateNow
};
await Name.create(objectItem);
const response = await request(app).get("/name/berviantoleo");
expect(response.statusCode).toBe(200);
expect(response.body.username).toBe(objectItem.username);
expect(response.body.name).toBe(objectItem.name);
expect(response.body.createdAt).toBe(new Date(objectItem.createdAt).toISOString());
});
});

0 comments on commit c581909

Please sign in to comment.