Ask Your Question
3

What is the method for mocking ofActionSuccessful in ngxs?

asked 2023-06-21 07:26:58 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-21 07:38:02 +0000

nofretete gravatar image

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
  });
});
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-21 07:26:58 +0000

Seen: 9 times

Last updated: Jun 21 '23