Skip to content

Commit 933e59c

Browse files
delca85smeijer
andauthored
feat(devtools): add support for custom test-id attributes (#227)
Co-authored-by: Stephan Meijer <stephan.meijer@gmail.com>
1 parent bd8f11a commit 933e59c

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Thanks goes to these people ([emoji key][emojis]):
126126

127127
<!-- markdownlint-enable -->
128128
<!-- prettier-ignore-end -->
129+
129130
<!-- ALL-CONTRIBUTORS-LIST:END -->
130131

131132
This project follows the [all-contributors][all-contributors] specification.

devtools/src/content-script/contentScript.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ function init() {
4646
return { result };
4747
});
4848

49+
Bridge.onMessage('SET_CUSTOM_TEST_ID', function ({ data }) {
50+
parser.configure({ testIdAttribute: data.customTestIdAttribute });
51+
});
52+
4953
// when the selected element is changed by using the element inspector,
5054
// this method will be called from devtools/main.js
5155
hook.onSelectionChanged = function onSelectionChanged(el) {

devtools/src/devtools/components/MenuBar.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import React from 'react';
1+
import React, { useState, useCallback } from 'react';
22
import Bridge from 'crx-bridge';
3+
import { SettingsIcon } from '@primer/octicons-react';
4+
import { Dialog } from '@reach/dialog';
35

46
import inspectedWindow from '../lib/inspectedWindow';
57

@@ -9,6 +11,33 @@ import InspectIcon from './InspectIcon';
911
import LogIcon from './LogIcon';
1012

1113
function MenuBar({ cssPath, suggestion }) {
14+
const [settingsModalVisible, setSettingsModalVisible] = useState(false);
15+
const [testIdCustomAttribute, setTestIdCustomAttribute] = useState(
16+
'data-testid',
17+
);
18+
19+
const onClickSettingsHandler = useCallback(
20+
() => setSettingsModalVisible(true),
21+
[setSettingsModalVisible],
22+
);
23+
const onCloseSettingsModalHandler = useCallback(
24+
() => setSettingsModalVisible(false),
25+
[setSettingsModalVisible],
26+
);
27+
28+
const onTestIdCustomAttributeChangeHandler = useCallback(
29+
(event) => setTestIdCustomAttribute(event.target.value),
30+
[setTestIdCustomAttribute],
31+
);
32+
33+
const onClickSettingsModalOkButtonHandler = useCallback(() => {
34+
Bridge.sendMessage(
35+
'SET_CUSTOM_TEST_ID',
36+
{ customTestIdAttribute: testIdCustomAttribute },
37+
'content-script',
38+
);
39+
onCloseSettingsModalHandler();
40+
}, [testIdCustomAttribute, onCloseSettingsModalHandler]);
1241
return (
1342
<div className="h-8 p-2 border-b space-x-4 flex">
1443
<button
@@ -33,6 +62,31 @@ function MenuBar({ cssPath, suggestion }) {
3362

3463
<div className="flex-auto" />
3564

65+
<button
66+
className="focus:outline-none"
67+
title="View settings"
68+
onClick={onClickSettingsHandler}
69+
>
70+
<SettingsIcon />
71+
</button>
72+
<Dialog
73+
isOpen={settingsModalVisible}
74+
onDismiss={onCloseSettingsModalHandler}
75+
aria-label="settings-modal"
76+
>
77+
<input
78+
type="text"
79+
value={testIdCustomAttribute}
80+
onChange={onTestIdCustomAttributeChangeHandler}
81+
/>
82+
<button
83+
onClick={onClickSettingsModalOkButtonHandler}
84+
disabled={!testIdCustomAttribute}
85+
>
86+
<span>Ok</span>
87+
</button>
88+
</Dialog>
89+
3690
<button
3791
className="focus:outline-none"
3892
title="Inspect the matching DOM element"

devtools/src/devtools/panel.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="utf8" />
55
<link rel="stylesheet" href="../../../src/styles/index.css" />
6+
<link rel="stylesheet" type="text/css" href="../../../node_modules/@reach/dialog/styles.css" />
67
</head>
78
<body>
89
<div id="app" />

devtools/src/devtools/panel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'regenerator-runtime/runtime';
2-
import React, { useState, useCallback, useEffect, useRef } from 'react';
2+
import React, { useState, useEffect, useRef, useCallback } from 'react';
33
import ReactDOM from 'react-dom';
44
import Bridge from 'crx-bridge';
55
import Query from '../../../src/components/Query';

src/parser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
queries,
1111
getRoles,
1212
logDOM,
13+
configure as testingLibraryConfigure,
1314
} from '@testing-library/dom';
1415

1516
import userEvent from '@testing-library/user-event';
@@ -99,7 +100,6 @@ function createEvaluator({ rootNode }) {
99100
user: userEvent,
100101
container: rootNode,
101102
});
102-
103103
const evaluator = Function.apply(null, [
104104
...Object.keys(context),
105105
'expr',
@@ -258,6 +258,10 @@ function runUnsafe({ rootNode, query }) {
258258
return result;
259259
}
260260

261+
function configure(configuration) {
262+
testingLibraryConfigure(configuration);
263+
}
264+
261265
function parse({ rootNode, markup, query, cacheId, prevResult }) {
262266
if (typeof markup !== 'string' && !rootNode) {
263267
throw new Error('either markup or rootNode should be provided');
@@ -295,4 +299,5 @@ function parse({ rootNode, markup, query, cacheId, prevResult }) {
295299

296300
export default {
297301
parse,
302+
configure,
298303
};

0 commit comments

Comments
 (0)