Skip to content

Commit

Permalink
Merge branch 'feat/migrate-JsonLink' of https://github.com/Ve33y/node…
Browse files Browse the repository at this point in the history
…js.org into feat/migrate-JsonLink
  • Loading branch information
Ve33y committed May 12, 2023
2 parents 2209452 + f843440 commit 251c5dc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Thank you for your interest in contributing to the Node.js Website. Before you p

```bash
git clone git@github.com:<GITHUB_ID>/nodejs.org.git # SSH
gh repo clone <GITHUB_ID>/nodejs.org # GitHub CLI
git clone https://github.com/<GITHUB_ID>/nodejs.org.git # HTTPS
gh repo clone <GITHUB_ID>/nodejs.org # GitHub CLI
```

3. Change into the nodejs.org directory.
Expand All @@ -36,7 +36,9 @@ cd nodejs.org
4. Create a remote for keeping your fork as well as your local clone up-to-date.

```bash
git remote add upstream git@github.com:nodejs/nodejs.org.git
git remote add upstream git@github.com:nodejs/nodejs.org.git # SSH
git remote add upstream https://github.com/nodejs/nodejs.org.git # HTTPS
gh repo sync nodejs/nodejs.org # GitHub CLI
```

5. Create a new branch for your work.
Expand Down
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 251c5dc

Please sign in to comment.