-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(screen): Add screen export which has all queries bound to the bo…
…dy (#412)
- Loading branch information
Kent C. Dodds
authored
Dec 13, 2019
1 parent
60b65f5
commit 4fed5ae
Showing
4 changed files
with
33 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {screen} from '..' | ||
|
||
test('the screen export throws a helpful error message when no global document is accessible', () => { | ||
expect(() => | ||
screen.getByText(/hello world/i), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error"`, | ||
) | ||
}) |
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,9 @@ | ||
import {renderIntoDocument} from './helpers/test-utils' | ||
import {screen} from '..' | ||
|
||
test('exposes queries that are attached to document.body', async () => { | ||
renderIntoDocument(`<div>hello world</div>`) | ||
screen.getByText(/hello world/i) | ||
await screen.findByText(/hello world/i) | ||
expect(screen.queryByText(/hello world/i)).not.toBeNull() | ||
}) |
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,14 @@ | ||
import * as queries from './queries' | ||
import {getQueriesForElement} from './get-queries-for-element' | ||
|
||
export const screen = | ||
typeof document !== 'undefined' && document.body | ||
? getQueriesForElement(document.body) | ||
: Object.keys(queries).reduce((helpers, key) => { | ||
helpers[key] = () => { | ||
throw new TypeError( | ||
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error', | ||
) | ||
} | ||
return helpers | ||
}, {}) |