Skip to content

feat(screen): Add screen export which has all queries bound to the body #412

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 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions src/__node_tests__/screen.js
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"`,
)
})
9 changes: 9 additions & 0 deletions src/__tests__/screen.js
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()
})
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {getDefaultNormalizer} from './matches'
export * from './get-node-text'
export * from './events'
export * from './get-queries-for-element'
export * from './screen'
export * from './query-helpers'
export {getRoles, logRoles, isInaccessible} from './role-helpers'
export * from './pretty-dom'
Expand Down
14 changes: 14 additions & 0 deletions src/screen.js
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
}, {})