Skip to content

Commit

Permalink
feat(screen): Add screen export which has all queries bound to the bo…
Browse files Browse the repository at this point in the history
…dy (#412)
  • Loading branch information
Kent C. Dodds authored Dec 13, 2019
1 parent 60b65f5 commit 4fed5ae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
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
}, {})

0 comments on commit 4fed5ae

Please sign in to comment.