Skip to content

Commit

Permalink
add unsubscribe support
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmehta committed Mar 22, 2020
1 parent 90f3b92 commit 2a6dfb6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -17,7 +17,13 @@ function StaticObservable() {
var call = self._observerCalls.shift()
observer[call.method].apply(observer, call.args)
}
return new Subscription()
return new Subscription(() => {
this._observer = {
next: () => {},
error: () => {},
complete: () => {}
}
})
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/static-observable.test.js
Expand Up @@ -172,4 +172,24 @@ describe('static-observable', () => {
}, 0)
})
})

describe('unsubscribe', () => {
it('should ignore any next after', function(done) {
var err = new Error('boom')
var values = [{ foo: 1 }, { foo: 2 }]
var observable = StaticObservable.next(values[0])
var onNext = jest.fn()
var onError = jest.fn()
var onComplete = jest.fn()
observable.subscribe(onNext, onError, onComplete).unsubscribe()
observable.next(values[1])
setTimeout(() => {
expect(onNext).toHaveBeenCalledWith(values[0])
expect(onNext).not.toHaveBeenCalledWith(values[1])
expect(onError).not.toHaveBeenCalled()
expect(onComplete).not.toHaveBeenCalled()
done()
}, 0)
})
})
})

0 comments on commit 2a6dfb6

Please sign in to comment.