Cuu.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.7 KiB

  1. import { EventEmitter } from 'events';
  2. import CLIENT from '../src/client';
  3. import '../src/handlers';
  4. jest.mock('dotenv', () => ({
  5. config: () => {
  6. process.env.DISCORD_BOT_TOKEN = 'token';
  7. process.env.DISCORD_BOT_INTENTS = 'GUILDS,GUILD_MESSAGES';
  8. },
  9. }));
  10. jest.mock('discord.js', () => ({
  11. Client: class MockClient extends EventEmitter {
  12. public user: any = {};
  13. public token = '';
  14. async login(token: string) {
  15. this.token = token;
  16. this.user = {
  17. id: '0',
  18. tag: 'User#0000',
  19. };
  20. return Promise.resolve(token);
  21. }
  22. },
  23. }));
  24. describe('Example', () => {
  25. let defaultConsoleLog: typeof console.log;
  26. beforeEach(() => {
  27. defaultConsoleLog = console.log;
  28. console.log = jest.fn();
  29. });
  30. afterEach(() => {
  31. console.log = defaultConsoleLog;
  32. });
  33. beforeEach(async () => {
  34. await CLIENT.login('token');
  35. });
  36. it('should ensure logged in user exists', () => {
  37. expect(CLIENT.user).toEqual({
  38. id: '0',
  39. tag: 'User#0000',
  40. });
  41. });
  42. it('should handle ready event', () => {
  43. CLIENT.emit('ready', CLIENT);
  44. expect(console.log).toBeCalledWith('client ready');
  45. });
  46. it('should handle messageCreate event by echoing user message', () => {
  47. const payload: Record<string, any> = {
  48. author: {
  49. id: '1',
  50. tag: 'Anon#1337',
  51. },
  52. embeds: [],
  53. content: 'Test content.',
  54. mentions: {
  55. users: new Map([
  56. ['0', {
  57. id: '0',
  58. tag: 'User#0000',
  59. }],
  60. ]),
  61. },
  62. reply: jest.fn(),
  63. };
  64. CLIENT.emit('messageCreate', payload as any);
  65. expect(payload.reply).toBeCalledWith({
  66. embeds: [],
  67. content: 'Test content.',
  68. });
  69. });
  70. });