简体   繁体   English

如何测试使用道具的 NgRx 选择器?

[英]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但我还没有找到有关如何使用道具调用投影仪 function 的信息

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.我的问题是,我在投影仪功能中添加了错误的 state(整个 appstate 而不仅仅是餐厅状态),并且道具格式错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM