Skip to content

Commit 81fbc17

Browse files
Merge pull request #30 from Real-Dev-Squad/feature/add-test
Remove dummy test and add proper tests
2 parents 817a246 + 2bc4d98 commit 81fbc17

11 files changed

+135
-14
lines changed

.github/workflows/pre-commit-test.yaml renamed to .github/workflows/pre-commit-lint-check.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ jobs:
1010
- run: npm install
1111
- run: npm run lint-check
1212
- run: npm run format-check
13-
- run: npm test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: pre-merge-checks
2+
on:
3+
pull_request:
4+
branches: [main, develop]
5+
jobs:
6+
Run-Tests:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- run: npm install
11+
- run: npm test

src/typeDefinitions/default.types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export interface discordCommand {
66
name: string;
77
description: string;
88
}
9+
10+
export interface responseJson {
11+
type: number;
12+
data: {
13+
content: string;
14+
};
15+
}

src/typeDefinitions/discordMessage.types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface discordMessageRequest {
77

88
export interface messageRequestData {
99
name: string;
10-
options: Array<messageRequestDataOptions>;
10+
options?: Array<messageRequestDataOptions>;
1111
}
1212

1313
export interface messageRequestDataOptions {

tests/fixtures/fixture.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { discordMessageRequest } from "../../src/typeDefinitions/discordMessage.types";
2+
import { InteractionType } from "discord-interactions";
3+
4+
export const dummyHelloMessage: discordMessageRequest = {
5+
type: InteractionType.APPLICATION_COMMAND,
6+
data: {
7+
name: "Hello",
8+
},
9+
member: {
10+
user: {
11+
id: 123456,
12+
username: "ritik",
13+
},
14+
},
15+
guild_id: 123456,
16+
};
17+
18+
export const dummyVerifyMessage: discordMessageRequest = {
19+
type: InteractionType.APPLICATION_COMMAND,
20+
data: {
21+
name: "VERIFY",
22+
},
23+
member: {
24+
user: {
25+
id: 123456,
26+
username: "ritik",
27+
},
28+
},
29+
guild_id: 123456,
30+
};

tests/unit/demo.test.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/unit/discordResponse.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { InteractionResponseType } from "discord-interactions";
2+
import { responseJson } from "../../src/typeDefinitions/default.types";
3+
import { discordTextResponse } from "../../src/utils/discordResponse";
4+
import JSONResponse from "../../src/utils/JsonResponse";
5+
6+
describe("Test discordResponse function", () => {
7+
it("should return a JSONResponse", () => {
8+
const response = discordTextResponse("Hello");
9+
expect(response).toBeInstanceOf(JSONResponse);
10+
});
11+
it("Should have type as channelMessageWithSource", async () => {
12+
const response: responseJson = await discordTextResponse("Hello").json();
13+
expect(response?.type).toBe(
14+
InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE
15+
);
16+
});
17+
it("Should contain a content property in data", async () => {
18+
const response: responseJson = await discordTextResponse("Hello").json();
19+
expect(response?.data).toHaveProperty("content");
20+
});
21+
it("Should have content as hello", async () => {
22+
const response: responseJson = await discordTextResponse("Hello").json();
23+
expect(response?.data.content).toBe("Hello");
24+
});
25+
});

tests/unit/getCommandName.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { HELLO, VERIFY } from "../../src/constants/commands";
2+
import { getCommandName } from "../../src/utils/getCommandName";
3+
4+
describe("Test getCommandName function", () => {
5+
it("Returns hello command name in lower case", () => {
6+
const commandName = getCommandName(HELLO);
7+
expect(commandName).toBe("hello");
8+
});
9+
it("Returns Verify command name in lower case", () => {
10+
const commandName = getCommandName(VERIFY);
11+
expect(commandName).toBe("verify");
12+
});
13+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { commandNotFound } from "../../../src/controllers/commandNotFound";
2+
import { responseJson } from "../../../src/typeDefinitions/default.types";
3+
import JSONResponse from "../../../src/utils/JsonResponse";
4+
5+
describe("Test CommandNotFound Handler", () => {
6+
it("Should return an instance of JSONResponse", () => {
7+
const response = commandNotFound();
8+
expect(response).toBeInstanceOf(JSONResponse);
9+
});
10+
it("Should contain text 'Command Not Found'", async () => {
11+
const response: responseJson = await commandNotFound().json();
12+
expect(response.data.content).toBe("Command Not Found");
13+
});
14+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { InteractionResponseType } from "discord-interactions";
2+
import { helloCommand } from "../../../src/controllers/helloCommand";
3+
import { responseJson } from "../../../src/typeDefinitions/default.types";
4+
import JSONResponse from "../../../src/utils/JsonResponse";
5+
6+
describe("Test helloCommand function", () => {
7+
it("Should be an instance of JSONResponse", () => {
8+
const response = helloCommand(1234);
9+
expect(response).toBeInstanceOf(JSONResponse);
10+
});
11+
it("Should have type as channelMessageWithSource", async () => {
12+
const response: responseJson = await helloCommand(1234).json();
13+
expect(response?.type).toBe(
14+
InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE
15+
);
16+
});
17+
it("Should have content as 'Hello <@userId>'", async () => {
18+
const response: responseJson = await helloCommand(1234).json();
19+
expect(response.data.content).toBe("Hello <@1234>");
20+
});
21+
});

0 commit comments

Comments
 (0)