Skip to content

fix: update tests for useClipboard to minimize risks of flakes #13250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 15, 2024
Merged
Prev Previous commit
Next Next commit
docs: add more clarifying comments
  • Loading branch information
Parkreiner committed May 15, 2024
commit f396df404a21383a40ed5ad3040f0d02e4c42d55
8 changes: 7 additions & 1 deletion site/src/hooks/useClipboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {

beforeEach(() => {
jest.useFakeTimers();

// Can't use jest.spyOn here because there's no guarantee that the mock
// browser environment actually implements execCommand. Trying to spy on an
// undefined value will throw an error
global.document.execCommand = mockExecCommand;

jest.spyOn(window, "navigator", "get").mockImplementation(() => ({
Expand All @@ -172,9 +176,11 @@ describe.each(secureContextValues)("useClipboard - secure: %j", (isSecure) => {
jest.runAllTimers();
jest.useRealTimers();
jest.resetAllMocks();
global.document.execCommand = originalExecCommand;

// Still have to reset the mock clipboard state because the same mock values
// are reused for each test case in a given describe.each iteration
resetMockClipboardState();
global.document.execCommand = originalExecCommand;
});

const assertClipboardUpdateLifecycle = async (
Expand Down