Skip to content

Commit d5f39d1

Browse files
Isolate playground logic in playground-helper.ts
1 parent 577132d commit d5f39d1

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"devDependencies": {
5151
"@testing-library/jest-dom": "^5.11.6",
52+
"@types/lz-string": "^1.3.34",
5253
"jest-in-case": "^1.0.2",
5354
"jest-serializer-ansi": "^1.0.3",
5455
"jest-watch-select-projects": "^2.0.0",

src/config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {getPlaygroundUrl} from 'playground-helper'
12
import {Config, ConfigFn} from '../types/config'
23
import {prettyDOM} from './pretty-dom'
34

@@ -31,14 +32,17 @@ let config: InternalConfig = {
3132

3233
// called when getBy* queries fail. (message, container) => Error
3334
getElementError(message, container) {
34-
const playgroundMessage =
35-
'Check out the Testing-Library playground to learn more on how to make the right query: https://testing-playground.com'
35+
const playgroundUrl =
36+
message &&
37+
getConfig().printPlaygroundLink &&
38+
getPlaygroundUrl(container, false)
3639

3740
const error = new Error(
3841
[
3942
message,
4043
prettyDOM(container),
41-
message && getConfig().printPlaygroundLink && playgroundMessage,
44+
playgroundUrl &&
45+
`Open this DOM in the Testing-Library Playground:\n${playgroundUrl}`,
4246
]
4347
.filter(Boolean)
4448
.join('\n\n'),

src/playground-helper.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {compressToEncodedURIComponent} from 'lz-string'
2+
3+
function unindent(value: string) {
4+
// remove white spaces first, to save a few bytes.
5+
// testing-playground will reformat on load any ways.
6+
return value.replace(/[ \t]*[\n][ \t]*/g, '\n')
7+
}
8+
9+
function encode(value: string) {
10+
return compressToEncodedURIComponent(unindent(value))
11+
}
12+
13+
function getPlaygroundUrl(element: Element | null, logErrors = true) {
14+
if (!element || !('innerHTML' in element)) {
15+
if (logErrors) {
16+
console.log(`The element you're providing isn't a valid DOM element.`)
17+
}
18+
return null
19+
}
20+
21+
if (!element.innerHTML) {
22+
if (logErrors) {
23+
console.log(`The provided element doesn't have any children.`)
24+
}
25+
return null
26+
}
27+
28+
return `https://testing-playground.com/#markup=${encode(element.innerHTML)}`
29+
}
30+
31+
export {getPlaygroundUrl}

src/screen.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
1-
import {compressToEncodedURIComponent} from 'lz-string'
1+
import {getPlaygroundUrl} from './playground-helper'
22
import * as queries from './queries'
33
import {getQueriesForElement} from './get-queries-for-element'
44
import {logDOM} from './pretty-dom'
55
import {getDocument} from './helpers'
66

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(markup) {
18-
return `https://testing-playground.com/#markup=${encode(markup)}`
19-
}
20-
217
const debug = (element, maxLength, options) =>
228
Array.isArray(element)
239
? element.forEach(el => logDOM(el, maxLength, options))
2410
: logDOM(element, maxLength, options)
2511

2612
const logTestingPlaygroundURL = (element = getDocument().body) => {
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
13+
const url = getPlaygroundUrl(element, console.log)
14+
if (url) {
15+
console.log(`Open this URL in your browser\n\n${url}`)
3416
}
35-
console.log(
36-
`Open this URL in your browser\n\n${getPlaygroundUrl(element.innerHTML)}`,
37-
)
3817
}
3918

4019
const initialValue = {debug, logTestingPlaygroundURL}

0 commit comments

Comments
 (0)