gratifyguy / botkit-mock

Write tests for Botkit.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi-step Testing?

roeintense opened this issue · comments

Hi guys,

We're trying to send multiple messages through a mock adapter. First message instantiates some controller events for interactive components, then we need to fake button pushes and assert against the way the bot responds. However, this.controller.detailed_answers don't populate beyond the first message sent through the controller, and we've tried it several ways. Here's how we just tried it:

it("should respond with appropriate verbiage on thumbsup click", async () => {
      this.middlewareStub.returns([{ answer: "This is a test response." }]);
      const message = await this.controller.usersInput([
        {
          type: "message",
          user: "someUserId",
          channel: "someChannel",
          ts: "1234.45",
          messages: [
            {
              text: "A qna question?",
              isAssertion: true
            }
          ]
        },
        {
          type: "block_actions",
          user: "someUserId",
          channel: "someChannel",
          messages: [
            {
              actions: [{ action_id: "qna_answered_question_undefined" }],
              response_url: "flerpity_floop",
              isAssertion: true
            }
          ]
        }
      ]);

      return assert.strictEqual(
        JSON.stringify(
          this.controller.detailed_answers.someChannel[1].channelData.blocks[1]
        ),
        JSON.stringify("poop")
      );
    });

And we've also done it this way:

it("should respond with appropriate verbiage on thumbsup click", async () => {
      this.middlewareStub.returns([{ answer: "This is a test response." }]);
      const message = await this.controller.usersInput([
        {
          type: "message",
          user: "someUserId",
          channel: "someChannel",
          ts: "1234.45",
          messages: [
            {
              text: "A qna question?",
              isAssertion: true
            },
            {
              actions: [{ action_id: "qna_answered_question_undefined" }],
              response_url: "flerpity_floop",
              isAssertion: true
            }
          ]
        }
      ]);

      return assert.strictEqual(
        JSON.stringify(
          this.controller.detailed_answers.someChannel[1].channelData.blocks[1]
        ),
        JSON.stringify("poop")
      );
    });

We've also just plain ol' called this.controller.usersInput() a second time with a message of type block_actions with no effect.

What are we doing wrong? Happy to put this in SO if you prefer.

Nevermind, I found it.

Botkit Core adds keys to apiLogByKey for api calls. So, I can assert that there is an entry in this.controller.apiLogByKey equal to my fake response_url on my message, which is close enough for me to know that it tried to respond the way I expected it to.

Here's the full green assertion:

it("should send message to other channel on thumbsdown click", async () => {
      this.middlewareStub.returns([{ answer: "This is a test response." }]);
      await this.controller.usersInput([
        {
          type: "message",
          user: "someUserId",
          channel: "someChannel",
          messages: [
            {
              text: "A qna question?",
              isAssertion: true,
              ts: 1234.45
            }
          ]
        }
      ]);

      await this.controller.usersInput([
        {
          type: "block_actions",
          user: "someUserId",
          channel: "someChannel",
          messages: [
            {
              actions: [{ action_id: "qna_did_not_answer_question_1234.45" }],
              response_url: "flerpity_floop",
              thread_ts: 1234.45,
              isAssertion: true
            }
          ]
        }
      ]);

      return assert.strictEqual(
        this.controller.apiLogByKey.flerpity_floop[0].text,
        "Thanks for the feedback."
      );
    });