elementAt.lua 1.7 KB

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