describe('takeUntil', function()
  it('produces an error if its parent errors', function()
    local trigger = Rx.Observable.create(function() end)
    local observable = Rx.Observable.of(''):map(function(x) return x() end)
    expect(observable).to.produce.error()
    expect(observable:takeUntil(trigger)).to.produce.error()
  end)

  it('fails if the first argument is not an Observable', function()
    local observable = Rx.Observable.of(3)
    expect(observable:takeUntil(nil)).to.fail()
    expect(observable:takeUntil(0)).to.fail()
    expect(observable:takeUntil({})).to.fail()
  end)

  it('produces all values if the specified observable does not produce any values', function()
    local trigger = Rx.Observable.create(function() end)
    local observable = Rx.Observable.fromTable({2, 3, 4}):takeUntil(trigger)
    expect(observable).to.produce(2, 3, 4)
  end)

  it('produces values after the specified observable produces a value', function()
    local onNext = spy()
    local trigger = Rx.Subject.create()
    local subject = Rx.Subject.create()
    subject:takeUntil(trigger):subscribe(Rx.Observer.create(onNext))
    subject:onNext('a')
    subject:onNext('b')
    trigger:onNext('a')
    subject:onNext('c')
    subject:onNext('d')
    subject:onCompleted()
    expect(onNext).to.equal({{'a'}, {'b'}})
  end)

  it('produces no values if the specified observable fires immediately', function()
    local onNext = spy()
    local subject = Rx.Subject.create()
    subject:takeUntil(Rx.Observable.of(1)):subscribe(Rx.Observer.create(onNext))
    subject:onNext(1)
    subject:onNext(2)
    subject:onNext(3)
    subject:onCompleted()
    expect(onNext).to.equal({})
  end)
end)
