From ef7977fc2850dddb8a30fed621ca9a549bb487f9 Mon Sep 17 00:00:00 2001 From: Joey Guerra Date: Fri, 5 Apr 2024 10:14:58 -0500 Subject: [PATCH] fix: is not a function in CatchAll listener --- src/Robot.mjs | 2 +- test/Robot_test.mjs | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Robot.mjs b/src/Robot.mjs index 1f16a292a..58e294605 100644 --- a/src/Robot.mjs +++ b/src/Robot.mjs @@ -229,7 +229,7 @@ class Robot { } this.listen(isCatchAllMessage, options, async msg => { - await callback(msg.message) + await callback(msg) }) } diff --git a/test/Robot_test.mjs b/test/Robot_test.mjs index 5b1464cc3..b823558dc 100644 --- a/test/Robot_test.mjs +++ b/test/Robot_test.mjs @@ -3,7 +3,7 @@ import { describe, it, beforeEach, afterEach } from 'node:test' import assert from 'node:assert/strict' import path from 'node:path' -import { Robot, CatchAllMessage, EnterMessage, LeaveMessage, TextMessage, TopicMessage, User } from '../index.mjs' +import { Robot, CatchAllMessage, EnterMessage, LeaveMessage, TextMessage, TopicMessage, User, Response } from '../index.mjs' import mockAdapter from './fixtures/MockAdapter.mjs' describe('Robot', () => { describe('#http', () => { @@ -204,11 +204,30 @@ describe('Robot', () => { it('sends a CatchAllMessage if no listener matches', async () => { const testMessage = new TextMessage(user, 'message123') robot.listeners = [] + let actual = null robot.catchAll(async (message) => { - assert.ok(message instanceof CatchAllMessage) - assert.deepEqual(message.message, testMessage) + actual = message }) await robot.receive(testMessage) + assert.ok(actual.message instanceof CatchAllMessage) + assert.deepEqual(actual.message.message, testMessage) + }) + + it('calls the catch-all listener with a Response object', async () => { + const testMessage = new TextMessage(user, 'message123') + + const listenerCallback = async () => { + assert.fail('Should not have called listener') + } + robot.hear(/^no-matches$/, listenerCallback) + let actual = null + robot.catchAll(async response => { + response.reply('caught by catchAll') + actual = response + }) + + await robot.receive(testMessage) + assert.ok(actual instanceof Response) }) it('does not trigger a CatchAllMessage if a listener matches', async () => { @@ -572,7 +591,7 @@ describe('Robot', () => { robot.hear(/^no-matches$/, listenerCallback) robot.catchAll(async response => { - assert.deepEqual(response.message, testMessage) + assert.deepEqual(response.message.message, testMessage) }) await robot.receive(testMessage)