Skip to content

Commit 3805b27

Browse files
authored
feat: add a debug method to screen. (#429)
* Add a debug method to screen. * Make suggested edits.
1 parent 6c4486c commit 3805b27

File tree

3 files changed

+81
-11
lines changed

3 files changed

+81
-11
lines changed

src/__tests__/screen.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,65 @@
11
import {renderIntoDocument} from './helpers/test-utils'
22
import {screen} from '..'
33

4+
beforeEach(() => {
5+
jest.spyOn(console, 'log').mockImplementation(() => {})
6+
})
7+
8+
afterEach(() => {
9+
console.log.mockRestore()
10+
})
11+
412
test('exposes queries that are attached to document.body', async () => {
513
renderIntoDocument(`<div>hello world</div>`)
614
screen.getByText(/hello world/i)
715
await screen.findByText(/hello world/i)
816
expect(screen.queryByText(/hello world/i)).not.toBeNull()
917
})
18+
19+
test('exposes debug method', () => {
20+
renderIntoDocument(
21+
`<button>test</button><span>multi-test</span><div>multi-test</div>`,
22+
)
23+
// log document
24+
screen.debug()
25+
expect(console.log).toHaveBeenCalledTimes(1)
26+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
27+
"<body>
28+
<button>
29+
test
30+
</button>
31+
<span>
32+
multi-test
33+
</span>
34+
<div>
35+
multi-test
36+
</div>
37+
</body>"
38+
`)
39+
console.log.mockClear()
40+
// log single element
41+
screen.debug(screen.getByText('test', {selector: 'button'}))
42+
expect(console.log).toHaveBeenCalledTimes(1)
43+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
44+
"<button>
45+
test
46+
</button>"
47+
`)
48+
console.log.mockClear()
49+
// log multiple elements
50+
screen.debug(screen.getAllByText('multi-test'))
51+
expect(console.log).toHaveBeenCalledTimes(2)
52+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
53+
"<span>
54+
multi-test
55+
</span>"
56+
`)
57+
expect(console.log.mock.calls[1][0]).toMatchInlineSnapshot(`
58+
"<div>
59+
multi-test
60+
</div>"
61+
`)
62+
console.log.mockClear()
63+
})
64+
65+
/* eslint no-console:0 */

src/get-queries-for-element.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import * as defaultQueries from './queries'
77
/**
88
* @param {HTMLElement} element container
99
* @param {FuncMap} queries object of functions
10+
* @param {Object} initialValue for reducer
1011
* @returns {FuncMap} returns object of functions bound to container
1112
*/
12-
function getQueriesForElement(element, queries = defaultQueries) {
13+
function getQueriesForElement(
14+
element,
15+
queries = defaultQueries,
16+
initialValue = {},
17+
) {
1318
return Object.keys(queries).reduce((helpers, key) => {
1419
const fn = queries[key]
1520
helpers[key] = fn.bind(null, element)
1621
return helpers
17-
}, {})
22+
}, initialValue)
1823
}
1924

2025
export {getQueriesForElement}

src/screen.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import * as queries from './queries'
22
import {getQueriesForElement} from './get-queries-for-element'
3+
import {logDOM} from './pretty-dom'
4+
5+
const debug = (element, maxLength, options) =>
6+
Array.isArray(element)
7+
? element.forEach(el => logDOM(el, maxLength, options))
8+
: logDOM(element, maxLength, options)
39

410
export const screen =
511
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-
}, {})
12+
? getQueriesForElement(document.body, queries, {debug})
13+
: Object.keys(queries).reduce(
14+
(helpers, key) => {
15+
helpers[key] = () => {
16+
throw new TypeError(
17+
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error',
18+
)
19+
}
20+
return helpers
21+
},
22+
{debug},
23+
)

0 commit comments

Comments
 (0)