Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: waitForElementToBeRemoved incorrect promise return type #610

Closed
mdjastrzebski opened this issue Jun 8, 2020 · 4 comments
Closed
Labels

Comments

@mdjastrzebski
Copy link

mdjastrzebski commented Jun 8, 2020

  • @testing-library/dom version: v7.10.1
  • Testing Framework and version: N/A
  • DOM Environment: N/A

Relevant code or config:

N/A

What you did:

Read docs/types/code for waitForElementToBeRemoved.

What happened:

Found discrepancy between declared return type for waitForElementToBeRemoved (docs & typescript types state Primise<T>) and actual implementation (Promise<true>).

Reproduction:

Compare docs or typescript types with waitForElementToBeRemoved implementation.

Problem description:

Documentation, states the promise return type is T.

With following declaration:

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

The same is declared in typescript types:

export function waitForElementToBeRemoved<T>(
    callback: (() => T) | T,
    options?: waitForOptions,
): Promise<T>;

However waitForElementToBeRemoved implementation returns however true instead of T.

Additionally, the same above docs later states that "Here is an example where the promise resolves with true because the element is removed:"

Suggested solution:

Make documentation and code agree with each other. For which I see 3 available options:

  1. Updated the docs & typescript types so that Promise<Boolean> or Promise<true> is used.
  2. Update the code, so that instead of returning true, the code returns the initial value of callback evaluation. This seems feasible, since in the implementation, callback is immediately called in order to check that elements are initially present & and only then subsequently removed. We might capture that initial value, and return it to the caller. Hence resolving the promise with T indeed.
  3. Change both code and docs so that promise without meaningful values is returned like Promise<null>. The advantage of this options is that it avoids using Boolean type where the return value is always true hence does not add meaning.
tjefferson08 added a commit to tjefferson08/dom-testing-library that referenced this issue Jun 13, 2020
There's a couple options here, as issue testing-library#610 enumerates nicely.

1) Resolve to the type param T

Keeping references to removed elements is a probable source of bugs and
confusing tests. A reference should be easy enough to obtain with other
test utilities; I submit that there's no added value in providing it
here.

2) Resolve to boolean (or literal `true`)

Not a bad choice by any means, but not particularly meaningful (the
fulfillment of the promise is signal enough IMO)

3) Resolve to something empty
This is what I've gone with. We are _literally_ waiting for removal,
after all :)

Fixes testing-library#610
tjefferson08 added a commit to tjefferson08/dom-testing-library that referenced this issue Jun 13, 2020
There's a couple options here, as issue testing-library#610 enumerates nicely.

1) Resolve to the type param T

Keeping references to removed elements is a probable source of bugs and
confusing tests. A reference should be easy enough to obtain with other
test utilities; I submit that there's no added value in providing it
here.

2) Resolve to boolean (or literal `true`)

Not a bad choice by any means, but not particularly meaningful (the
fulfillment of the promise is signal enough IMO)

3) Resolve to something empty
This is what I've gone with. We are _literally_ waiting for removal,
after all :)

Fixes testing-library#610
@tjefferson08
Copy link
Contributor

Hey @mdjastrzebski, I noticed this little hiccup as well!

I jumped the gun and gave a PR a shot, maybe have a look if you're interested ❤️

@kentcdodds
Copy link
Member

@all-contributors please add @mdjastrzebski for bugs

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @mdjastrzebski! 🎉

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 7.14.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants