Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
userEvent.type from react testing library not updating value of input in material UI TextField with default values

I have a material UI TextField element that keeps track of the latitude. The latitude should only be between -90 and 90 degrees. I have a unit test to enforce this, but when I try to update the value in the TextField element using userEvent.type, it is not currently updating. When I debug, the value for latitude is "1" the same as what I'm passing in with the entity.

The element I want to change:

<div>
  <TextField
  id="editor-latitude"
  label={"Latitude"}
  defaultValue={latitude}
  error={latitudeError !== ""}
  helperText={latitudeError}
  variant="standard"
  onChange={(e) => setLatitude(e.target.value)}
  InputProps={{
    endAdornment: 
    <InputAdornment position="end">degrees</InputAdornment>
    }}
    inputProps={{ "aria-label": "Latitude" }}
      data-testId="editor-latitude"
     />
</div>

The unit test:

describe("Test", () => {
const entity: MapEntity = {
        id: "1",
        latitude: 1,
    };
test.each([
            ["-91", true],
            ["-90", false],
            ["0", false],
            ["90", false],
            ["91", true],
            ["abc", true]
        ])("Latitude values test", async (val: string, error: boolean) => {
            const { queryByLabelText, queryByTestId } = render(<EntityEditor entities={[entity]} />);
            expect(queryByLabelText("edit entity")).toBeInTheDocument();
            queryByLabelText("edit entity")!.click();
            await waitFor(() => expect(queryByTestId("editor-dialog")).toBeInTheDocument());
            const latitude = queryByLabelText("Latitude");
            userEvent.type(latitude!, val);
            expect(latitude?.outerHTML.includes('aria-invalid="false"')).toBe(true);
            queryByLabelText("save")!.click();
            expect(latitude?.outerHTML.includes('aria-invalid="true"')).toBe(error);
        });

})

Things I've tried:

  • changing userEvent.type to fireEvent.change
  • clearing the latitude container first
  • Wrapping user.type in a waitFor

Any help is appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The issue was that I forgot to add value={latitude} in the TextField component. After adding that, I needed to call userEvent.clear(latitude) in the unit test then the userEvent.type worked fine.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!