Skip to content

Commit 042ae51

Browse files
Balavishnu V JBalavishnu V J
Balavishnu V J
authored and
Balavishnu V J
committed
feat: add logPlaygroundUrl in screen
1 parent 03cfef8 commit 042ae51

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@
4242
"@babel/runtime": "^7.10.3",
4343
"@types/aria-query": "^4.2.0",
4444
"aria-query": "^4.2.2",
45+
"chalk": "^4.1.0",
4546
"dom-accessibility-api": "^0.5.1",
46-
"pretty-format": "^26.4.2",
47-
"chalk": "^4.1.0"
47+
"lz-string": "^1.4.4",
48+
"pretty-format": "^26.4.2"
4849
},
4950
"devDependencies": {
5051
"@testing-library/jest-dom": "^5.10.1",
@@ -78,4 +79,4 @@
7879
"url": "https://github.com/testing-library/dom-testing-library/issues"
7980
},
8081
"homepage": "https://github.com/testing-library/dom-testing-library#readme"
81-
}
82+
}

src/__tests__/screen.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {screen} from '..'
2-
import {renderIntoDocument} from './helpers/test-utils'
2+
import {render, renderIntoDocument} from './helpers/test-utils'
33

44
// Since screen.debug internally calls getUserCodeFrame, we mock it so it doesn't affect these tests
55
jest.mock('../get-user-code-frame', () => ({
@@ -21,6 +21,27 @@ test('exposes queries that are attached to document.body', async () => {
2121
expect(screen.queryByText(/hello world/i)).not.toBeNull()
2222
})
2323

24+
test('logs Playground URL that are attached to document.body', () => {
25+
renderIntoDocument(`<div>hello world</div>`)
26+
screen.logTestingPlaygroundURL()
27+
expect(console.log).toHaveBeenCalledTimes(1)
28+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
29+
"Open this URL in your browser
30+
31+
https://testing-playground.com/#markup=DwEwlgbgfAFgpgGwQewAQHdkCcEmAenGiA"
32+
`)
33+
})
34+
35+
test('logs Playground URL that are passed as element', () => {
36+
screen.logTestingPlaygroundURL(render(`<h1>Sign <em>up</em></h1>`).container)
37+
expect(console.log).toHaveBeenCalledTimes(1)
38+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
39+
"Open this URL in your browser
40+
41+
https://testing-playground.com/#markup=DwCwjAfAyglg5gOwATAKYFsIFcAOwD0GEB4EQA"
42+
`)
43+
})
44+
2445
test('exposes debug method', () => {
2546
renderIntoDocument(
2647
`<button>test</button><span>multi-test</span><div>multi-test</div>`,

src/screen.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1+
import {compressToEncodedURIComponent} from 'lz-string'
12
import * as queries from './queries'
23
import {getQueriesForElement} from './get-queries-for-element'
34
import {logDOM} from './pretty-dom'
5+
import {getDocument} from './helpers'
6+
7+
function unindent(string) {
8+
// remove white spaces first, to save a few bytes.
9+
// testing-playground will reformat on load any ways.
10+
return (string || '').replace(/[ \t]*[\n][ \t]*/g, '\n')
11+
}
12+
13+
function encode(value) {
14+
return compressToEncodedURIComponent(unindent(value))
15+
}
16+
17+
function getPlaygroundUrl(element) {
18+
return `https://testing-playground.com/#markup=${encode(element.innerHTML)}`
19+
}
420

521
const debug = (element, maxLength, options) =>
622
Array.isArray(element)
723
? element.forEach(el => logDOM(el, maxLength, options))
824
: logDOM(element, maxLength, options)
925

26+
const logTestingPlaygroundURL = (element = getDocument().body) => {
27+
console.log(`Open this URL in your browser\n\n${getPlaygroundUrl(element)}`)
28+
}
29+
30+
const initialValue = {debug, logTestingPlaygroundURL}
1031
export const screen =
1132
typeof document !== 'undefined' && document.body
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-
)
33+
? getQueriesForElement(document.body, queries, initialValue)
34+
: Object.keys(queries).reduce((helpers, key) => {
35+
helpers[key] = () => {
36+
throw new TypeError(
37+
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error',
38+
)
39+
}
40+
return helpers
41+
}, initialValue)

0 commit comments

Comments
 (0)