describe('concat', function()
  it('produces an error if its parent errors', function()
    expect(Rx.Observable.throw():concat()).to.produce.error()
  end)

  it('returns the first argument if it is the only argument', function()
    local observable = Rx.Observable.fromRange(1, 3):concat()
    expect(observable).to.produce(1, 2, 3)
  end)

  it('waits until one observable completes before producing items from the next', function()
    local subjectA = Rx.Subject.create()
    local subjectB = Rx.Subject.create()
    local onNext, onError, onCompleted = observableSpy(Rx.Observable.concat(subjectA, subjectB))
    subjectA:onNext(1)
    subjectB:onNext(2)
    subjectA:onNext(3)
    subjectA:onCompleted()
    subjectB:onNext(4)
    subjectB:onNext(5)
    subjectB:onCompleted()
    expect(onNext).to.equal({{1}, {3}, {4}, {5}})
    expect(#onError).to.equal(0)
    expect(#onCompleted).to.equal(1)
  end)

  it('should error if any of the sources error', function()
    local badObservable = Rx.Observable.create(function(observer) observer:onError('oh no') end)
    local observable = Rx.Observable.of(1):concat(Rx.Observable.of(2), badObservable)
    expect(observable).to.produce.error()
  end)

  it('should complete once the rightmost observable completes', function()
    local subject = Rx.Subject.create()
    local onCompleted = spy()
    local observable = Rx.Observable.concat(Rx.Observable.fromRange(1, 5), Rx.Observable.fromRange(1, 5), subject)
    observable:subscribe(nil, nil, onCompleted)
    expect(#onCompleted).to.equal(0)
    subject:onCompleted()
    expect(#onCompleted).to.equal(1)
  end)
end)
