Sha256: 4d4edd3c09d41bc632788884023ce73db69593fd8d9ac6d258417d61ab3acd88

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

require 'test_helper'

# callbacks are tested in Disposable::Callback::Group.
class OperationCallbackTest < MiniTest::Spec
  Song = Struct.new(:name)

  #---
  # with contract and disposable semantics
  class Create < Trailblazer::Operation
    extend Contract::DSL

    contract do
      property :name
    end

    self.| Model( Song, :new )
    self.| Contract::Build()
    self.| Contract::Validate()
    self.| Callback( :default )


    extend Callback::DSL

    callback do
      on_change :notify_me!
      on_change :notify_you!
    end


    # TODO: always dispatch, pass params.

    def dispatched
      self["dispatched"] ||= []
    end

  private
    def notify_me!(*)
      dispatched << :notify_me!
    end

    def notify_you!(*)
      dispatched << :notify_you!
    end
  end


  class Update < Create
    # TODO: allow skipping groups.
    # skip_dispatch :notify_me!

    callback do
      remove! :on_change, :notify_me!
    end
  end

  #---
  #- inheritance
  it { Update["pipetree"].inspect.must_equal %{[>>operation.new,&model.build,>contract.build,&contract.default.params,&contract.default.validate,&callback.default]} }


  it "invokes all callbacks" do
    res = Create.({"name"=>"Keep On Running"})
    res["dispatched"].must_equal [:notify_me!, :notify_you!]
  end

  it "does not invoke removed callbacks" do
    res = Update.({"name"=>"Keep On Running"})
    res["dispatched"].must_equal [:notify_you!]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trailblazer-2.0.0 test/operation/callback_test.rb
trailblazer-2.0.0.rc1 test/operation/callback_test.rb
trailblazer-2.0.0.beta3 test/operation/callback_test.rb
trailblazer-2.0.0.beta2 test/operation/callback_test.rb