0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TypeScriptのgetByText()について

Posted at

getByText()とは

Testing Libraryの一部であり、ReactのUIコンポーネントをテストする際に テキスト要素を取得する関数 のことです。

getByText()の使用方法

/** * @jest-environment */

import renderer from 'react-test-renderer'
import { render, fireEvent } from '@testing-library/react'
import { Button } from './Button' 

describe('Button', () => {
  it('changes the button text upon clicking the button using React Testing Library', () => {
    const button = render(<Button />)
    fireEvent.click(button.getByText('ON'))
    expect(button.getByText(/OFF/i)).toBeTruthy()
    fireEvent.click(button.getByText('OFF'))
    expect(button.getByText(/ON/i)).toBeTruthy()
  })
})

感想

よく使う関数だと思うので、すぐに用法が浮かぶようにします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?