Skip to content

Commit

Permalink
test: Add tests with subcommand for build app command
Browse files Browse the repository at this point in the history
  • Loading branch information
RybakovFE authored and RybakovFE committed Feb 26, 2023
1 parent efb0d31 commit f6203b1
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions packages/core/src/services/build-application-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ChatInputCommandOptions } from '../decorators/command/chat-input-comman
import { Command } from '../decorators/command/command.decorator';
import { Handler } from '../decorators/command/handler/handler.decorator';
import { Param } from '../decorators/option/param/param.decorator';
import { UseGroup } from '../decorators/sub-command-group/use-group';
import { SubCommand } from '../decorators/sub-command/sub-command.decorator';
import { OptionExplorer } from '../explorers/option/option.explorer';
import { DiscordCommandProvider } from '../providers/discord-command.provider';
import { ReflectMetadataProvider } from '../providers/reflect-metadata.provider';
Expand Down Expand Up @@ -157,4 +159,157 @@ describe('Build application command service', () => {
},
});
});

describe('Subcommand', () => {
class EmailDto {
@Param({ description: 'Your email' })
email: string;
}

@SubCommand({
name: 'email',
description: 'Register by subcommand',
})
class EmailSubCommand {
@Handler()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onEmailCommand(dto: EmailDto) {
return 'ok';
}
}

@SubCommand({
name: 'base-info',
description: 'Return base info about user',
})
class BaseInfoSubCommand {
@Handler()
onBaseInfoCommand() {
return 'ok';
}
}

beforeEach(async () => {
testingModule = await Test.createTestingModule({
providers: [
BuildApplicationCommandService,
ReflectMetadataProvider,
DiscordCommandProvider,
DtoService,
OptionExplorer,
CommandHandlerFinderService,
MetadataScanner,
EmailSubCommand,
BaseInfoSubCommand,
],
}).compile();
});

it('should explore slash sub command and add it to DiscordCommandProvider', async () => {
const buildApplicationCommand = testingModule.get(
BuildApplicationCommandService,
);

const commandOptions = {
name: 'reg',
description: 'User registration',
include: [
UseGroup(
{ name: 'type', description: 'Registration type' },
EmailSubCommand,
),
BaseInfoSubCommand,
],
};

@Command(commandOptions)
class RegistrationCommand {}

const commandInstance = new RegistrationCommand();
const discordCommandProvider = testingModule.get(DiscordCommandProvider);
jest.spyOn(discordCommandProvider, 'addCommand');

const actual = await buildApplicationCommand.exploreCommand(
commandInstance,
commandOptions,
);

expect(actual).toStrictEqual(
expect.arrayContaining([
{
group: 'type',
instance: testingModule.get(EmailSubCommand),
methodName: 'onEmailCommand',
name: 'reg',
subName: 'email',
},
{
group: undefined,
instance: testingModule.get(BaseInfoSubCommand),
methodName: 'onBaseInfoCommand',
name: 'reg',
subName: 'base-info',
},
]),
);

expect(discordCommandProvider.addCommand).toBeCalledWith(
RegistrationCommand,
{
additionalOptions: { forGuild: undefined },
commandData: {
defaultMemberPermissions: undefined,
description: 'User registration',
descriptionLocalizations: undefined,
dmPermission: undefined,
name: 'reg',
nameLocalizations: undefined,
options: [
{
description: 'Registration type',
descriptionLocalizations: undefined,
name: 'type',
nameLocalizations: undefined,
options: [
{
description: 'Register by subcommand',
descriptionLocalizations: undefined,
name: 'email',
nameLocalizations: undefined,
options: [
{
autocomplete: undefined,
channelTypes: undefined,
choices: undefined,
description: 'Your email',
descriptionLocalizations: undefined,
maxLength: undefined,
maxValue: undefined,
minLength: undefined,
minValue: undefined,
name: 'email',
nameLocalizations: undefined,
required: false,
type: 3,
},
],
type: 1,
},
],
type: 2,
},
{
description: 'Return base info about user',
descriptionLocalizations: undefined,
name: 'base-info',
nameLocalizations: undefined,
type: 1,
},
],
type: 1,
},
},
);
});
});
});

0 comments on commit f6203b1

Please sign in to comment.