Sha256: 330d14d52807f45921518c7f9cb175734f7022252c53a467e72c7e429f097e78

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Acfs::Request::Callbacks do
  let(:callback) { ->(_res) {} }
  let(:request)  { Acfs::Request.new('fubar') }

  describe '#on_complete' do
    it 'stores a given callback' do
      request.on_complete(&callback)

      expect(request.callbacks).to have(1).item
      expect(request.callbacks[0]).to eq callback
    end

    it 'stores multiple callback' do
      request.on_complete {|_res| 'abc' }
      request.on_complete(&callback)

      expect(request.callbacks).to have(2).item
      expect(request.callbacks[0]).to eq callback
    end
  end

  describe '#complete!' do
    let(:response) { Acfs::Response.new(request) }

    it 'triggers registered callbacks with given response' do
      expect(callback).to receive(:call).with(response, kind_of(Proc))

      request.on_complete(&callback)
      request.complete! response
    end

    it 'triggers multiple callback in reverted insertion order' do
      check = []

      request.on_complete do |res, nxt|
        check << 1
        nxt.call res
      end
      request.on_complete do |res, nxt|
        check << 2
        nxt.call res
      end
      request.on_complete do |res, nxt|
        check << 3
        nxt.call res
      end

      request.complete! response

      expect(check).to eq [3, 2, 1]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acfs-2.0.0 spec/acfs/request/callbacks_spec.rb
acfs-1.7.0 spec/acfs/request/callbacks_spec.rb