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

Wait for element to be removed #218

Merged
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f3eca1c
Add tests and snapshots
Tolsee Feb 22, 2019
c547962
Implement waitForElementToBeRemoved method
Tolsee Feb 22, 2019
cc21522
Export waitForElementToBeRemoved
Tolsee Feb 22, 2019
c75e492
Add as contributor
Tolsee Feb 22, 2019
a349725
Update README.md after rebase
Tolsee Feb 22, 2019
d1d303d
Throw error is the element is not present in the first place
Tolsee Feb 23, 2019
39fea0e
Add test for full coverage
Tolsee Feb 23, 2019
36c4e4c
User jest time faker
Tolsee Feb 24, 2019
f5b081d
Add typings
Tolsee Feb 24, 2019
2dedb1f
Cleanup
Tolsee Feb 24, 2019
54cbc89
Update snapshot
Tolsee Feb 24, 2019
999ff5c
Get rid of iife
Tolsee Feb 25, 2019
e7a018e
Cleanup from review
Tolsee Feb 26, 2019
0518e78
Check for empty array as well
Tolsee Feb 26, 2019
d6150af
Check for error, falsy and empty to check whether the element is removed
Tolsee Feb 26, 2019
4e2f81c
error not defined error fix
Tolsee Feb 27, 2019
20ada81
Change snapshot to inline snapshot
Tolsee Feb 27, 2019
46a952f
Update snapshot
Tolsee Feb 27, 2019
618c240
Merge branch 'ts-wait-for-element-to-be-removed' of https://github.co…
Tolsee Feb 27, 2019
af327d1
Merge branch 'master' into ts-wait-for-element-to-be-removed
Tolsee Feb 27, 2019
505d783
Only observe mutations if synchronous test passes
Tolsee Feb 27, 2019
6fcfc83
only observer if synchronous test passes fix
Tolsee Feb 27, 2019
579c876
Await for the respective promise to resolve in test
Tolsee Feb 27, 2019
2f7452d
Snapshot test to normal toHaveBeenCalledWith assertions
Tolsee Feb 28, 2019
20cd110
Update comment
Tolsee Feb 28, 2019
bc2e3f2
Remove comment
Tolsee Feb 28, 2019
9834de2
test: improve tests for wait-for-element-to-be-removed
Mar 9, 2019
17f136f
test: fix everything
Mar 9, 2019
6664f0a
test: do not test unstable node versions
Mar 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change snapshot to inline snapshot
  • Loading branch information
Tolsee committed Feb 27, 2019
commit 20ada81c2bb1b7cabf943db8934f6293d9d441ed
58 changes: 47 additions & 11 deletions src/__tests__/wait-for-element-to-be-removed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const skipSomeTimeForMutationObserver = (delayMs = 50) => {
jest.runAllImmediates()
}

test('it waits for the callback to throw error or a falsy value and only reacts to DOM mutations', async () => {
const {container, getByTestId} = render(
test('it waits for the callback to throw error, a falsy value or empty array and only reacts to DOM mutations', async () => {
const {container, getByTestId, queryAllByTestId} = render(
`<div data-testid="initial-element">
</div>`,
)
Expand All @@ -35,6 +35,10 @@ test('it waits for the callback to throw error or a falsy value and only reacts
const mutationsAndCallbacks = [
[makeMutationFn(), () => true],
[makeMutationFn(), () => getByTestId('the-element-we-are-looking-for')],
[
makeMutationFn(),
() => queryAllByTestId('the-element-we-are-looking-for'),
],
[
() =>
container.removeChild(getByTestId('the-element-we-are-looking-for')),
Expand Down Expand Up @@ -80,7 +84,11 @@ test('it waits for the callback to throw error or a falsy value and only reacts

expect(callback).toHaveBeenCalledTimes(1 + mutationsAndCallbacks.length)
expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler.mock.calls[0]).toMatchSnapshot()
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(`
Array [
true,
]
`)
expect(errorHandler).toHaveBeenCalledTimes(0)
})

Expand Down Expand Up @@ -113,7 +121,11 @@ test('it waits characterData mutation', async () => {
await promise

expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler.mock.calls[0]).toMatchSnapshot()
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this couldn't be simplified to:

expect(successHandler).toHaveBeenCalledWith(true)

Or you could also replace this line and the one above with:

expect(successHandler.mock.calls).toEqual([true])

I don't really care either way, but using snapshots for this doesn't make any sense to me personally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, make sense. I am a little 🆕 to jest that is the only reason.

Array [
true,
]
`)
expect(errorHandler).toHaveBeenCalledTimes(0)
expect(callback).toHaveBeenCalledTimes(2)
})
Expand Down Expand Up @@ -148,7 +160,11 @@ test('it waits for the attributes mutation', async () => {

expect(callback).toHaveBeenCalledTimes(2)
expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler.mock.calls[0]).toMatchSnapshot()
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(`
Array [
true,
]
`)
expect(errorHandler).toHaveBeenCalledTimes(0)
})

Expand Down Expand Up @@ -183,10 +199,14 @@ test('it throws if timeout is exceeded', async () => {
expect(callback).toHaveBeenCalledTimes(3)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(1)
expect(errorHandler.mock.calls[0]).toMatchSnapshot()
expect(errorHandler.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[Error: Timed out in waitForElementToBeRemoved.],
]
`)
})

test('it returns error immediately if there callback returns falsy value or error before any mutations', async () => {
test('it returns error immediately if there callback returns falsy value, empty array or error before any mutations', async () => {
const {container, getByTestId, queryByTestId, queryAllByTestId} = render(``)

const callbackForError = jest
Expand Down Expand Up @@ -235,8 +255,16 @@ test('it returns error immediately if there callback returns falsy value or erro
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(3)
expect(errorHandler.mock.calls[0]).toMatchSnapshot()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm fine with this being a snapshot, but please make it inline.

expect(errorHandler.mock.calls[1]).toMatchSnapshot()
expect(errorHandler.mock.calls[3]).toMatchSnapshot()
expect(errorHandler.mock.calls[1]).toMatchInlineSnapshot(`
Array [
[Error: The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal.],
]
`)
expect(errorHandler.mock.calls[2]).toMatchInlineSnapshot(`
Array [
[Error: The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal.],
]
`)
})

test('works if a container is not defined', async () => {
Expand Down Expand Up @@ -275,7 +303,11 @@ test('works if a container is not defined', async () => {

expect(callback).toHaveBeenCalledTimes(2)
expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler.mock.calls[0]).toMatchSnapshot()
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(`
Array [
true,
]
`)
expect(errorHandler).toHaveBeenCalledTimes(0)

document.getElementsByTagName('html')[0].innerHTML = '' // cleans the document
Expand All @@ -299,6 +331,10 @@ test('throws an error if callback is not a function', async () => {
await promise

expect(errorHandler).toHaveBeenCalledTimes(1)
expect(errorHandler.mock.calls[0]).toMatchSnapshot()
expect(errorHandler.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[Error: waitForElementToBeRemoved requires a function as the first parameter],
]
`)
expect(successHandler).toHaveBeenCalledTimes(0)
})