@@ -212,14 +212,32 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {
212
212
expect ( onError ) . toBeCalled ( ) ;
213
213
} ) ;
214
214
215
- it ( "Should dispatch a new toast message to the global snackbar if no callback is provided" , async ( ) => {
215
+ it ( "Should dispatch a new toast message to the global snackbar when errors happen if no error callback is provided to the hook " , async ( ) => {
216
216
const textToCopy = "crow" ;
217
217
const { result } = renderUseClipboard ( { textToCopy } ) ;
218
218
219
+ /**
220
+ * @todo Look into why deferring error-based state updates to the global
221
+ * snackbar still kicks up act warnings, even after using act for the main
222
+ * source of the state transition
223
+ */
219
224
setSimulateFailure ( true ) ;
220
225
await act ( ( ) => result . current . copyToClipboard ( ) ) ;
221
226
222
227
const errorMessageNode = screen . queryByText ( COPY_FAILED_MESSAGE ) ;
223
228
expect ( errorMessageNode ) . not . toBeNull ( ) ;
224
229
} ) ;
230
+
231
+ it ( "Should expose the error value for render logic when a copy fails" , async ( ) => {
232
+ // Using empty error callback to silence any possible act warnings from
233
+ // Snackbar state transitions
234
+ const onError = jest . fn ( ) ;
235
+ const textToCopy = "hamster" ;
236
+ const { result } = renderUseClipboard ( { textToCopy, onError } ) ;
237
+
238
+ setSimulateFailure ( true ) ;
239
+ await act ( ( ) => result . current . copyToClipboard ( ) ) ;
240
+
241
+ expect ( result . current . error ) . toBeInstanceOf ( Error ) ;
242
+ } ) ;
225
243
} ) ;
0 commit comments