Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

46 ksicius unit testing #50

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions __tests__/isAbleToEdit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// import { ConfigModel } from '../src/models/config';
// import { isAbleToEdit } from '../src/utils/isAbleToEdit';
import isAbleToEdit from "../src/utils/isAbleToEdit";
import {
Role,
Collection,
ChatInputCommandInteraction,
GuildMember,
GuildMemberRoleManager
} from 'discord.js';


describe("isAbleToEdit", () => {
const role = ({
} as unknown) as Role

const rolemanager = ({
cache : Collection.combineEntries([["a", role], ["b", role], ["c", role]], () => (role)),
} as unknown) as GuildMemberRoleManager

const member = ({
roles: rolemanager,
} as unknown) as GuildMember

const interaction = ({
member : member,
} as unknown) as ChatInputCommandInteraction


it("should return true", async () => {
expect(isAbleToEdit(interaction, "a")).toBe(true);
});

it("should return true", async () => {
expect(isAbleToEdit(interaction, "c")).toBe(true);
});

it("should return false", async () => {
expect(isAbleToEdit(interaction, "f")).toBe(false);
});

});
62 changes: 62 additions & 0 deletions __tests__/setupAutorole.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ConfigModel } from '../src/models/config';
import { giveAutorole, setAutoroleEnabled } from '../src/modules/setupAutorole';
import {
ChatInputCommandInteraction,
CommandInteractionOption,
CommandInteractionOptionResolver,
GuildMember,
GuildMemberRoleManager
} from 'discord.js';


describe("giveAutorole", () => {
const rolemanager = ({
add : jest.fn(),
} as unknown) as GuildMemberRoleManager
const member = ({
roles: rolemanager,
} as unknown) as GuildMember

const config = {
IS_AUTOROLE_ENABLED : false
}

it("shouldn't set role", async () => {
jest.spyOn(ConfigModel, 'findOne').mockResolvedValue(config);
await giveAutorole(member);
expect(rolemanager.add).not.toHaveBeenCalled();
});
});

describe("setAutoroleEnabled", () => {
const option = ({
value: true
} as unknown) as CommandInteractionOption

const optionresolver = ({
data: [option]
} as unknown) as CommandInteractionOptionResolver

const interaction = ({
options : optionresolver,
reply : jest.fn()
} as unknown) as ChatInputCommandInteraction

const config = {
IS_AUTOROLE_ENABLED : false,
save : jest.fn()
}

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');


it("should change IS_AUTOROLE_ENABLED", async () => {
jest.spyOn(ConfigModel, 'findOne').mockResolvedValue(config);
await setAutoroleEnabled(interaction);
expect(interaction.reply).toHaveBeenCalled();
expect(config.IS_AUTOROLE_ENABLED).toBe(true);
expect(config.save).toHaveBeenCalled();
});
});

20 changes: 20 additions & 0 deletions __tests__/welcomeMessage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ConfigModel } from '../src/models/config';
import { sendWelcomeMessage } from '../src/modules/welcomeMessage';
import { GuildMember } from 'discord.js';


describe("sendWelcomeMessage", () => {
const member = ({
send: jest.fn(),
} as unknown) as GuildMember

const config = {
WELCOME_MESSAGE: 'wiadomosc'
}

it("should send message", async () => {
jest.spyOn(ConfigModel, 'findOne').mockResolvedValue(config);
await sendWelcomeMessage(member);
expect(member.send).toHaveBeenCalledWith('wiadomosc');
});
});
19 changes: 19 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
"roots": [
"<rootDir>"
],

"modulePaths": [
"<rootDir>",
],
"moduleDirectories": [
"node_modules"
],
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
}
Loading