Skip to content

BREAKING: make waitForDomChange accept a callback #376

Closed
@kentcdodds

Description

@kentcdodds

Just an idea. When I use wait, I give it an assertion to wait for something to pass and I know my callback is called every 50ms until it passes. The nice thing about this is that we don't care what goes on during that time. We just wait for that assertion to pass.

However with waitForDomChange, we're just waiting for a single update to the DOM, but that's less helpful and arguably a bit more like implementation details because we shouldn't care how many DOM changes there are, we should wait until the DOM changes to a desirable state.

I'd like to think of waitForDomChange as a variant to wait which re-runs a callback whenever the DOM changes rather than arbitrarily every 50ms.

So it'd look like this (the same as waitForElement):

function waitForDomChange<T>(
  callback: () => T,
  options?: {
    container?: HTMLElement
    timeout?: number
    mutationObserverOptions?: MutationObserverInit
  }
): Promise<T>

I expect that it would work similar to wait if you provide no callback. So this would be a breaking change only for people who are passing options (they would have to pass a callback of undefined ahead of the options, or even better, an actual callback making an actual useful assertion).

So this change would not break existing code that does this:

await waitForDomChange()

But it would break existing code that does this:

await waitForDomChange({container: el})

And that would need to change to:

await waitForDomChange(undefined, {container: el})

For the exact same behavior as before, or, preferably someone would give a callback with an assertion:

await waitForDomChange(() => expect(getByText(/status/i).toHaveTextContent('finished'), {container: el})

I think this is a good time to make this change considering we're about to release another breaking change: #373

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions