Skip to content

Instantly share code, notes, and snippets.

@Nelfimov
Created February 8, 2024 11:51
Show Gist options
  • Save Nelfimov/ade5263ea89d82a240368fe7aa8c09ba to your computer and use it in GitHub Desktop.
Save Nelfimov/ade5263ea89d82a240368fe7aa8c09ba to your computer and use it in GitHub Desktop.
/**
* @jest-environment jsdom
*/
import { act } from '@testing-library/react'
import { renderHook } from '@testing-library/react'
import { useFocus } from './use-focus.hook'
describe('Use focus hook', () => {
it('should change focus', () => {
const { result } = renderHook(() => useFocus())
act(() => result.current.focusProps.onFocus())
expect(result.current.focus).toBeTruthy()
act(() => result.current.focusProps.onBlur())
expect(result.current.focus).toBeFalsy()
})
})
@Nelfimov
Copy link
Author

Nelfimov commented Feb 8, 2024

import { useState } from 'react'

export const useFocus = () => {
  const [focus, setFocus] = useState<boolean>(false)

  const focusProps = {
    onFocus: () => setFocus(true),
    onBlur: () => setFocus(false),
  }

  return { focus, focusProps }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment