|
@@ -0,0 +1,87 @@
|
|
|
+describe('ReplaySubject', function()
|
|
|
+ describe('create', function()
|
|
|
+ it('returns a ReplaySubject', function()
|
|
|
+ expect(Rx.ReplaySubject.create()).to.be.an(Rx.ReplaySubject)
|
|
|
+ end)
|
|
|
+
|
|
|
+ it('sets an appropriate buffer size if it is specified', function()
|
|
|
+ local subject = Rx.ReplaySubject.create(2)
|
|
|
+ local observer = Rx.Observer.create()
|
|
|
+ local onNext = spy(observer, '_onNext')
|
|
|
+ subject:onNext(1)
|
|
|
+ subject:onNext(2)
|
|
|
+ subject:onNext(3)
|
|
|
+ subject:subscribe(observer)
|
|
|
+ expect(onNext).to.equal({{2}, {3}})
|
|
|
+ end)
|
|
|
+
|
|
|
+ it('keeps an infinite buffer if no buffer size is specified', function()
|
|
|
+ local subject = Rx.ReplaySubject.create()
|
|
|
+ local observer = Rx.Observer.create()
|
|
|
+ local onNext = spy(observer, '_onNext')
|
|
|
+ subject:onNext(1)
|
|
|
+ subject:onNext(2)
|
|
|
+ subject:onNext(3)
|
|
|
+ subject:subscribe(observer)
|
|
|
+ expect(onNext).to.equal({{1}, {2}, {3}})
|
|
|
+ end)
|
|
|
+ end)
|
|
|
+
|
|
|
+ describe('subscribe', function()
|
|
|
+ it('returns a Subscription', function()
|
|
|
+ local subject = Rx.ReplaySubject.create()
|
|
|
+ local observer = Rx.Observer.create()
|
|
|
+ expect(subject:subscribe(observer)).to.be.an(Rx.Subscription)
|
|
|
+ end)
|
|
|
+
|
|
|
+ it('accepts 3 functions as arguments', function()
|
|
|
+ local onNext, onCompleted = spy(), spy()
|
|
|
+ local subject = Rx.ReplaySubject.create()
|
|
|
+ subject:subscribe(onNext, nil, onCompleted)
|
|
|
+ subject:onNext(5)
|
|
|
+ subject:onCompleted()
|
|
|
+ expect(onNext).to.equal({{5}})
|
|
|
+ expect(#onCompleted).to.equal(1)
|
|
|
+ end)
|
|
|
+
|
|
|
+ it('calls onNext with the current buffer', function()
|
|
|
+ local subject = Rx.ReplaySubject.create(2)
|
|
|
+ local observer = Rx.Observer.create()
|
|
|
+ local onNext = spy(observer, '_onNext')
|
|
|
+ subject:onNext(1)
|
|
|
+ subject:onNext(2)
|
|
|
+ subject:onNext(3)
|
|
|
+ subject:subscribe(observer)
|
|
|
+ expect(onNext).to.equal({{2}, {3}})
|
|
|
+ end)
|
|
|
+ end)
|
|
|
+
|
|
|
+ describe('onNext', function()
|
|
|
+ it('pushes values to all subscribers', function()
|
|
|
+ local observers = {}
|
|
|
+ local spies = {}
|
|
|
+ for i = 1, 2 do
|
|
|
+ observers[i] = Rx.Observer.create()
|
|
|
+ spies[i] = spy(observers[i], '_onNext')
|
|
|
+ end
|
|
|
+
|
|
|
+ local subject = Rx.ReplaySubject.create()
|
|
|
+ subject:subscribe(observers[1])
|
|
|
+ subject:subscribe(observers[2])
|
|
|
+ subject:onNext(1)
|
|
|
+ subject:onNext(2)
|
|
|
+ subject:onNext(3)
|
|
|
+ expect(spies[1]).to.equal({{1}, {2}, {3}})
|
|
|
+ expect(spies[2]).to.equal({{1}, {2}, {3}})
|
|
|
+ end)
|
|
|
+
|
|
|
+ it('can be called using function syntax', function()
|
|
|
+ local observer = Rx.Observer.create()
|
|
|
+ local subject = Rx.ReplaySubject.create()
|
|
|
+ local onNext = spy(observer, 'onNext')
|
|
|
+ subject:subscribe(observer)
|
|
|
+ subject(4)
|
|
|
+ expect(#onNext).to.equal(1)
|
|
|
+ end)
|
|
|
+ end)
|
|
|
+end)
|