mercredi 13 mai 2020

Get and Set localStorage data using enzyme in React

I am saving few data at login page in local storage. That i am suppose to show in Header. So while writing test suite for header, i thought of adding some local storage data there. But Below code is not working out for it.

export default class HeaderComponent extends Component {

   render() {
      const data = JSON.parse(localStorage.getItem('tokenized'));
      return(
         <div id="ui_8">{data}</div>
   )}

}

test case on this Header comp

let wrapper = shallow(<HeaderComponent />);
test('Display user data', () => {
    window.localStorage.setItem('tokenized', 'User_787');
    expect(wrapper.find('#ui_8').text()).toBe('User_787'); 
})

Aucun commentaire:

Enregistrer un commentaire