question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fire event type is not updating the redux form store value with latest version of react-testing-library

See original GitHub issue
  • @testing-library/react version: 10.2.1
  • @testing-library/user-event version: 11.2.1
  • Testing Framework and version: Jest v24
  • DOM Environment:

Relevant code or config:

import React from "react";
import { Provider } from "react-redux";
import { combineReducers, createStore } from "redux";
import { reducer as reduxFormReducer, reduxForm, Field } from "redux-form";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

const RenderTextField = additionalProps => props => {
  const fabricProps = getFabricPropsFromReduxFormProps(props);
  return (
    <div>
      <TextField {...additionalProps} {...fabricProps} />
    </div>
  );
};
const getFabricPropsFromReduxFormProps = props => {
  const {
    input,
    id,
    type,
    rows,
    placeholder,
    label,
    autoComplete,
    required,
    disabled
  } = props;
  const { onBlur, ...inputRemaining } = input;
  const typedType = type;
  // wrapping onBlur without event object due to issue: https://github.com/erikras/redux-form/issues/2768
  return {
    ...inputRemaining,
    onBlur: () => onBlur(undefined),
    id,
    type: typedType,
    rows,
    placeholder,
    label,
    autoComplete,
    required,
    disabled
  };
};
test("RenderTextField sets initial value and responds to change", async () => {
  const title = "title-text";
  const component = RenderTextField({ title });
  const label = "label-text";
  const initialValue = "test";
  const formName = "foo";
  const fieldName = "bar";
  const onChange = jest.fn();
  const onBlur = jest.fn();
  const fieldProps = {
    autoComplete: "off",
    component,
    id: "id-text",
    key: "key-text",
    label,
    name: fieldName,
    onChange,
    onBlur,
    placeholder: "placeholder-text",
    required: true,
    rows: 2,
    type: "text"
  };
  const TestForm = reduxForm({
    form: formName,
    initialValues: { [fieldName]: initialValue }
  })(() => <Field {...fieldProps} />);
  const store = createStore(combineReducers({ form: reduxFormReducer }), {});
  const result = render(
    <Provider store={store}>
      <TestForm />
    </Provider>
  );
  // expect(result.container).toMatchSnapshot();
  expect(store.getState()).toEqual({
    form: {
      [formName]: expect.objectContaining({
        values: { [fieldName]: initialValue }
      })
    }
  });
  const input = result.getByLabelText(label);
  const newValue = "newvalue";
  userEvent.clear(input);
  await userEvent.type(input, newValue, { allAtOnce: true });
  expect(onChange).toHaveBeenCalledTimes(2);
  expect(store.getState()).toEqual({
    form: {
      [formName]: expect.objectContaining({ values: { [fieldName]: newValue } })
    }
  });
});

What you did:

Unit testing redux form field change event

What happened:

Redux form field value is not updated in store

Reproduction:

Issue is happening in the below code https://codesandbox.io/s/basic-react-testing-library-w-setuptestsjs-skclz?file=/src/Sample.test.js:0-2582

Issue is not happening with the older versions of libraries https://codesandbox.io/s/basic-react-testing-library-w-setuptestsjs-u3lx8?file=/src/Sample.test.js

Problem description:

Store is not updated with the new value

Please help me what could be wrong here.

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
nanivijaycommented, Jun 9, 2020

@kentcdodds issue is still happening with the latest version of user-event library. here is the codesandbox url https://codesandbox.io/s/basic-react-testing-library-w-setuptestsjs-skclz?file=/src/Sample.test.js

1reaction
kentcdoddscommented, Jun 8, 2020

Hi @nanivijay,

I’m pretty sure this is related to this issue: https://github.com/testing-library/user-event/issues/316

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fire event type is not updating the redux form store value with ...
Fire event type is not updating the redux form store value with latest version of react-testing-library #639 · Comments · Footer.
Read more >
Input not updating on react testing library, thus test failing ...
When ome types on inputA, an action is dispatched and then inputB gets a new value from the redux state. This is my...
Read more >
React Testing Library and the “not wrapped in act” Errors
I recently upgraded React and React testing library. ... So in most cases, we do not need to wrap render and fireEvent in...
Read more >
API | Testing Library
React Testing Library re-exports everything from DOM Testing Library as well as these methods: render; render Options.
Read more >
Writing Tests | Redux
For a React app using Redux, render a <Provider> with a real store instance wrapping the components being tested. Interactions with the page ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found