Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
test(message.test.ts): add tests for isDiscordLink function
Browse files Browse the repository at this point in the history
feat(message.util.ts): add isDiscordLink function to check if a link is a valid discord link
  • Loading branch information
Steellgold committed Jun 7, 2023
1 parent e5e8fd1 commit 0e8c272
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/utils/message/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { msgParams } from "./message.util";
import { isDiscordLink, msgParams } from "./message.util";

describe("msgParams function", () => {
it("should correctly replace placeholders with provided words", () => {
Expand Down Expand Up @@ -33,4 +33,30 @@ describe("msgParams function", () => {
it("should ignore extra words when there are no placeholders", () => {
expect(msgParams("Hello", ["Hello", "World"])).toBe("Hello");
});
});

describe("isDiscordLink function", () => {
it("should return true when the link is a discord canary link", () => {
expect(isDiscordLink("https://canary.discord.com/channels/732251741999071303/1113731433123565589/1113731587570413588")).toBe(true);
});

it("should return true when the link is a discord ptb link", () => {
expect(isDiscordLink("https://ptb.discord.com/channels/732251741999071303/786216771723198514/1075749993631191110")).toBe(true);
});

it("should return true when the link is a discord link", () => {
expect(isDiscordLink("https://discord.com/channels/732251741999071303/786216771723198514/803532192793493544")).toBe(true);
});

it("should return false because is not valid", () => {
expect(isDiscordLink("https://discord.com/channels/732251741999071303")).toBe(false);
});

it("should return false because is litteraly not a discord link", () => {
expect(isDiscordLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ")).toBe(false);
});

it("should return false when the link is not a discord link", () => {
expect(isDiscordLink("https://fakediscord.com/channels/732251741999071303/1113731433123565589/1113731587570413588")).toBe(false);
});
});
4 changes: 4 additions & 0 deletions src/utils/message/message.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export const msgParams = (message: string, params: (string | number)[]): string
}

return message;
};

export const isDiscordLink = (link: string): boolean => {
return link.match(/^http(s?):\/\/(www\.|canary\.|ptb\.)?discord.com\/channels(\/\d*){3}$/gi) !== null;
};

0 comments on commit 0e8c272

Please sign in to comment.