Sha256: 5dc729f361e950ab57ba22f0e55a65683c970938e703add2cf8ff752702e1f83

Contents?: true

Size: 1.83 KB

Versions: 28

Compression:

Stored size: 1.83 KB

Contents

require 'test/unit'
require 'promise/v2'

class TestPromiseThen < Test::Unit::TestCase
  def test_calls_the_block_when_the_promise_has_already_been_resolved
    x = 42
    PromiseV2.value(23)
      .then { |v| x = v }
      .always { assert_equal(x, 23) }
  end

  def test_calls_the_block_when_the_promise_is_resolved
    a = PromiseV2.new
    x = 42

    p = a.then { |v| x = v }
    a.resolve(23)

    p.always { assert_equal(x, 23) }
  end

  def test_works_with_multiple_chains
    x = 42
    PromiseV2.value(2)
      .then { |v| v * 2 }
      .then { |v| v * 4 }
      .then { |v| x = v }
      .always { assert_equal(x, 16) }
  end

  def test_works_when_a_block_returns_a_promise
    a = PromiseV2.new
    b = PromiseV2.new

    x = 42
    p = a.then { b }.then { |v| x = v }

    a.resolve(42)
    b.resolve(23)

    p.always { assert_equal(x, 23) }
  end

  def test_sends_raised_exceptions_as_rejections
    x = nil

    PromiseV2.value(2)
      .then { raise "hue" }
      .rescue { |v| x = v }
      .always { assert_equal(x.class, RuntimeError) }
  end

  def test_sends_raised_exceptions_inside_rescue_blocks_as_next_errors
    x = nil

    PromiseV2.value(2)
      .then { raise "hue" }
      .rescue { raise "omg" }
      .rescue { |v| x = v }
      .always { assert_equal(x.class, RuntimeError) }
  end

  def test_allows_then_to_be_called_multiple_times
    pr = PromiseV2.value(2)
    x = 1

    ps = []

    ps << pr.then { x += 1 }
    ps << pr.then { x += 1 }

    PromiseV2.when(ps).always { assert_equal(x, 3) }
  end

  def test_raises_with_thenB_if_a_promise_has_already_been_chained
    pr = PromiseV2.new

    pr.then! {}

    assert_raise(ArgumentError) { pr.then! {} }
  end

  def test_should_pass_a_delayed_falsy_value
    pr = PromiseV2.new.resolve(5).then { nil }

    pr.always do |value|
      assert_equal(value, nil)
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 test/opal/promisev2/test_then.rb
opal-1.8.2 test/opal/promisev2/test_then.rb
opal-1.8.1 test/opal/promisev2/test_then.rb
opal-1.8.0 test/opal/promisev2/test_then.rb
opal-1.8.0.beta1 test/opal/promisev2/test_then.rb
opal-1.7.4 test/opal/promisev2/test_then.rb
opal-1.8.0.alpha1 test/opal/promisev2/test_then.rb
opal-1.7.3 test/opal/promisev2/test_then.rb
opal-1.7.2 test/opal/promisev2/test_then.rb
opal-1.7.1 test/opal/promisev2/test_then.rb
opal-1.7.0 test/opal/promisev2/test_then.rb
opal-1.7.0.rc1 test/opal/promisev2/test_then.rb
opal-1.6.1 test/opal/promisev2/test_then.rb
opal-1.6.0 test/opal/promisev2/test_then.rb
opal-1.6.0.rc1 test/opal/promisev2/test_then.rb
opal-1.6.0.alpha1 test/opal/promisev2/test_then.rb
opal-1.5.1 test/opal/promisev2/test_then.rb
opal-1.5.0 test/opal/promisev2/test_then.rb
opal-1.5.0.rc1 test/opal/promisev2/test_then.rb
opal-1.4.1 test/opal/promisev2/test_then.rb