Skip to content

Commit 7ccfeba

Browse files
committed
fix: handle non element cases
1 parent 042ae51 commit 7ccfeba

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/__tests__/screen.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ test('logs Playground URL that are attached to document.body', () => {
3232
`)
3333
})
3434

35+
test('logs messsage when element is empty', () => {
36+
screen.logTestingPlaygroundURL(render('').container)
37+
expect(console.log).toHaveBeenCalledTimes(1)
38+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(
39+
`"The provided element doesn't have any children."`,
40+
)
41+
})
42+
43+
test('logs messsage when element is not a valid HTML', () => {
44+
screen.logTestingPlaygroundURL(null)
45+
expect(console.log).toHaveBeenCalledTimes(1)
46+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(
47+
`"The element you're providing isn't a valid DOM element."`,
48+
)
49+
console.log.mockClear()
50+
screen.logTestingPlaygroundURL({})
51+
expect(console.log).toHaveBeenCalledTimes(1)
52+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(
53+
`"The element you're providing isn't a valid DOM element."`,
54+
)
55+
})
56+
3557
test('logs Playground URL that are passed as element', () => {
3658
screen.logTestingPlaygroundURL(render(`<h1>Sign <em>up</em></h1>`).container)
3759
expect(console.log).toHaveBeenCalledTimes(1)

src/screen.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import {getDocument} from './helpers'
77
function unindent(string) {
88
// remove white spaces first, to save a few bytes.
99
// testing-playground will reformat on load any ways.
10-
return (string || '').replace(/[ \t]*[\n][ \t]*/g, '\n')
10+
return string.replace(/[ \t]*[\n][ \t]*/g, '\n')
1111
}
1212

1313
function encode(value) {
1414
return compressToEncodedURIComponent(unindent(value))
1515
}
1616

17-
function getPlaygroundUrl(element) {
18-
return `https://testing-playground.com/#markup=${encode(element.innerHTML)}`
17+
function getPlaygroundUrl(markup) {
18+
return `https://testing-playground.com/#markup=${encode(markup)}`
1919
}
2020

2121
const debug = (element, maxLength, options) =>
@@ -24,7 +24,17 @@ const debug = (element, maxLength, options) =>
2424
: logDOM(element, maxLength, options)
2525

2626
const logTestingPlaygroundURL = (element = getDocument().body) => {
27-
console.log(`Open this URL in your browser\n\n${getPlaygroundUrl(element)}`)
27+
if (!element || !('innerHTML' in element)) {
28+
console.log(`The element you're providing isn't a valid DOM element.`)
29+
return
30+
}
31+
if (!element.innerHTML) {
32+
console.log(`The provided element doesn't have any children.`)
33+
return
34+
}
35+
console.log(
36+
`Open this URL in your browser\n\n${getPlaygroundUrl(element.innerHTML)}`,
37+
)
2838
}
2939

3040
const initialValue = {debug, logTestingPlaygroundURL}

0 commit comments

Comments
 (0)