1
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?

More than 3 years have passed since last update.

jestでリクエスト途中の状態をチェックする

Posted at

例えばボタンが押されてからリクエストが完了するまで読み込み中になる場合

Button.js
//...
const onPress = async ()=> {
  setLoading(true)
  await fetch(...) // 何かしらの非同期処理
  setLoading(false)
}
Button.spec.js

describe('button', ()=> {
  it('assigns loading true till the request to complete', ()=> {
    // ...createやfetchをmockした後
    expect(button.props.loading).toEqual(false)
    let promise
    act(()=> {
      promise = button.props.onPress()
    }) // ボタンが押された処理を開始
    expect(button.props.visible).toEqual(true)
    await act(async()=> await promise) // リクエストを待機
    expect(button.props.visible).toEqual(false)
  })
})

と言った具合に、非同期な処理の途中状態をテストすることが出来ます。

1
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
1
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?