elementAt.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. describe('elementAt', function()
  2. it('errors when its parent errors', function()
  3. expect(Rx.Observable:throw():elementAt(0)).to.produce.error()
  4. end)
  5. it('chains subscriptions', function()
  6. local subscription = Rx.Subscription.create()
  7. local observable = Rx.Observable.create(function() return subscription end)
  8. expect(observable:subscribe()).to.equal(subscription)
  9. expect(observable:elementAt(1):subscribe()).to.equal(subscription)
  10. end)
  11. it('unsubscribes the subscription if the element is produced', function()
  12. local subject = Rx.Subject.create()
  13. local subscription = subject:elementAt(2):subscribe()
  14. subscription.unsubscribe = spy()
  15. subject:onNext('a')
  16. expect(#subscription.unsubscribe).to.equal(0)
  17. subject:onNext('b')
  18. expect(#subscription.unsubscribe).to.equal(1)
  19. end)
  20. it('errors if no index is specified', function()
  21. expect(function () Rx.Observable.of(1):elementAt() end).to.fail()
  22. end)
  23. it('produces no values if the specified index is less than one', function()
  24. expect(Rx.Observable.of(1):elementAt(0)).to.produce.nothing()
  25. expect(Rx.Observable.of(1):elementAt(-1)).to.produce.nothing()
  26. end)
  27. it('produces no values if the specified index is greater than the number of elements produced by the source', function()
  28. expect(Rx.Observable.of(1):elementAt(2)).to.produce.nothing()
  29. end)
  30. it('produces all values produced by the source at the specified index', function()
  31. local observable = Rx.Observable.create(function(observer)
  32. observer:onNext(1, 2, 3)
  33. observer:onNext(4, 5, 6)
  34. observer:onCompleted()
  35. end)
  36. expect(observable:elementAt(2)).to.produce({{4, 5, 6}})
  37. end)
  38. end)