Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve .receivedNext() #67

Open
ortegamarcel opened this issue Sep 19, 2023 · 0 comments
Open

Improve .receivedNext() #67

ortegamarcel opened this issue Sep 19, 2023 · 0 comments

Comments

@ortegamarcel
Copy link

ortegamarcel commented Sep 19, 2023

Use Case / Problem

I am using jest to write a test for a angular 12 component. I would like to use receivedNext() in a specific way, but it doesn't do what I would expect.

Here is an example:

//  I create a spy to a observable, which emits some values when submit$ emits but
// ONLY WHEN service returns something.
 jest.spyOn(mockService, 'load').mockReturnValue(of(values));
const valuesSpy = subscribeSpyTo(component.values$);

// submit
component.submit$.next();
tick();

expect(valuesSpy.receivedNext()).toBe(true); // works until here

// now I want to submit again, but with no values being returned
jest.spyOn(mockService, 'load').mockReturnValue(of(null));

// now submit again
component.submit$.next();
tick();

expect(valueSpy.receivedNext()).toBe(false); // fails, but I would expect that it passes, since it did not emit since the last tick() or the last time I checked.

(The component which is tested here, is implemented declaratively.)

Expected Behaviour

I would expect that receivedNext() would indicate if the observable was "nexted" again AFTER the last time I checked.
So once receivedNext() is called, it resets it. For example when called directly after I would assume that it means "received next two times in a row":

expect(valueSpy.receivedNext()).toBe(true); // passes
expect(valueSpy.receivedNext()).toBe(true); // fails

It would also be awesome if it would "reset" when using tick (inside fakeAsync()). "Received next since the last time something changed". For exmaple:

// check/emit something but don't use receivedNext here
// then do something again
tick(); // tick to forward in time which should reset receivedNext
expect(valueSpy.receivedNext()).toBe(false); // works, since it did not emit since the last tick()

(I could use this in my use case example, to only check at the end that it was not emitted.)

Possible Solutions

  1. Change the behaviour of .receivedNext()
  2. Add a optional parameter. I.e. .receivedNext({ sinceLastTimeChecked: true }) - It would be nice if I could configure the lib in a way that this is the default.
  3. Make it a new function .receivedNextSinceLastTimeChecked() - This is my least favorite option

Work Around

I know it is easily possible to use .getValues() or .getValuesLength() to check if something emitted again but for that I need to know how often it already emitted, which can change while updating the test. Therefore it would be nicer to use .receivedNext() instead.

Additional context

I am using angular v12. RxJS v6 and @hirez_io/observer-spy v2.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant