import { SlashCommand } from '@slack/bolt'; import { InvalidGrowiCommandError } from '~/models/errors'; import { parse } from './slash-command-parser'; const SlashCommandMock = jest.fn().mockImplementation((text) => { return { text } as SlashCommand; }); describe('parse SlashCommand', () => { describe('without growiCommandType', () => { test('throws InvalidGrowiCommandError', () => { // setup const slashCommandText = ''; const slashCommand = new SlashCommandMock(slashCommandText); // when/then expect(() => { parse(slashCommand); }).toThrowError(InvalidGrowiCommandError); }); }); test('returns a GrowiCommand instance with empty growiCommandArgs', () => { // setup const slashCommandText = 'search'; const slashCommand = new SlashCommandMock(slashCommandText); // when const result = parse(slashCommand); // then expect(result.text).toBe(slashCommandText); expect(result.growiCommandType).toBe('search'); expect(result.growiCommandArgs).toStrictEqual([]); }); test('returns a GrowiCommand instance with space growiCommandType', () => { // setup const slashCommandText = 'search '; const slashCommand = new SlashCommandMock(slashCommandText); // when const result = parse(slashCommand); // then expect(result.text).toBe(slashCommandText); expect(result.growiCommandType).toBe('search'); expect(result.growiCommandArgs).toStrictEqual([]); }); test('returns a GrowiCommand instance with space growiCommandArgs', () => { // setup const slashCommandText = 'search hoge '; const slashCommand = new SlashCommandMock(slashCommandText); // when const result = parse(slashCommand); // then expect(result.text).toBe(slashCommandText); expect(result.growiCommandType).toBe('search'); expect(result.growiCommandArgs).toStrictEqual(['hoge']); }); test('returns a GrowiCommand instance', () => { // setup const slashCommandText = 'search keyword1 keyword2'; const slashCommand = new SlashCommandMock(slashCommandText); // when const result = parse(slashCommand); // then expect(result.text).toBe(slashCommandText); expect(result.growiCommandType).toBe('search'); expect(result.growiCommandArgs).toStrictEqual(['keyword1', 'keyword2']); }); });