Search⌘ K
AI Features

Testing Selectors

Discover how to write unit tests for NgRx selectors in Angular by creating spec files, using the projector function, and defining mock states to verify selector accuracy. This lesson helps you ensure your selectors correctly retrieve state slices without relying on server data.

Introduction

In our application’s Products module, we have defined the getProductsSelector in the src/app/products/state/products.selectors.ts file. This selector retrieves the entire products slice from the NgRx store.

TypeScript 3.3.4
import { createFeatureSelector } from "@ngrx/store";
import { ProductsState } from "./products.interfaces";
export const getProductsSelector = createFeatureSelector<ProductsState>('products');
Selectors defined in the Products module

In this lesson, let’s write a test case to verify that our getProductsSelector is properly retrieving the products slice.

Unit testing the NgRx selectors

We can unit test the NgRx ...