Skip to content

Testing in-progress state after user-event #911

Answered by ph-fritsche
rvsia asked this question in Q&A
Discussion options

You must be logged in to vote

The Promise returned by .click() resolves when the library is done simulating the interaction.
The asynchronous request is not resolved instantly, just before this is finished.

One way to test such things is to await the state you want to test on:

// Get a Promise and the callback to resolve it
let resolveWaitForLoadingState = () => {}
const waitForLoadingState = new Promise(r => { resolveWaitForLoadingState = r })

// Resolve the Promise when the mock puts your component in the state to be observed
reloadData = jest.fn(() => {
  resolveWaitForLoadingState()
  return data
})

// Start the click simulation and wait for the right moment to run your assertion
await Promise.all([
  user.click(e…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@rvsia
Comment options

@ph-fritsche
Comment options

@rvsia
Comment options

@ph-fritsche
Comment options

@rvsia
Comment options

Answer selected by rvsia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants