Search code examples
angularunit-testingjasminengrxngrx-store

How to test NgRx selector which uses props?


I have the following selector:

export const selectTableById = createSelector(selectRestaurantState, (state: RestaurantState, props) =>
  state.tables.find((table) => table.id === props.id)
);

Which uses the props to add additional information to the selctor when calling it.

In unit test i am trying something like this:

  it("should ...", () => {
    expect(selectTableById.projector(state, { props: { id: "id" } })).toEqual(...);
  });

but i have not found info about how to call the projector function with props


Solution

  • Found it:

    const tables = ...
    const state = <RestaurantState>{ tables };
    
    it("should select table matched by id from props", () => {
        const props = { id: "t2" };
        expect(selectTableById.projector(state, props)).toEqual(tables[1]);
    });
    

    My problem was that, i added the wrong state (the whole appstate rather than just the restaurant state) to the projector funciton, and the props was badly formatted.