Comments (5)
Thanks @JasonLandbridge !
lol, I subscribed :)
I like the idea
I think I'd change onEmitted
to onCount
or onValuesCount
to make it more clear..
because onEmitted
might also be referring to the values themselves (like when the number 5 arrives, stop)
Mind creating a PR?
from observer-spy.
Haha I will keep it in mind, thanks for the consideration!
from observer-spy.
Awesome, sure I will create something! Not to be outdone by your naming suggestion, maybe onFirstValue
and onValues
to be in line with getFirstValue
and getValues
? Or would that be confusing? Naming things is definitely the most difficult part of programming :p
Anyway, I will make something and the naming can always change so I will go start with your suggestion. 👍
from observer-spy.
So, this sounded good in theory but not so much in practice 😅
Consider the following:
const arrayObservable$ = of(0,1,2,3,4,5,6,7,8,9,10);
const observerSpy: ObserverSpy<number> = new ObserverSpy();
const _subscription = arrayObservable$.subscribe(observerSpy)
await observerSpy.onValuesCount(5);
expect(observerSpy.getValues()).toEqual([0,1,2,3,4]);
Before the await is reached, the observable has already emitted all values from 0 to 10 at the moment the .subscribe()
is called. Which makes the await useless as it instantly resolves due to the count being reached.
A possibility here would be to add the onValuesCount
before the subscription is called and then await after the subscription.
So it would be something like this:
const arrayObservable$ = of(0,1,2,3,4,5,6,7,8,9,10);
const observerSpy: ObserverSpy<number> = new ObserverSpy({valueCount: 5}); // Add the valueCount as a config property
const _subscription = arrayObservable$.subscribe(observerSpy)
await observerSpy.onValuesCount();
expect(observerSpy.getValues()).toEqual([0,1,2,3,4]);
This would "work", but then this is way more verbose than the following:
const arrayObservable$ = of(0,1,2,3,4,5,6,7,8,9,10).pipe(take(5)); // Add the take(5) here which completes
const observerSpy: ObserverSpy<number> = new ObserverSpy();
const _subscription = arrayObservable$.subscribe(observerSpy)
await observerSpy.onComplete();
expect(observerSpy.getValues()).toEqual([0,1,2,3,4]);
So my proposed idea is worse then the simple addition of .pipe(take(5))
and awaiting observable$.onComplete()
. Which actually works perfectly with an observable that should stay alive in production but during testing you just need the first 5 values.
Please let me know if I missed something that would make this idea viable!
from observer-spy.
Yes I agree that the extra ceremony makes it equivalent to just using plain RxJS
I can't think about a simple way from the top of my head, so unless you do, let's close this issue for now and revisit if a "A HA" moment will pop up in the shower 😊
Thanks again @JasonLandbridge !
from observer-spy.
Related Issues (16)
- Promise support for `onComplete` HOT 4
- DISCUSSION 1: ObserverSpy API - Methods vs getters HOT 7
- DISCUSSION 2: ObserverSpy API - passing `OnComplete` callback in the constructor? HOT 8
- DISCUSSION 3: `subscribeAndSpyOn` + `unsubscribe` VS `spyOn` and `dispose` HOT 10
- `ObserverSpy` swallows errors HOT 1
- is there a method to count the number of times the source observable has emitted? HOT 5
- The automated release is failing 🚨
- Jest matchers HOT 8
- Requires ESM module transformation HOT 7
- Support for RxJS 7 HOT 1
- subscribeSpyTo is causing the ''ERROR TypeError: You provided 'undefined' where a stream was expected'' with the RxJS 7 upgrade HOT 2
- subscribeSpyTo<T> returns SubscriberSpy<unknown> instead of SubscriberSpy<T> HOT 6
- Make .expectErrors() return this HOT 5
- Improve .receivedNext()
- Simplify usage with factory function and internal subscription handling HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from observer-spy.