Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To mock ofActionSuccessful in ngxs, you can use the spyOn operator provided by Jasmine testing framework. This will allow you to spy on the ofActionSuccessful function and provide a mock implementation that you can use in your tests.

Here is an example:

import { of } from 'rxjs';
import { spyOn } from 'jasmine';
import { Store } from '@ngxs/store';
import { ofActionSuccessful } from '@ngxs/store/operators';

describe('Test ofActionSuccessful operator', () => {
  let store: Store;

  beforeEach(() => {
    store = new Store();
  });

  it('should mock ofActionSuccessful', async () => {
    spyOn(store, 'dispatch').and.returnValue(of(true)); // Mock store.dispatch to return an observable of true

    let result: any;
    await store.dispatch(new MyAction()).pipe(
      ofActionSuccessful(MySuccessAction),
    ).toPromise().then((res) => result = res); // Trigger the action and use ofActionSuccessful operator

    expect(result).toEqual(new MySuccessAction()); // Assert that the result is the expected success action
  });
});