Not update properties when useState() is mocked

Viewed 20

I have const in my JS app

const [props, setProps] = useState<TreeProps>({
  inputValue: '',
  disabled: false,
  open: false,
  expanded: []
});

and mock the implementation of useState() in my tests

const useStateMock: any = (prevState: any) => {
  const obj = [ { ...prevState },
    (nextState: any) => {
      obj[0] = { ...nextState, expanded: ['Value_1', 'Value_2'] }
  }];
  return obj;
}

jest.spyOn(React, 'useState').mockImplementation(useStateMock);

Why is not property updated after the following lines in my component?

{
   console.log(props); 
   setProps({inputValue: 'Stackoverflow'});
   console.log(props.inputValue); //equals '', expected 'Stackoverflow'
   console.log(props.expanded);   //equals [], expected ['Value_1', 'Value_2']       
}

The defined type in useState() is used to show/hide components. I'd like to influence on behavior a single component by changing only 'expanded' property.

Thanks in advance.

0 Answers
Related