-
Notifications
You must be signed in to change notification settings - Fork 12
Description
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); // failsIt 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
- Change the behaviour of
.receivedNext() - 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. - 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.