Sha256: 03a3dee9479159aef13cc4976d3bfb0559e7257bcdc363df1b2c3da7fafa6e42

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

describe SlackRubyBot::Hooks::Set do
  let(:client) { Slack::RealTime::Client.new }

  context 'adding hooks' do
    subject { described_class.new(client) }

    it 'lets you add hook handlers' do
      handler = ->(_, _) {}

      expect do
        subject.add(:message, handler)
      end.to change(subject, :handlers)

      expect(subject.handlers).to have_key(:message)
      expect(subject.handlers[:message]).to eq Set.new.add(handler)
    end

    it 'lets you add multiple handlers for the same hook' do
      handler_1 = ->(_, _) {}
      handler_2 = ->(_, _) {}

      expect do
        subject.add(:message, handler_1)
        subject.add(:message, handler_2)
      end.to change(subject, :handlers)

      expect(subject.handlers).to have_key(:message)
      expect(subject.handlers[:message]).to eq Set.new([handler_1, handler_2])
    end
  end

  context 'Slack Client injection' do
    subject { described_class.new }

    it "doesn't barf when add callbacks prior to injecting client" do
      expect do
        subject.add(:hook, ->(_, _) {})
      end.to_not raise_error
    end

    it 'triggers hooks when client is configured later' do
      handler = ->(_, _) {}

      subject.add(:hook, handler)

      expect(subject).to receive(:flush_handlers).once.and_call_original
      expect(handler).to receive(:call).once

      subject.client = client

      client.send(:callback, { data: 1 }, :hook) # Abusing the callback method 100% :D
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slack-ruby-bot-0.16.1 spec/slack-ruby-bot/hooks/set_spec.rb
slack-ruby-bot-0.16.0 spec/slack-ruby-bot/hooks/set_spec.rb
slack-ruby-bot-0.15.0 spec/slack-ruby-bot/hooks/set_spec.rb
slack-ruby-bot-0.14.0 spec/slack-ruby-bot/hooks/set_spec.rb
slack-ruby-bot-0.13.0 spec/slack-ruby-bot/hooks/set_spec.rb