Skip to content

Commit

Permalink
fix(test): dir name (#5354)
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustinMauroy authored and ovflowd committed May 24, 2023
1 parent db1e8a0 commit b94cb5f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions hooks/__tests__/useCopyToClipboard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { render, fireEvent, screen } from '@testing-library/react';
import { FormattedMessage } from 'react-intl';
import { IntlProvider } from 'react-intl';
import { useCopyToClipboard } from '../useCopyToClipboard';

const mockWriteText = jest.fn();
const originalNavigator = { ...window.navigator };

describe('useCopyToClipboard', () => {
beforeEach(() => {
Object.defineProperty(window, 'navigator', {
value: {
clipboard: {
writeText: mockWriteText,
},
},
});
});

afterEach(() => {
Object.defineProperty(window, 'navigator', {
value: originalNavigator,
});
});

const TestComponent = ({ textToCopy }: { textToCopy: string }) => {
const [copied, copyText] = useCopyToClipboard();

return (
<IntlProvider locale="en" onError={() => {}}>
<button onClick={() => copyText(textToCopy)} type="button">
<FormattedMessage
id="components.common.shellBox.copy"
values={{ copied }}
/>
</button>
</IntlProvider>
);
};

it('should call clipboard API with `test` once', () => {
const navigatorClipboardWriteTextSpy = jest
.fn()
.mockImplementation(() => Promise.resolve());

Object.defineProperty(window.navigator, 'clipboard', {
writable: true,
value: {
writeText: navigatorClipboardWriteTextSpy,
},
});

render(<TestComponent textToCopy="test" />);
const button = screen.getByRole('button');
fireEvent.click(button);
expect(navigatorClipboardWriteTextSpy).toHaveBeenCalledTimes(1);
expect(navigatorClipboardWriteTextSpy).toHaveBeenCalledWith('test');
});
});
1 change: 1 addition & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.mjs'],
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
testMatch: ['**/__tests__/*.test.{,ts,tsx}'],
};

export default createJestConfig(customJestConfig);

0 comments on commit b94cb5f

Please sign in to comment.