distinctUntilChanged.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. describe('distinctUntilChanged', function()
  2. it('produces an error if its parent errors', function()
  3. local observable = Rx.Observable.fromValue(''):map(function(x) return x() end)
  4. expect(observable.subscribe).to.fail()
  5. expect(observable:distinctUntilChanged().subscribe).to.fail()
  6. end)
  7. describe('with the default comparator', function()
  8. it('produces a value if it is the first value or different from the previous', function()
  9. local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):distinctUntilChanged()
  10. expect(observable).to.produce(1, 3, 1, 4, 5)
  11. end)
  12. it('produces the first value if it is nil', function()
  13. local observable = Rx.Observable.fromValue(nil):distinctUntilChanged()
  14. expect(observable).to.produce({{}})
  15. end)
  16. it('produces multiple onNext arguments but only uses the first argument to check for equality', function()
  17. local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs, true):distinctUntilChanged()
  18. expect(observable).to.produce({{1, 1}, {3, 3}, {1, 4}, {4, 5}, {5, 6}})
  19. end)
  20. end)
  21. describe('with a custom comparator', function()
  22. 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()
  23. local observable = Rx.Observable.fromTable({1, 1, 3, 1, 4, 5, 5, 5}, ipairs):distinctUntilChanged(function(x, y) return x % 2 == y % 2 end)
  24. expect(observable).to.produce(1, 4, 5)
  25. end)
  26. it('produces the first value if it is nil', function()
  27. local observable = Rx.Observable.fromValue(nil):distinctUntilChanged(function(x, y) return true end)
  28. expect(observable).to.produce({{}})
  29. end)
  30. end)
  31. end)