-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(waitForExpect): add waitForExpect (#25)
* added waitForExpect with test * added typescript and simplified version of the waitForExpect, used and exports its typings * added initial notes about waitForExpect * fixed styling * minor stylistic change * updated tests to remove the nesting * updated readme * fixed d .md syntax * improved style Closes #21
- Loading branch information
Showing
6 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,40 @@ | ||
import React from 'react' | ||
import {render, waitForExpect} from '../' | ||
|
||
const fetchAMessage = () => | ||
new Promise(resolve => { | ||
// we are using random timeout here to simulate a real-time example | ||
// of an async operation calling a callback at a non-deterministic time | ||
const randomTimeout = Math.floor(Math.random() * 100) | ||
setTimeout(() => { | ||
resolve({returnedMessage: 'Hello World'}) | ||
}, randomTimeout) | ||
}) | ||
|
||
class ComponentWithLoader extends React.Component { | ||
state = {loading: true} | ||
async componentDidMount() { | ||
const data = await fetchAMessage() | ||
this.setState({data, loading: false}) // eslint-disable-line | ||
} | ||
render() { | ||
if (this.state.loading) { | ||
return <div>Loading...</div> | ||
} else { | ||
return ( | ||
<div data-testid="message"> | ||
Loaded this message: {this.state.data.returnedMessage}! | ||
</div> | ||
) | ||
} | ||
} | ||
} | ||
|
||
test('it waits for the data to be loaded', async () => { | ||
const {queryByText, queryByTestId} = render(<ComponentWithLoader />) | ||
|
||
expect(queryByText('Loading...')).toBeTruthy() | ||
|
||
await waitForExpect(() => expect(queryByText('Loading...')).toBeNull()) | ||
expect(queryByTestId('message').textContent).toMatch(/Hello World/) | ||
}) |
This file contains 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 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