score:0
localstorage.getitem
should be spied before expected call:
jest.spyon(localstorage, 'getitem');
and asserted that it was called afterwards:
expect(localstorage.getitem).tobecalledwith('access_token');
score:0
put this in your test initializer files:
const localstoragemock = {
getitem: jest.fn(),
setitem: jest.fn(),
clear: jest.fn()
};
global.localstorage = localstoragemock;
so now you can just use localstoragemock
globally
expect(localstoragemock.getitem).tohavebeencalledwith('access_token')
the only gotcha is to make sure that your tests have jest.clearallmocks()
cleanup step so the mock is reinitialized for other tests.
score:1
you can use like this:-
const result = '{ "name":"john", "age":30, "car":"bmw"}';
//set json object to storage
localstorage.setitem('user', json.stringify(result));
//get object
const value = localstorage.getitem('user');
//remove object
localstorage.removeitem('user');
Source: stackoverflow.com
Related Query
- How to unit test the output of a React component method in a React Router / Redux app with Enzyme
- How to test localStorage in my react app?
- React app testing, how to test an axios post request with Jest?
- how do you deal with localstorage with tests in create react app withour ejecting?
- How to test React APP in development on an actual phone?
- How do i unit test a react app that has all the text fields in the children components?
- How to create a React app directly in the current folder
- How to test a className with the Jest and React testing library
- How to show a loading indicator in React Redux app while fetching the data?
- How to create multiple page app using react
- How to test a react component that is dependent on useContext hook?
- How to unit test a method of react component?
- How to deploy a React App on Apache web server
- How to detect when a React Native app is closed (not suspended)?
- How to test style for a React component attribute with Enzyme
- How to make React app preview on mobile?
- How to test a prop update on React component
- Figuring out how to mock the window size changing for a react component test
- How to bundle a React app to a subdirectory on a server?
- How to test React Native Module?
- How to test React PropTypes through Jest?
- React & Jest, how to test changing state and checking for another component
- How to test material ui autocomplete with react testing library
- How to test a React component with RouteComponentProps?
- How to detect the device on React SSR App with Next.js?
- How can I test a change handler for a file-type input in React using Jest/Enzyme?
- How to write test case for ErrorBoundary in React using Jest / Enzyme
- How to know if react-router can go back to display back button in react app
- How to "mock" navigator.geolocation in a React Jest Test
- How to not show warnings in Create React App
More Query from same tag
- How can i convert this JSON object into this format?
- Add item to the list in React
- confusion between props and state in react.js
- Webpack Uncaught TypeError: Cannot read property 'call' of undefined
- CSS Modules Value is not retrieved
- How to display a ref variable without using state in React?
- setInterval in React - unexpected behavior
- How to transpile React ES6 classes using Grunt + Browserify + Reactify?
- I am importing Bootstrap in my project but it isn't working
- how to get an array into another array with a button in react
- useState not updating displays on first click
- Logo not showing in AppBar (Material UI / React)
- How to pass all props in one variable in React?
- "The CSRF session token is missing" when CSRF token is present in request
- Cypress delete testing data after testing
- How to sort inbound props in a ReactJS component?
- Google Places autocomplete not working (in Bootstrap modal)
- How to load a file on component mounting using React w/ raw-loader
- ReactJs redux : How to call a function from the render function if the prop value change?
- React routes dosen't show my components. Only one component is shown, others are not
- How do I play audio from an array of objects in React.js
- Dynamic data showing undefined
- onEnter/onExit method in React Native Component (react-native-router-flux)
- how to get code coverage in react with react testing library
- Prefer inline event handler to avoid indirection in code to improve human readability or use handler function?
- How to prevent rows adjustment after they are expanded?
- How to access a function in a child class component from a stateless parent?
- Switch focus to table after adding an element
- What is the easiest way to show a confirmation dialog in react-admin?
- Return boolean value from Material UI Radio to use in Yup .when Validation