@@ -61,33 +61,71 @@ test("copies via execCommand when navigator.clipboard is missing", async () => {
6161 expect ( execCommandCalls ) . toEqual ( [ { command : "copy" , selected : "hello insecure origin" } ] )
6262} )
6363
64- test ( "code-block copy button works without navigator.clipboard" , async ( ) => {
65- hideClipboard ( )
66- expect ( navigator . clipboard ) . toBeUndefined ( )
67- stubExecCommand ( true )
64+ for ( const legacy of [ "missing" , "throws" , "false" , "success" ] as const ) {
65+ for ( const modern of [ "missing" , "rejected" , "success" ] as const ) {
66+ test ( `clipboard: execCommand ${ legacy } , navigator ${ modern } ` , async ( ) => {
67+ hideClipboard ( )
68+ const writes : string [ ] = [ ]
69+ if ( modern !== "missing" ) {
70+ Object . defineProperty ( navigator , "clipboard" , {
71+ configurable : true ,
72+ value : {
73+ writeText : async ( text : string ) => {
74+ writes . push ( text )
75+ if ( modern === "rejected" ) throw new Error ( "Clipboard denied" )
76+ } ,
77+ } ,
78+ } )
79+ }
80+ stubExecCommand ( legacy === "success" )
81+ if ( legacy === "missing" ) {
82+ Object . defineProperty ( document , "execCommand" , { configurable : true , value : undefined } )
83+ }
84+ if ( legacy === "throws" ) {
85+ document . execCommand = ( ) => {
86+ throw new Error ( "Legacy copy denied" )
87+ }
88+ }
6889
69- const root = document . createElement ( "div" )
70- const wrapper = document . createElement ( "div" )
71- wrapper . setAttribute ( "data-component" , "markdown-code" )
72- const pre = document . createElement ( "pre" )
73- const code = document . createElement ( "code" )
74- code . textContent = "const x = 1"
75- pre . appendChild ( code )
76- const host = document . createElement ( "div" )
77- host . setAttribute ( "data-slot" , "markdown-copy-button" )
78- const inner = document . createElement ( "button" )
79- inner . type = "button"
80- host . appendChild ( inner )
81- wrapper . appendChild ( pre )
82- wrapper . appendChild ( host )
83- root . appendChild ( wrapper )
84- document . body . appendChild ( root )
85-
86- const cleanup = setupCodeCopy ( root , ( ) => ( { copy : "Copy" , copied : "Copied" } ) )
87- inner . dispatchEvent ( new MouseEvent ( "click" , { bubbles : true } ) )
88- await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
89- cleanup ( )
90-
91- expect ( execCommandCalls ) . toEqual ( [ { command : "copy" , selected : "const x = 1" } ] )
92- expect ( host . getAttribute ( "data-copied" ) ) . toBe ( "true" )
93- } )
90+ try {
91+ expect ( await writeClipboard ( "copy me" ) ) . toBe ( legacy === "success" || modern === "success" )
92+ expect ( writes ) . toEqual ( legacy === "success" || modern === "missing" ? [ ] : [ "copy me" ] )
93+ } finally {
94+ expect ( document . querySelectorAll ( "textarea" ) . length ) . toBe ( 0 )
95+ }
96+ } )
97+ }
98+ }
99+
100+ for ( const succeeds of [ true , false ] ) {
101+ test ( `code-block copy button without navigator.clipboard: ${ succeeds } ` , async ( ) => {
102+ hideClipboard ( )
103+ expect ( navigator . clipboard ) . toBeUndefined ( )
104+ stubExecCommand ( succeeds )
105+
106+ const root = document . createElement ( "div" )
107+ const wrapper = document . createElement ( "div" )
108+ wrapper . setAttribute ( "data-component" , "markdown-code" )
109+ const pre = document . createElement ( "pre" )
110+ const code = document . createElement ( "code" )
111+ code . textContent = "const x = 1"
112+ pre . appendChild ( code )
113+ const host = document . createElement ( "div" )
114+ host . setAttribute ( "data-slot" , "markdown-copy-button" )
115+ const inner = document . createElement ( "button" )
116+ inner . type = "button"
117+ host . appendChild ( inner )
118+ wrapper . appendChild ( pre )
119+ wrapper . appendChild ( host )
120+ root . appendChild ( wrapper )
121+ document . body . appendChild ( root )
122+
123+ const cleanup = setupCodeCopy ( root , ( ) => ( { copy : "Copy" , copied : "Copied" } ) )
124+ inner . dispatchEvent ( new MouseEvent ( "click" , { bubbles : true } ) )
125+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
126+ cleanup ( )
127+
128+ expect ( execCommandCalls ) . toEqual ( [ { command : "copy" , selected : "const x = 1" } ] )
129+ expect ( host . getAttribute ( "data-copied" ) ) . toBe ( succeeds ? "true" : null )
130+ } )
131+ }
0 commit comments