4
4
*
5
5
* When you call user.setup to make a new user session, it will make a mock
6
6
* clipboard instance that will always succeed. It also can't be removed after
7
- * it's been added. This actually makes testing useClipboard impossible to test
8
- * properly because any call to user.setup immediately pollutes the tests with
9
- * false negatives. Even if something should fail, it won't.
7
+ * it's been added, and it will persist across test cases. This actually makes
8
+ * testing useClipboard properly impossible because any call to user.setup
9
+ * immediately pollutes the tests with false negatives. Even if something should
10
+ * fail, it won't.
10
11
*/
11
12
import { act , renderHook , screen } from "@testing-library/react" ;
12
13
import { GlobalSnackbar } from "components/GlobalSnackbar/GlobalSnackbar" ;
@@ -218,8 +219,9 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {
218
219
219
220
/**
220
221
* @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
222
+ * snackbar still kicks up act warnings, even after wrapping copyToClipboard
223
+ * in act. copyToClipboard should be the main source of the state
224
+ * transitions,
223
225
*/
224
226
setSimulateFailure ( true ) ;
225
227
await act ( ( ) => result . current . copyToClipboard ( ) ) ;
@@ -228,12 +230,12 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {
228
230
expect ( errorMessageNode ) . not . toBeNull ( ) ;
229
231
} ) ;
230
232
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 ( ) ;
233
+ it ( "Should expose the error as a value when a copy fails" , async ( ) => {
234
+ // Using empty onError callback to silence any possible act warnings from
235
+ // Snackbar state transitions that you might get if the hook uses the
236
+ // default
235
237
const textToCopy = "hamster" ;
236
- const { result } = renderUseClipboard ( { textToCopy, onError } ) ;
238
+ const { result } = renderUseClipboard ( { textToCopy, onError : jest . fn ( ) } ) ;
237
239
238
240
setSimulateFailure ( true ) ;
239
241
await act ( ( ) => result . current . copyToClipboard ( ) ) ;
0 commit comments