describe('sample', function()
  it('errors if no sampler is specified', function()
    expect(function() Rx.Observable.empty():sample() end).to.fail()
  end)

  it('produces nil values if the sampler fires before the source does', function()
    local sampler = Rx.Observable.fromRange(3)
    local onNext, onError, onCompleted = observableSpy(Rx.Observable.empty():sample(sampler))
    expect(#onNext).to.equal(0)
  end)

  it('produces the latest value produced by the source when the sampler fires', function()
    local a = Rx.Subject.create()
    local b = Rx.Subject.create()
    local onNext, onError, onCompleted = observableSpy(a:sample(b))
    a:onNext(1)
    b:onNext('a')
    b:onNext('b')
    a:onNext(2)
    a:onNext(3)
    b:onNext('c')
    a:onCompleted()
    b:onCompleted()
    expect(onNext).to.equal({{1}, {1}, {3}})
  end)

  it('completes when the sampler completes', function()
    local a = Rx.Subject.create()
    local b = Rx.Subject.create()
    local onNext, onError, onCompleted = observableSpy(a:sample(b))
    a:onNext(1)
    a:onCompleted()
    b:onNext('a')
    b:onNext('b')
    b:onNext('c')
    expect(#onCompleted).to.equal(0)
    b:onCompleted()
    expect(#onCompleted).to.equal(1)
    expect(onNext).to.equal({{1}, {1}, {1}})
  end)

  it('errors when the source errors', function()
    local a = Rx.Observable.throw()
    local b = Rx.Observable.fromRange(3)
    expect(a:sample(b)).to.produce.error()
  end)

  it('errors when the sampler errors', function()
    local a = Rx.Observable.fromRange(3)
    local b = Rx.Observable.throw()
    expect(a:sample(b)).to.produce.error()
  end)
end)
