-
Notifications
You must be signed in to change notification settings - Fork 276
feature: wait for element to be removed #376
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc20169
feature: basic implementation
mdjastrzebski a5c439d
feature: basic implementation
mdjastrzebski e5f6d40
feature: added typescript types
mdjastrzebski acd9937
feature: check if element exists initially
mdjastrzebski 8277254
resolve promise with initial elements
mdjastrzebski c4d6c81
docs: README & API
mdjastrzebski 1143370
use ErrorWithStack helper
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // @flow | ||
| import React, { useState } from 'react'; | ||
| import { View, Text, TouchableOpacity } from 'react-native'; | ||
| import { render, fireEvent, waitForElementToBeRemoved } from '..'; | ||
|
|
||
| const TestSetup = () => { | ||
| const [isAdded, setIsAdded] = useState(true); | ||
|
|
||
| const removeElement = async () => { | ||
| setTimeout(() => setIsAdded(false), 300); | ||
| }; | ||
|
|
||
| return ( | ||
| <View> | ||
| {isAdded && <Text>Observed Element</Text>} | ||
|
|
||
| <TouchableOpacity onPress={removeElement}> | ||
| <Text>Remove Element</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| test('waits when using getBy query', async () => { | ||
| const screen = render(<TestSetup />); | ||
|
|
||
| fireEvent.press(screen.getByText('Remove Element')); | ||
| expect(screen.getByText('Observed Element')).toBeTruthy(); | ||
|
|
||
| await waitForElementToBeRemoved(() => screen.getByText('Observed Element')); | ||
| expect(screen.queryByText('Observed Element')).toBeNull(); | ||
| }); | ||
|
|
||
| test('waits when using getAllBy query', async () => { | ||
| const screen = render(<TestSetup />); | ||
|
|
||
| fireEvent.press(screen.getByText('Remove Element')); | ||
| expect(screen.getByText('Observed Element')).toBeTruthy(); | ||
|
|
||
| await waitForElementToBeRemoved(() => | ||
| screen.getAllByText('Observed Element') | ||
| ); | ||
| expect(screen.queryByText('Observed Element')).toBeNull(); | ||
| }); | ||
|
|
||
| test('waits when using queryBy query', async () => { | ||
| const screen = render(<TestSetup />); | ||
|
|
||
| fireEvent.press(screen.getByText('Remove Element')); | ||
| expect(screen.getByText('Observed Element')).toBeTruthy(); | ||
|
|
||
| await waitForElementToBeRemoved(() => screen.queryByText('Observed Element')); | ||
| expect(screen.queryByText('Observed Element')).toBeNull(); | ||
| }); | ||
|
|
||
| test('waits when using queryAllBy query', async () => { | ||
| const screen = render(<TestSetup />); | ||
|
|
||
| fireEvent.press(screen.getByText('Remove Element')); | ||
| expect(screen.getByText('Observed Element')).toBeTruthy(); | ||
|
|
||
| await waitForElementToBeRemoved(() => | ||
| screen.queryAllByText('Observed Element') | ||
| ); | ||
| expect(screen.queryByText('Observed Element')).toBeNull(); | ||
| }); | ||
|
|
||
| test('waits until timeout', async () => { | ||
| const screen = render(<TestSetup />); | ||
|
|
||
| fireEvent.press(screen.getByText('Remove Element')); | ||
| expect(screen.getByText('Observed Element')).toBeTruthy(); | ||
|
|
||
| await expect( | ||
| waitForElementToBeRemoved(() => screen.getByText('Observed Element'), { | ||
| timeout: 100, | ||
| }) | ||
| ).rejects.toThrow('Timed out in waitForElementToBeRemoved.'); | ||
|
|
||
| // Async action ends after 300ms and we only waited 100ms, so we need to wait for the remaining | ||
| // async actions to finish | ||
| await waitForElementToBeRemoved(() => screen.getByText('Observed Element')); | ||
| }); | ||
|
|
||
| test('waits with custom interval', async () => { | ||
| const mockFn = jest.fn(() => <View />); | ||
|
|
||
| try { | ||
| await waitForElementToBeRemoved(() => mockFn(), { | ||
| timeout: 400, | ||
| interval: 200, | ||
| }); | ||
| } catch (e) { | ||
| // Suppress expected error | ||
| } | ||
|
|
||
| expect(mockFn).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| test('works with fake timers', async () => { | ||
| jest.useFakeTimers(); | ||
|
|
||
| const mockFn = jest.fn(() => <View />); | ||
|
|
||
| waitForElementToBeRemoved(() => mockFn(), { | ||
| timeout: 400, | ||
| interval: 200, | ||
| }); | ||
|
|
||
| jest.advanceTimersByTime(400); | ||
| expect(mockFn).toHaveBeenCalledTimes(3); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // @flow | ||
| import waitFor from './waitFor'; | ||
| import type { WaitForOptions } from './waitFor'; | ||
|
|
||
| const isRemoved = (result) => | ||
| !result || (Array.isArray(result) && !result.length); | ||
|
|
||
| export default async function waitForElementToBeRemoved<T>( | ||
| expectation: () => T, | ||
| options?: WaitForOptions | ||
| ): Promise<null> { | ||
| // Created here so we get a nice stacktrace | ||
| const timeoutError = new Error('Timed out in waitForElementToBeRemoved.'); | ||
|
|
||
| return waitFor(() => { | ||
| let result; | ||
| try { | ||
| result = expectation(); | ||
| } catch (error) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!isRemoved(result)) { | ||
| throw timeoutError; | ||
| } | ||
|
|
||
| return null; | ||
| }, options); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if
nullis a proper type here. But we do not have anything meaningful to return.RTL declares its version in TS as
Promise<T>but usesreturn truein their impl.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO I would keep the
Promise<T>return type and return the initial result ofexpectationcall. Since in code we require that initiallyexpectationis successful (returns something) and only then starts throwing/returning nothing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, let's do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also logged issue in Dom TL that they have similar discrepancy.