Skip to content

Commit caf1222

Browse files
committed
chore: add extra test case for exposed error value
1 parent 8dfe422 commit caf1222

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

site/src/hooks/useClipboard.test.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,32 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {
212212
expect(onError).toBeCalled();
213213
});
214214

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 () => {
216216
const textToCopy = "crow";
217217
const { result } = renderUseClipboard({ textToCopy });
218218

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+
*/
219224
setSimulateFailure(true);
220225
await act(() => result.current.copyToClipboard());
221226

222227
const errorMessageNode = screen.queryByText(COPY_FAILED_MESSAGE);
223228
expect(errorMessageNode).not.toBeNull();
224229
});
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+
});
225243
});

0 commit comments

Comments
 (0)