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

Dispatched Actions from Effects getting not emitted though "customActionsStream" #57

Open
TobbiAR opened this issue Apr 25, 2023 · 3 comments

Comments

@TobbiAR
Copy link
Contributor

TobbiAR commented Apr 25, 2023

Which @ngneat/effects-* package(s) are the source of the bug?

effects-ng

Is this a regression?

No

Description

During writing jest tests for effects I've encountered the problem, that actions, which are dispatched by effects are not getting emitted inside customActionsStream. During runtime and regular use (without customActionsStream), this problem does not exist.

The simple test example below illustrates the problem: loadTodos gets dispached and dispatches after completion loadTodos2.
The test fails, because loadTodos2 never apprears inside customActionsStream$.

How I've encountered this problem? / Use Case
I want to test effects, which are performing store updates. To ensure, that the update process triggered by the effect is properly performed, the effect dispatches a "completion" action. When the completion action is dispatched, I'm able to verify, that the performed state mutations/ async side-effects are properly done.

Reproduction example:

import { TestBed } from '@angular/core/testing';
import { Actions, createAction, createEffect, ofType } from '@ngneat/effects';
import { provideEffects, provideEffectsManager } from '@ngneat/effects-ng';
import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';

const loadTodos = createAction('[Todos] Load Todos');
const loadTodos2 = createAction('[Todos] Load Todos2');

@Injectable()
class EffectsOne {
  loadTodos$ = createEffect(
    (actions$) =>
      actions$.pipe(
        ofType(loadTodos),
        map(() => loadTodos2()),
      ),
    { dispatch: true },
  );
}

it('should emit loadTodos2 action', (done) => {
  const customActionsStream$ = new Actions();

  TestBed.configureTestingModule({
    providers: [
      provideEffects(EffectsOne),
      // eslint-disable-next-line rxjs/finnish
      provideEffectsManager({ customActionsStream: customActionsStream$, dispatchByDefault: true }),
    ],
  });

  customActionsStream$.pipe(ofType(loadTodos2)).subscribe((todos) => {
    expect(todos).toBeDefined();
    done();
  });
  customActionsStream$.dispatch(loadTodos());
});

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Error: thrown: "Exceeded timeout of 5000 ms for a test while waiting for `done()` to be called.
Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."

Please provide the environment you discovered this bug in

jest 29.5.0
@ngneat/effects 2.1.1
@ngneat/effects-ng 3.1.2

Anything else?

No response

Do you want to create a pull request?

No

@EricPoul
Copy link
Collaborator

I checked a code. There's a bug when we return an action to dispatch in effect. You're welcome to make a pr. We need to change a little subscribeEffect and check for customActionsStream not only in effect.sourceFn but also while dispatching in the subscribe.

@EricPoul
Copy link
Collaborator

EricPoul commented Jun 9, 2023

@TobbiAR please update to the latest version. I made a little fix so it should work fine now.

@TobbiAR
Copy link
Contributor Author

TobbiAR commented Jun 26, 2023

I'll test this feature as soon I'm able to migrate my project to angular v16. Thank you!

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

2 participants