Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to test useState in jest

import { useState } from 'react';

export function useCounter(initial = 0) {
  const [count, setCount] = useState(initial);

  return [count, () => setCount(count + 1)];
}
Unit test
import { useCounter } from './Calculator';

const mockSetState = jest.fn();

jest.mock('react', () => ({
  useState: initial => [initial, mockSetState]
}));

test('Can increment from 1 to 2', () => {
  const [_, increment] = useCounter(1);

  increment();

  expect(mockSetState).toHaveBeenCalledWith(2);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: python json pandas Expected object or value 
Javascript :: regex on input 
Javascript :: get current date + 1 js 
Javascript :: loopback model count 
Javascript :: javascript remove all event listeners 
Javascript :: how to use absolute path in react 
Javascript :: how to right plain text format file in node js 
Javascript :: tailwind modal react 
Javascript :: utc to local time javascript 
Javascript :: change href with jquery 
Javascript :: redux logger 
Javascript :: componentdidupdate in hooks 
Javascript :: how to change the tab text in React 
Javascript :: jquery is check 
Javascript :: how to import background image in inline css in react 
Javascript :: Unexpected token a in JSON at position 
Javascript :: js date format mm/dd/yyyy 
Javascript :: javascript synchronous wait 
Javascript :: create module with routing by angular 
Javascript :: jest testmatch specific folder 
Javascript :: How to install react native hooks with npm 
Javascript :: how get checkbox if checked in jquery 
Javascript :: transitionduration js 
Javascript :: swiftyjson 
Javascript :: javascript redirect to route laravel 
Javascript :: materialize for react 
Javascript :: javascript date get future 15 minutes 
Javascript :: (intermediate value).getdate is not a function 
Javascript :: button size react native 
Javascript :: javascript change dataset value 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =