08 / 12

How do you test actions generated by a slice?

Testing Actions Generated by createSlice in Redux Toolkit

Actions generated by createSlice() in Redux Toolkit can be easily tested since they are plain JavaScript objects. You can test them by calling the action creator and verifying that it returns the correct action type and payload.

Steps to Test Slice Actions
  1. 1

    Import the auto-generated action creator from the slice file.

  2. 2

    Call the action creator with the desired payload.

  3. 3

    Check that the returned object matches the expected type and payload structure.

Example: Testing Slice Actions

Since createSlice() automatically generates action creators, these functions can be tested like any other pure JavaScript function — without needing to mock the Redux store.