NashTech Insights

Testing Actions in NGRX Store

Alka Vats
Alka Vats
Table of Contents


In Angular applications that utilize the NGRX Store for state management, testing actions is a critical aspect of ensuring the correctness and reliability of state transitions. Actions represent intentions to change the state and are dispatched to reducers for processing. In this blog post, we will dive deeper into testing actions in the NGRX Store, covering advanced concepts and providing detailed examples to illustrate the process.

If you want to learn about testing reducers in NGRX Store you can refer here.

Understanding Actions in NGRX Store:

Actions in the NGRX Store are simple objects that describe an intention to change the state. They typically consist of a type property that identifies the action and an optional payload property that carries additional data. Testing actions involves verifying their proper definition and the accuracy of dispatched actions.

Setting up the Testing Environment:

Before we begin testing actions, let’s ensure we have the necessary dependencies and configurations in place.

a) Install the required dependencies:
npm install @ngrx/store-testing jasmine
b) Create a testing module:

Create a testing module that imports the required dependencies and provides any additional dependencies needed for testing.

c) Configure the TestBed:

In your test setup, configure the TestBed to utilize the testing module you created earlier.

Writing Tests for Actions:

To thoroughly test actions in the NGRX Store, we need to focus on two essential aspects: validating the action definitions and testing the action dispatching.

a) Testing Action Definitions:

To ensure the correctness of action definitions, we need to test the types and payload structures. This helps identify any errors or inconsistencies in the action definition.

Example Test for Action Definitions:

import { MyAction, MyActionTypes } from './my.actions';

describe('My Actions', () => {
  it('should create MyAction with payload', () => {
    const payload = 'Test Payload';
    const action = new MyAction(payload);

In this example, we import the action class MyAction and its associated action types from the my.actions file. We create an instance of the action with a payload and assert that the action type matches the expected value.

b) Testing Action Dispatching:

To test the dispatching of actions, we need to simulate the action dispatching process and ensure that the dispatched actions are accurate.

Example Test for Action Dispatching:

import { Store } from '@ngrx/store';
import { MyAction } from './my.actions';

describe('My Component', () => {
  let store: Store;

  beforeEach(() => {
    store = TestBed.inject(Store);
    spyOn(store, 'dispatch');

  it('should dispatch MyAction', () => {
    const payload = 'Test Payload';
    const action = new MyAction(payload);

    // Trigger the action dispatch


In this example, we set up the test by injecting the Store and creating a spy on the dispatch method. We create an instance of the action (MyAction) with a payload and trigger the action dispatch in the component’s method (dispatchAction()). Finally, we assert that the store.dispatch method was called with the expected action.

Running Action Tests:

To execute the action tests, run the test command in your Angular project:

ng test

Ensure that the action tests are included in the test suite and that they pass successfully. This validates the correctness of your action definitions and guarantees the accurate dispatching of actions.


Testing actions in the NGRX Store is crucial for ensuring the correctness and reliability of state transitions in Angular applications. By following the steps outlined in this blog post and incorporating the detailed examples, you can effectively test your actions and ensure their proper definition and dispatching. Thoroughly tested actions contribute to the robustness and maintainability of your application’s state management. Implementing a comprehensive testing strategy for actions is an essential practice in delivering high-quality software and an exceptional user experience.

Finally, for more such posts, please follow our LinkedIn page- FrontEnd Competency.

Alka Vats

Alka Vats

Alka Vats is a Software Consultant at Nashtech. She is passionate about web development. She is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. She is a quick learner & curious to learn new technologies. Her hobbies include reading books, watching movies, and traveling.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: