@@ -262,13 +262,27 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {
262
262
expect ( result . current . error ) . toBeInstanceOf ( Error ) ;
263
263
} ) ;
264
264
265
+ it ( "Clears out existing errors if a new copy operation succeeds" , async ( ) => {
266
+ const text = "dummy-text" ;
267
+ const { result} = renderUseClipboard ( ) ;
268
+ setSimulateFailure ( true ) ;
269
+
270
+ await act ( ( ) => result . current . copyToClipboard ( text ) ) ;
271
+ expect ( result . current . error ) . toBeInstanceOf ( Error ) ;
272
+
273
+ setSimulateFailure ( false ) ;
274
+ await assertClipboardUpdateLifecycle ( result , text ) ;
275
+ expect ( result . current . error ) . toBeUndefined ( ) ;
276
+ } ) ;
277
+
265
278
// This check is really important to ensure that it's easy to plop this
266
279
// inside of useEffect calls without having to think about dependencies too
267
280
// much
268
281
it ( "Ensures that the copyToClipboard function always maintains a stable reference across all re-renders" , async ( ) => {
269
282
const initialOnError = jest . fn ( ) ;
270
283
const { result, rerender} = renderUseClipboard ( {
271
284
onError :initialOnError ,
285
+ clearErrorOnSuccess :true ,
272
286
} ) ;
273
287
const initialCopy = result . current . copyToClipboard ;
274
288
@@ -283,6 +297,11 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {
283
297
expect ( result . current . copyToClipboard ) . toBe ( initialCopy ) ;
284
298
rerender ( { onError :initialOnError } ) ;
285
299
300
+ // Re-render with a new clear value then swap back to simplify testing
301
+ rerender ( { onError :initialOnError , clearErrorOnSuccess :false } ) ;
302
+ expect ( result . current . copyToClipboard ) . toBe ( initialCopy ) ;
303
+ rerender ( { onError :initialOnError , clearErrorOnSuccess :true } ) ;
304
+
286
305
// Trigger a failed clipboard interaction
287
306
setSimulateFailure ( true ) ;
288
307
await act ( ( ) => result . current . copyToClipboard ( "dummy-text-2" ) ) ;