Skip to content

Add render test helper to follow suggested Redux testing practices#1392

Draft
zetter-rpf wants to merge 6 commits intomainfrom
cursor/redux-test-patterns-530b
Draft

Add render test helper to follow suggested Redux testing practices#1392
zetter-rpf wants to merge 6 commits intomainfrom
cursor/redux-test-patterns-530b

Conversation

@zetter-rpf
Copy link
Contributor

@zetter-rpf zetter-rpf commented Mar 18, 2026

In our tests we have a lot of boilerplate to set up redux state and providers in order to render components.

Redux suggests adding a renderWithProviders helper to manage this and make testing easier. See https://redux.js.org/usage/writing-tests

These changes add a renderWithProviders helper and changes a spec to use it.

If we're happy with this approach we can gradually move our existing tests over as we work on them.

One thing I wasn't sure about was naming of the helper and props - I've followed the example from redux, but it might be simpler and fit in with our existing code better if we call it render and initialState than preloadedState (however render will need to be renamed when imported if we want to mix styles in single test file).

cursoragent and others added 5 commits March 18, 2026 11:19
Extract a shared render helper for the mock Redux store and Provider wiring. This removes repetitive setup from each test case so scenarios focus on state differences instead of store plumbing, which aligns with Redux testing guidance to keep tests maintainable.

Co-authored-by: Chris Zetter <zetter-rpf@users.noreply.github.com>
Replace redux-mock-store with configureStore + root reducer so tests exercise real reducer behavior, matching Redux's recommended integration testing pattern. Assertions now verify observable store state changes instead of mocked dispatched-action arrays, which reduces false confidence and avoids mock-store deprecation warnings.

Co-authored-by: Chris Zetter <zetter-rpf@users.noreply.github.com>
Create a reusable Redux-aware render helper so component tests can share one Provider/store setup pattern. Updating SaveButton to use this keeps Redux test wiring consistent with the recommended real-store approach while reducing per-test boilerplate and making future migrations easier.

Co-authored-by: Chris Zetter <zetter-rpf@users.noreply.github.com>
Remove the local renderSaveButton wrapper and call renderWithProviders directly in each setup block. This keeps the test aligned to the shared utility API, avoids an extra indirection layer, and makes the common pattern easier to copy into other Redux-connected tests.

Co-authored-by: Chris Zetter <zetter-rpf@users.noreply.github.com>
Use preloadedState naming and object shorthand when calling renderWithProviders. This matches Redux Toolkit terminology and removes repetitive key:value syntax, making each setup block clearer and more consistent.

Co-authored-by: Chris Zetter <zetter-rpf@users.noreply.github.com>
@zetter-rpf zetter-rpf temporarily deployed to previews/1392/merge March 18, 2026 11:49 — with GitHub Actions Inactive
@cursor
Copy link

cursor bot commented Mar 18, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

Remove unused store bindings in scenarios that only verify rendered button classes. This resolves no-unused-vars warnings from yarn lint and keeps the setup focused on the behavior each test actually asserts.

Co-authored-by: Chris Zetter <zetter-rpf@users.noreply.github.com>
@zetter-rpf zetter-rpf changed the title Improve SaveButton Redux tests with modern store patterns Add render test helper to follow suggested Redux testing practices Mar 19, 2026
@zetter-rpf zetter-rpf marked this pull request as ready for review March 19, 2026 09:37
Copilot AI review requested due to automatic review settings March 19, 2026 09:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a shared renderWithProviders test helper to reduce Redux boilerplate in component tests, and migrates SaveButton tests to use a real RTK store instead of redux-mock-store.

Changes:

  • Introduce src/utils/renderWithProviders.js to create/configure a Redux store and wrap renders with react-redux’s <Provider>.
  • Update SaveButton.test.js to use the new helper and assert state changes via the real reducer/store.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/utils/renderWithProviders.js New RTL helper to render UI with a configured RTK store + Redux provider.
src/components/SaveButton/SaveButton.test.js Refactors tests to use renderWithProviders and validate save behavior against real store state.
Comments suppressed due to low confidence (1)

src/components/SaveButton/SaveButton.test.js:11

  • logInHandler is shared across all tests in this file and is never reset, so later assertions like expect(logInHandler).toHaveBeenCalled() can pass due to calls from earlier tests rather than the click under test. Clear the mock in a beforeEach/afterEach (or assert toHaveBeenCalledTimes(1) after clearing) to make these event tests independent and reliable.
const logInHandler = jest.fn();

describe("When project is loaded", () => {
  beforeAll(() => {
    document.addEventListener("editor-logIn", logInHandler);
  });

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

ui,
{ preloadedState, store, reducer = rootReducer, ...renderOptions } = {},
) => {
const testStore = store || configureStore({ reducer, preloadedState });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the redux example, it set up the apps root store instead of using configure store.

I want to look at this again to see if we can follow that pattern - this would mean that tests would use the same initial store as the app which might mean less setup

@zetter-rpf zetter-rpf marked this pull request as draft March 19, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants