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

122
Views
How to update mock values state in Jest React Context

I need guidance on how to test and change react context mock values with jest, What I have done is created context which has two properties inputs and setInputs with my test the initial test are passing with the mock state of the inputs, where I hit a wall is how to change the values of the valuesValidate objects from property valid from mock state false to become true and test that there is change. I am just starting whit jest tesing so I would Appreciate any comments and help. I want to simulate a typing event that will change the values in the mocked stated for the valiedated properties to become from false to true. This is my test

  it("user types on input fields", () => {
    const mockState = {
      initial: {
        email: "some@email.com",
        password: "1234567asd",
        secretQuestionA: "some question",
        secretQuestionB: "some question",
      },
      valuesValidate: {
        email: { valid: false, errorMessage: "" },
        password: { valid: false, errorMessage: "" },
        secretQuestionA: { valid: false, errorMessage: "" },
        secretQuestionB: { valid: false, errorMessage: "" },
      },
    };

    const setInputs = jest.fn();
    const inputs: InputsInitiaState = mockState;
    const storeValues: IAccountDetailsContextProps = {
      inputs,
      setInputs,
    };

    render(
      <AccountDetalsContext.Provider value={storeValues}>
        <AccountDetails />
      </AccountDetalsContext.Provider>
    );
    const emailInput = screen.getByTestId("email");
    const passwordInput = screen.getByTestId("password");
    const securityQuestionAInput = screen.getByTestId("security-question-1");
    const securityQuestionBInput = screen.getByTestId("security-question-2");

    const button = screen.getByTestId("continue-button");

    user.click(button);

    expect(emailInput).toHaveValue("some@email.com");
    expect(passwordInput).toHaveValue("1234567asd");
    expect(securityQuestionAInput).toHaveValue("some question");
    expect(securityQuestionBInput).toHaveValue("some question");
    expect(button).toBeInTheDocument();

  });
about 4 years ago · Juan Pablo Isaza
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!