distinctUntilChanged.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. describe('distinctUntilChanged', function()
  2. it('produces an error if its parent errors', function()
  3. local _, onError = observableSpy(Rx.Observable.throw():distinctUntilChanged())
  4. expect(#onError).to.equal(1)
  5. end)
  6. describe('with the default comparator', function()
  7. it('produces a value if it is the first value or different from the previous', function()
  8. local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):distinctUntilChanged()
  9. expect(observable).to.produce(1, 3, 1, 4, 5)
  10. end)
  11. it('produces the first value if it is nil', function()
  12. local observable = Rx.Observable.of(nil):distinctUntilChanged()
  13. expect(observable).to.produce({{}})
  14. end)
  15. it('produces multiple onNext arguments but only uses the first argument to check for equality', function()
  16. local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs, true):distinctUntilChanged()
  17. expect(observable).to.produce({{1, 1}, {3, 3}, {1, 4}, {4, 5}, {5, 6}})
  18. end)
  19. end)
  20. describe('with a custom comparator', function()
  21. it('produces a value if it is the first value or the comparator returns false when passed the previous value and the current value', function()
  22. local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):distinctUntilChanged(function(x, y) return x % 2 == y % 2 end)
  23. expect(observable).to.produce(1, 4, 5)
  24. end)
  25. it('produces the first value if it is nil', function()
  26. local observable = Rx.Observable.of(nil):distinctUntilChanged(function(x, y) return true end)
  27. expect(observable).to.produce({{}})
  28. end)
  29. it('calls onError if the comparator errors', function()
  30. local onError = spy()
  31. Rx.Observable.fromRange(2):distinctUntilChanged(error):subscribe(nil, onError, nil)
  32. expect(#onError).to.equal(1)
  33. end)
  34. end)
  35. end)