File tree 4 files changed +33
-0
lines changed
4 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { screen } from '..'
2
+
3
+ test ( 'the screen export throws a helpful error message when no global document is accessible' , ( ) => {
4
+ expect ( ( ) =>
5
+ screen . getByText ( / h e l l o w o r l d / i) ,
6
+ ) . toThrowErrorMatchingInlineSnapshot (
7
+ `"For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error"` ,
8
+ )
9
+ } )
Original file line number Diff line number Diff line change
1
+ import { renderIntoDocument } from './helpers/test-utils'
2
+ import { screen } from '..'
3
+
4
+ test ( 'exposes queries that are attached to document.body' , async ( ) => {
5
+ renderIntoDocument ( `<div>hello world</div>` )
6
+ screen . getByText ( / h e l l o w o r l d / i)
7
+ await screen . findByText ( / h e l l o w o r l d / i)
8
+ expect ( screen . queryByText ( / h e l l o w o r l d / i) ) . not . toBeNull ( )
9
+ } )
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ export {getDefaultNormalizer} from './matches'
11
11
export * from './get-node-text'
12
12
export * from './events'
13
13
export * from './get-queries-for-element'
14
+ export * from './screen'
14
15
export * from './query-helpers'
15
16
export { getRoles , logRoles , isInaccessible } from './role-helpers'
16
17
export * from './pretty-dom'
Original file line number Diff line number Diff line change
1
+ import * as queries from './queries'
2
+ import { getQueriesForElement } from './get-queries-for-element'
3
+
4
+ export const screen =
5
+ typeof document !== 'undefined' && document . body
6
+ ? getQueriesForElement ( document . body )
7
+ : Object . keys ( queries ) . reduce ( ( helpers , key ) => {
8
+ helpers [ key ] = ( ) => {
9
+ throw new TypeError (
10
+ 'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error' ,
11
+ )
12
+ }
13
+ return helpers
14
+ } , { } )
You can’t perform that action at this time.
0 commit comments