GithubHelp home page GithubHelp logo

Comments (5)

shairez avatar shairez commented on May 28, 2024 1

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.

JasonLandbridge avatar JasonLandbridge commented on May 28, 2024 1

Haha I will keep it in mind, thanks for the consideration!

from observer-spy.

JasonLandbridge avatar JasonLandbridge commented on May 28, 2024

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.

JasonLandbridge avatar JasonLandbridge commented on May 28, 2024

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.

shairez avatar shairez commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.