Is it enough to only test if ngrx action was called? (unit test)

Viewed 737

Let say i have the following code in a component, which is public and used in the view:

removeItemFromCart(item: CartItem): void {
  this.store$.dispatch(OrderingStoreActions.removeItemFromCart({ cartItem: item }));
}

In the unit test, is it enough to only test that the action was dispatched?

it("should remove the item from the cart", () => {
    const item = <CartItem>{ amount: 1, product: { id: "p" } };
    component.removeItemFromCart(item);
    expect(store.dispatch).toHaveBeenCalledWith(OrderingStoreActions.removeItemFromCart({ cartItem: item  }));
});

Or should i test in the mockstore if the action result was executed on the feature store?

1 Answers

You should unit-test that your code uses the given API/library correctly, not that that API/library works (that would be an integration test). So, you need to confirm that the action is dispatched with the parameters you intended to send to it. The test you have shown here is sufficient.

Related