slash-command-parser.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { SlashCommand } from '@slack/bolt';
  2. import { InvalidGrowiCommandError } from '~/models/errors';
  3. import { parse } from './slash-command-parser';
  4. const SlashCommandMock = jest.fn<SlashCommand, [string]>().mockImplementation((text) => {
  5. return { text } as SlashCommand;
  6. });
  7. describe('parse SlashCommand', () => {
  8. describe('without growiCommandType', () => {
  9. test('throws InvalidGrowiCommandError', () => {
  10. // setup
  11. const slashCommandText = '';
  12. const slashCommand = new SlashCommandMock(slashCommandText);
  13. // when/then
  14. expect(() => {
  15. parse(slashCommand);
  16. }).toThrowError(InvalidGrowiCommandError);
  17. });
  18. });
  19. test('returns a GrowiCommand instance with empty growiCommandArgs', () => {
  20. // setup
  21. const slashCommandText = 'search';
  22. const slashCommand = new SlashCommandMock(slashCommandText);
  23. // when
  24. const result = parse(slashCommand);
  25. // then
  26. expect(result.text).toBe(slashCommandText);
  27. expect(result.growiCommandType).toBe('search');
  28. expect(result.growiCommandArgs).toStrictEqual([]);
  29. });
  30. test('returns a GrowiCommand instance with space growiCommandType', () => {
  31. // setup
  32. const slashCommandText = 'search ';
  33. const slashCommand = new SlashCommandMock(slashCommandText);
  34. // when
  35. const result = parse(slashCommand);
  36. // then
  37. expect(result.text).toBe(slashCommandText);
  38. expect(result.growiCommandType).toBe('search');
  39. expect(result.growiCommandArgs).toStrictEqual([]);
  40. });
  41. test('returns a GrowiCommand instance with space growiCommandArgs', () => {
  42. // setup
  43. const slashCommandText = 'search hoge ';
  44. const slashCommand = new SlashCommandMock(slashCommandText);
  45. // when
  46. const result = parse(slashCommand);
  47. // then
  48. expect(result.text).toBe(slashCommandText);
  49. expect(result.growiCommandType).toBe('search');
  50. expect(result.growiCommandArgs).toStrictEqual(['hoge']);
  51. });
  52. test('returns a GrowiCommand instance', () => {
  53. // setup
  54. const slashCommandText = 'search keyword1 keyword2';
  55. const slashCommand = new SlashCommandMock(slashCommandText);
  56. // when
  57. const result = parse(slashCommand);
  58. // then
  59. expect(result.text).toBe(slashCommandText);
  60. expect(result.growiCommandType).toBe('search');
  61. expect(result.growiCommandArgs).toStrictEqual(['keyword1', 'keyword2']);
  62. });
  63. });