From f2d4b5bda10f49ac9d77810d4c8bcab486baae4b Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Tue, 7 Jan 2025 17:33:33 -0600 Subject: [PATCH] cleanup-tests --- test/core/functions.test.ts | 43 -------------- test/core/id.test.ts | 43 +------------- test/handlers.test.ts | 108 ++++++++---------------------------- test/setup/setup-tests.ts | 46 +++++++++++++++ test/setup/util.ts | 30 ++++++++++ vitest.config.ts | 8 +++ 6 files changed, 109 insertions(+), 169 deletions(-) create mode 100644 test/setup/setup-tests.ts create mode 100644 test/setup/util.ts create mode 100644 vitest.config.ts diff --git a/test/core/functions.test.ts b/test/core/functions.test.ts index 8a54ec7d..7a7d22d4 100644 --- a/test/core/functions.test.ts +++ b/test/core/functions.test.ts @@ -6,49 +6,6 @@ import { partitionPlugins, treeSearch } from '../../src/core/functions'; import { faker } from '@faker-js/faker'; import { ApplicationCommandOptionType, AutocompleteInteraction } from 'discord.js'; -vi.mock('discord.js', async (importOriginal) => { - const mod = await importOriginal() - const ModalSubmitInteraction = class { - customId; - type = 5; - isModalSubmit = vi.fn(); - constructor(customId) { - this.customId = customId; - } - }; - const ButtonInteraction = class { - customId; - type = 3; - componentType = 2; - isButton = vi.fn(); - constructor(customId) { - this.customId = customId; - } - }; - const AutocompleteInteraction = class { - type = 4; - option: string; - constructor(s: string) { - this.option = s; - } - options = { - getFocused: vi.fn(), - getSubcommand: vi.fn(), - }; - }; - - return { - Collection: mod.Collection, - ComponentType: mod.ComponentType, - InteractionType: mod.InteractionType, - ApplicationCommandOptionType: mod.ApplicationCommandOptionType, - ApplicationCommandType: mod.ApplicationCommandType, - ModalSubmitInteraction, - ButtonInteraction, - AutocompleteInteraction, - }; -}); - describe('functions', () => { afterEach(() => { vi.clearAllMocks(); diff --git a/test/core/id.test.ts b/test/core/id.test.ts index 25925c43..e50ba585 100644 --- a/test/core/id.test.ts +++ b/test/core/id.test.ts @@ -4,48 +4,7 @@ import { CommandType } from '../../src/core/structures/enums'; import * as Id from '../../src/core/id' import { ButtonInteraction, ModalSubmitInteraction } from 'discord.js'; -vi.mock('discord.js', async (importOriginal) => { - const mod = await importOriginal() - const ModalSubmitInteraction = class { - customId; - type = 5; - isModalSubmit = vi.fn(); - constructor(customId) { - this.customId = customId; - } - }; - const ButtonInteraction = class { - customId; - type = 3; - componentType = 2; - isButton = vi.fn(); - constructor(customId) { - this.customId = customId; - } - }; - const AutocompleteInteraction = class { - type = 4; - option: string; - constructor(s: string) { - this.option = s; - } - options = { - getFocused: vi.fn(), - getSubcommand: vi.fn(), - }; - }; - - return { - Collection: mod.Collection, - ComponentType: mod.ComponentType, - InteractionType: mod.InteractionType, - ApplicationCommandOptionType: mod.ApplicationCommandOptionType, - ApplicationCommandType: mod.ApplicationCommandType, - ModalSubmitInteraction, - ButtonInteraction, - AutocompleteInteraction, - }; -}); + test('id -> Text', () => { expect(Id.create("ping", CommandType.Text)).toBe("ping_T") }) diff --git a/test/handlers.test.ts b/test/handlers.test.ts index 80c64114..33f1eb92 100644 --- a/test/handlers.test.ts +++ b/test/handlers.test.ts @@ -1,83 +1,16 @@ //@ts-nocheck -import { beforeEach, describe, expect, vi, it, test } from 'vitest'; +import { beforeEach, describe, expect, it, test } from 'vitest'; import { callInitPlugins, eventDispatcher } from '../src/handlers/event-utils'; -import { Client, ChatInputCommandInteraction } from 'discord.js' +import { Client } from 'discord.js' import { faker } from '@faker-js/faker'; import { Module } from '../src/types/core-modules'; import { Processed } from '../src/types/core-modules'; import { EventEmitter } from 'events'; -import { EventType } from '../src/core/structures/enums'; import { CommandControlPlugin, CommandInitPlugin, CommandType, controller } from '../src'; +import { createRandomModule, createRandomInitPlugin } from './setup/util'; -vi.mock('discord.js', async (importOriginal) => { - const mod = await importOriginal() - const ModalSubmitInteraction = class { - customId; - type = 5; - isModalSubmit = vi.fn(); - constructor(customId) { - this.customId = customId; - } - }; - const ButtonInteraction = class { - customId; - type = 3; - componentType = 2; - isButton = vi.fn(); - constructor(customId) { - this.customId = customId; - } - }; - const AutocompleteInteraction = class { - type = 4; - option: string; - constructor(s: string) { - this.option = s; - } - options = { - getFocused: vi.fn(), - getSubcommand: vi.fn(), - }; - }; - - return { - Client : vi.fn(), - Collection: mod.Collection, - ComponentType: mod.ComponentType, - InteractionType: mod.InteractionType, - ApplicationCommandOptionType: mod.ApplicationCommandOptionType, - ApplicationCommandType: mod.ApplicationCommandType, - ModalSubmitInteraction, - ButtonInteraction, - AutocompleteInteraction, - ChatInputCommandInteraction: vi.fn() - }; -}); -function createRandomPlugin (s: 'go', mut?: Partial) { - return CommandInitPlugin(({ module }) => { - if(mut) { - Object.entries(mut).forEach(([k, v]) => { - module[k] = v - }) - } - return s == 'go' - ? controller.next() - : controller.stop() - }) -} -function createRandomModule(plugins: any[]): Processed { - return { - type: EventType.Discord, - meta: { id:"", absPath: "" }, - description: faker.string.alpha(), - plugins, - name: "cheese", - onEvent: [], - execute: vi.fn(), - }; -} function mockDeps() { return { @@ -103,24 +36,31 @@ describe('eventDispatcher standard', () => { }); }); -test ('call init plugins', async () => { - const deps = mockDeps() - const plugins = createRandomPlugin('go', { name: "abc" }) - const mod = createRandomModule([plugins]) - const s = await callInitPlugins(mod, deps, false) - expect("abc").equal(s.name) -}) +describe('calling init plugins', async () => { + let deps; + beforeEach(() => { + deps = mockDeps() + }); + + test ('call init plugins', async () => { + const plugins = createRandomInitPlugin('go', { name: "abc" }) + const mod = createRandomModule([plugins]) + const s = await callInitPlugins(mod, deps, false) + expect("abc").equal(s.name) + }) + test('init plugins replace array', async () => { + const plugins = createRandomInitPlugin('go', { opts: [] }) + const plugins2 = createRandomInitPlugin('go', { opts: ['a'] }) + const mod = createRandomModule([plugins, plugins2]) + const s = await callInitPlugins(mod, deps, false) + expect(['a']).deep.equal(s.opts) + }) -test('init plugins replace array', async () => { - const deps = mockDeps() - const plugins = createRandomPlugin('go', { opts: [] }) - const plugins2 = createRandomPlugin('go', { opts: ['a'] }) - const mod = createRandomModule([plugins, plugins2]) - const s = await callInitPlugins(mod, deps, false) - expect(['a']).deep.equal(s.opts) }) + + test('form sdt', async () => { const expectedObject = { diff --git a/test/setup/setup-tests.ts b/test/setup/setup-tests.ts new file mode 100644 index 00000000..7cb8684d --- /dev/null +++ b/test/setup/setup-tests.ts @@ -0,0 +1,46 @@ +import { vi } from 'vitest' + +vi.mock('discord.js', async (importOriginal) => { + const mod = await importOriginal() + const ModalSubmitInteraction = class { + customId; + type = 5; + isModalSubmit = vi.fn(); + constructor(customId) { + this.customId = customId; + } + }; + const ButtonInteraction = class { + customId; + type = 3; + componentType = 2; + isButton = vi.fn(); + constructor(customId) { + this.customId = customId; + } + }; + const AutocompleteInteraction = class { + type = 4; + option: string; + constructor(s: string) { + this.option = s; + } + options = { + getFocused: vi.fn(), + getSubcommand: vi.fn(), + }; + }; + + return { + Client : vi.fn(), + Collection: mod.Collection, + ComponentType: mod.ComponentType, + InteractionType: mod.InteractionType, + ApplicationCommandOptionType: mod.ApplicationCommandOptionType, + ApplicationCommandType: mod.ApplicationCommandType, + ModalSubmitInteraction, + ButtonInteraction, + AutocompleteInteraction, + ChatInputCommandInteraction: vi.fn() + }; +}); diff --git a/test/setup/util.ts b/test/setup/util.ts new file mode 100644 index 00000000..6dea20e3 --- /dev/null +++ b/test/setup/util.ts @@ -0,0 +1,30 @@ +import { faker } from "@faker-js/faker" +import { CommandInitPlugin, CommandType, Module, controller } from "../../src" +import { Processed } from "../../src/types/core-modules" +import { vi } from 'vitest' + +export function createRandomInitPlugin (s: 'go', mut?: Partial) { + return CommandInitPlugin(({ module }) => { + if(mut) { + Object.entries(mut).forEach(([k, v]) => { + module[k] = v + }) + } + return s == 'go' + ? controller.next() + : controller.stop() + }) +} + +export function createRandomModule(plugins: any[]): Processed { + return { + type: CommandType.Both, + meta: { id:"", absPath: "" }, + description: faker.string.alpha(), + plugins, + name: "cheese", + onEvent: [], + locals: {}, + execute: vi.fn(), + }; +} diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 00000000..17d64f59 --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,8 @@ +// vitest.config.ts or vitest.config.js +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + setupFiles: ['./test/setup/setup-tests.ts'], + }, +})