Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit 9b72d7c

Browse files
committed
fix: improved error messaging for failed copy commands
1 parent ffcccbc commit 9b72d7c

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"activeTab",
3636
"contextMenus",
3737
"clipboardWrite",
38-
"clipboardRead"
38+
"clipboardRead",
39+
"notifications"
3940
]
4041
}

src/background.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Bridge.onMessage(
88
})
99
);
1010

11+
Bridge.onMessage("show-notification", ({ data: { notification } }) => {
12+
chrome.notifications.create(notification);
13+
});
14+
1115
const contextMenuItem = {
1216
id: "whichQuery",
1317
title: "Testing Library",
@@ -28,9 +32,14 @@ chrome.contextMenus.create(contextMenuItem, () => {
2832
});
2933
chrome.contextMenus.onClicked.addListener((info, tab) => {
3034
if (info.parentMenuItemId === "whichQuery") {
31-
chrome.tabs.sendMessage(tab.id, {
32-
type: "getSuggestedQuery",
33-
variant: info.menuItemId,
35+
chrome.windows.update(tab.windowId, { focused: true }, () => {
36+
chrome.tabs.update(tab.id, { active: true }, () => {
37+
chrome.tabs.sendMessage(tab.id, {
38+
type: "getSuggestedQuery",
39+
variant: info.menuItemId,
40+
});
41+
});
3442
});
43+
// Set focus on tab
3544
}
3645
});

src/contentScript.js

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
const Bridge = require("crx-bridge").default;
2+
// screen is used by the eval below. 😎
3+
// eslint-disable-next-line no-unused-vars
4+
const { screen, getSuggestedQuery } = require("@testing-library/dom");
5+
26
Bridge.onMessage("connect", () => {
37
//needed to establish connection
48
return "connected";
@@ -16,54 +20,91 @@ document.addEventListener(
1620
true
1721
);
1822

23+
const CANT_COPY_NOTIFICATION = {
24+
type: "basic",
25+
iconUrl: "icons/icon64.png",
26+
title: "Can't Copy Query",
27+
message: "Check browser console for details.",
28+
contextMessage: "Are you are debugging?",
29+
};
30+
1931
chrome.runtime.onMessage.addListener((request) => {
2032
if (request.type == "getSuggestedQuery") {
2133
const { suggestedQuery } = getClosestQuery(currentElement, request.variant);
2234
if (suggestedQuery) {
23-
navigator.clipboard.writeText(`screen.${suggestedQuery.toString()}`).then(
24-
() => {},
35+
const queryToCopy = `screen.${suggestedQuery.toString()}`;
36+
navigator.clipboard.writeText(queryToCopy).then(
2537
() => {
26-
// eslint-disable-next-line no-console
27-
console.log(suggestedQuery.toString());
28-
// eslint-disable-next-line no-console
38+
// TODO: add option to toggle this
39+
Bridge.sendMessage(
40+
"show-notification",
41+
{
42+
notification: {
43+
type: "basic",
44+
iconUrl: "icons/icon64.png",
45+
title: "Copied Query",
46+
message: queryToCopy,
47+
},
48+
},
49+
"background"
50+
);
51+
},
52+
(err) => {
53+
/* eslint-disable no-console */
54+
Bridge.sendMessage(
55+
"show-notification",
56+
{ notification: CANT_COPY_NOTIFICATION },
57+
"background"
58+
);
59+
60+
// TODO: figure this crap out https://github.com/testing-library/which-query/issues/9
2961
console.warn(
30-
"Can't copy query to clipboard when focus is not in the browser."
62+
`
63+
Can't copy query to clipboard when focus is not in the browser.
64+
Click in the page and be sure devtools does not have focus when using Testing Library copy menus.
65+
Know how to fix this issue? Pull Requests welcome: https://github.com/testing-library/which-query/issues/9
66+
`,
67+
err
3168
);
69+
console.log("Here is the query you tried to copy:");
70+
console.log(queryToCopy);
71+
/* eslint-enable no-console */
3272
}
3373
);
3474
}
3575
}
3676
});
3777

38-
function getClosestQuery(element, variant) {
78+
function getClosestQuery(element, variant, { doValidate = false } = {}) {
3979
let suggestedQuery = null;
4080
let nextEl = element;
4181
while (!suggestedQuery && nextEl !== document) {
42-
suggestedQuery = window.TestingLibraryDom.getSuggestedQuery(
43-
nextEl,
44-
variant
45-
);
82+
suggestedQuery = getSuggestedQuery(nextEl, variant);
4683

4784
nextEl = nextEl.parentElement;
4885
}
4986

50-
// Why use javascript if you can't use eval from time to time. Deal with it. 😎
51-
// eslint-disable-next-line no-eval
52-
const proposed = window.eval(
53-
`window.TestingLibraryDom.screen.${suggestedQuery
54-
.toString()
55-
.replace("get", "queryAll")}`
56-
);
87+
let validations = {};
88+
if (doValidate) {
89+
// Why use javascript if you can't use eval from time to time. Deal with it. 😎
90+
// eslint-disable-next-line no-eval
91+
const proposed = eval(
92+
`screen.${suggestedQuery.toString().replace("get", "queryAll")}`
93+
);
5794

58-
const exactIndex = proposed.findIndex((el) => el === element);
95+
const exactIndex = proposed.findIndex((el) => el === element);
5996

60-
const length = proposed.length;
97+
const length = proposed.length;
98+
validations = { exactIndex, length };
99+
}
61100

62-
return { suggestedQuery, length, exactIndex };
101+
return { suggestedQuery, ...validations };
63102
}
64103

65104
function showElement(el) {
66-
const { suggestedQuery, length, exactIndex } = getClosestQuery(el, "get");
105+
const { suggestedQuery, length, exactIndex } = getClosestQuery(el, "get", {
106+
doValidate: true,
107+
});
67108
Bridge.sendMessage(
68109
"show-suggestion",
69110
{

0 commit comments

Comments
 (0)