Sha256: 87e5e0e8939d9da7d398ce9dfa2d806a180f75317751a59068bf968c7fe1a660

Contents?: true

Size: 996 Bytes

Versions: 2

Compression:

Stored size: 996 Bytes

Contents

require 'dry/events/listener'

RSpec.describe Dry::Events::Listener do
  subject(:listener) do
    Class.new {
      include Dry::Events::Listener[:test_publisher]
    }
  end

  let!(:publisher) do
    Class.new {
      include Dry::Events::Publisher[:test_publisher]

      register_event :test_event
    }.new
  end

  describe '.subscribe' do
    let(:captured) { [] }

    it 'subscribes a listener at class level' do
      listener.subscribe(:test_event) do |event|
        captured << event.id
      end

      publisher.publish(:test_event)

      expect(captured).to eql([:test_event])
    end

    describe 'filters' do
      it 'filters events' do
        listener.subscribe(:test_event, level: :info) do |event|
          captured << event.payload
        end

        publisher.publish(:test_event)
        publisher.publish(:test_event, level: :debug)
        publisher.publish(:test_event, level: :info)

        expect(captured).to eql([level: :info])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-events-0.2.0 spec/unit/dry/events/listener_spec.rb
dry-events-0.1.1 spec/unit/dry/events/listener_spec.rb