11const 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+
26Bridge . 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+
1931chrome . 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
65104function 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