Skip to content
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
56 changes: 56 additions & 0 deletions src/__tests__/screen.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
import {renderIntoDocument} from './helpers/test-utils'
import {screen} from '..'

beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {})
})

afterEach(() => {
console.log.mockRestore()
})

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()
})

test('exposes debug method', () => {
renderIntoDocument(
`<button>test</button><span>multi-test</span><div>multi-test</div>`,
)
// log document
screen.debug()
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
"<body>
<button>
test
</button>
<span>
multi-test
</span>
<div>
multi-test
</div>
</body>"
`)
console.log.mockClear()
// log single element
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a console.log.mockClear() between these so that each group of assertions can be more independent from the others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does make it a-lot more clear. Cool, I appreciate this suggestion.

screen.debug(screen.getByText('test', {selector: 'button'}))
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
"<button>
test
</button>"
`)
console.log.mockClear()
// log multiple elements
screen.debug(screen.getAllByText('multi-test'))
expect(console.log).toHaveBeenCalledTimes(2)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
"<span>
multi-test
</span>"
`)
expect(console.log.mock.calls[1][0]).toMatchInlineSnapshot(`
"<div>
multi-test
</div>"
`)
console.log.mockClear()
})

/* eslint no-console:0 */
9 changes: 7 additions & 2 deletions src/get-queries-for-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import * as defaultQueries from './queries'
/**
* @param {HTMLElement} element container
* @param {FuncMap} queries object of functions
* @param {Object} initialValue for reducer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea. This probably isn't how I would have done it, but I'm actually pretty cool with it.

* @returns {FuncMap} returns object of functions bound to container
*/
function getQueriesForElement(element, queries = defaultQueries) {
function getQueriesForElement(
element,
queries = defaultQueries,
initialValue = {},
) {
return Object.keys(queries).reduce((helpers, key) => {
const fn = queries[key]
helpers[key] = fn.bind(null, element)
return helpers
}, {})
}, initialValue)
}

export {getQueriesForElement}
27 changes: 18 additions & 9 deletions src/screen.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import * as queries from './queries'
import {getQueriesForElement} from './get-queries-for-element'
import {logDOM} from './pretty-dom'

const debug = (element, maxLength, options) =>
Array.isArray(element)
? element.forEach(el => logDOM(el, maxLength, options))
: logDOM(element, maxLength, options)

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
}, {})
? getQueriesForElement(document.body, queries, {debug})
: 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
},
{debug},
)