Skip to content

Commit 69b8f4e

Browse files
feat: add subscribeAndSpyOn (#8)
Co-authored-by: Shai Reznik <shairez@users.noreply.github.com>
1 parent 10d4999 commit 69b8f4e

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ A simple little class and a helper function that help make Observable testing a
88
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
99
[![codecov](https://img.shields.io/codecov/c/github/hirezio/observer-spy.svg)](https://codecov.io/gh/hirezio/observer-spy) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
1010
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)
11-
<!-- ALL-CONTRIBUTORS-BADGE:END -->
11+
12+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
1213

1314
<div align="center">
1415
<a href="http://testangular.com/?utm_source=github&utm_medium=link&utm_campaign=observer-spy">
@@ -135,6 +136,32 @@ it('should spy on Observable errors', () => {
135136
});
136137
```
137138

139+
## Quick Usage with `subscribeAndSpyOn(observable)`
140+
141+
You can also create an `ObserverSpy` and immediately subscribe to an observable with this simple helper function.
142+
Observer spies generated that way will provide an additional `unsubscribe()` method that you might want to call
143+
if your source observable does not complete or does not get terminated by an error while testing.
144+
145+
**Example:**
146+
147+
```js
148+
import { subscribeAndSpyOn } from '@hirez_io/observer-spy';
149+
150+
it('should immediately subscribe and spy on Observable ', () => {
151+
const fakeObservable = of('first', 'second', 'third');
152+
153+
// get an "ObserverSpyWithSubscription"
154+
const observerSpy = subscribeAndSpyOn(fakeObservable);
155+
// and optionally unsubscribe
156+
observerSpy.unsubscribe();
157+
158+
expect(observerSpy.getFirstValue()).toEqual('first');
159+
160+
// or use the shorthand version:
161+
expect(subscribeAndSpyOn(fakeObservable).getFirstValue()).toEqual('first');
162+
});
163+
```
164+
138165
# Testing Async Observables
139166

140167
#### `it('should do something', fakeTime((flush) => { ... flush(); });`
@@ -314,6 +341,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
314341

315342
<!-- markdownlint-enable -->
316343
<!-- prettier-ignore-end -->
344+
317345
<!-- ALL-CONTRIBUTORS-LIST:END -->
318346

319347
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { ObserverSpy } from './observer-spy';
2+
export { subscribeAndSpyOn, ObserverSpyWithSubscription } from './subscribe-and-spy-on';
23
export { fakeTime } from './fake-time';

src/subscribe-and-spy-on.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Subject } from 'rxjs';
2+
import { subscribeAndSpyOn } from './subscribe-and-spy-on';
3+
4+
describe('subscribeAndSpyOn', () => {
5+
it('should match with given subscription points', () => {
6+
const fakeSubject = new Subject<number>();
7+
fakeSubject.next(1);
8+
const observerSpy = subscribeAndSpyOn(fakeSubject.asObservable());
9+
10+
fakeSubject.next(2);
11+
fakeSubject.next(3);
12+
observerSpy.unsubscribe();
13+
14+
fakeSubject.next(4);
15+
fakeSubject.complete();
16+
17+
expect(observerSpy.getFirstValue()).toBe(2);
18+
expect(observerSpy.getLastValue()).toBe(3);
19+
});
20+
});

src/subscribe-and-spy-on.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Observable, Subscription } from 'rxjs';
2+
import { ObserverSpy } from './observer-spy';
3+
4+
export function subscribeAndSpyOn<T>(observableUnderTest: Observable<T>) {
5+
return new ObserverSpyWithSubscription(observableUnderTest);
6+
}
7+
8+
export class ObserverSpyWithSubscription<T> extends ObserverSpy<T> {
9+
public subscription = new Subscription();
10+
11+
constructor(observableUnderTest: Observable<T>) {
12+
super();
13+
this.subscription.add(observableUnderTest.subscribe(this));
14+
}
15+
16+
unsubscribe(): void {
17+
this.subscription.unsubscribe();
18+
}
19+
}

0 commit comments

Comments
 (0)