-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #743 from JiscSD/OC-977
OC-977: Button to copy DOI
- Loading branch information
Showing
8 changed files
with
122 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as Components from '@/components'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
|
||
Object.assign(navigator, { | ||
clipboard: { | ||
writeText: jest.fn(() => Promise.resolve()) | ||
} | ||
}); | ||
|
||
describe('CopyButton', () => { | ||
const buttonTitle = 'Copy some text'; | ||
const textToCopy = 'Test value'; | ||
|
||
beforeEach(() => { | ||
render(<Components.CopyButton textToCopy={textToCopy} title={buttonTitle} />); | ||
}); | ||
|
||
it('Button is present', () => { | ||
expect(screen.getByRole('button', { name: buttonTitle })).toBeInTheDocument(); | ||
}); | ||
|
||
it('Clicking the button copies the text to the clipboard', async () => { | ||
await userEvent.click(screen.getByRole('button', { name: buttonTitle })); | ||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(textToCopy); | ||
}); | ||
|
||
it('Clicking the button updates the aria-live message', async () => { | ||
expect(screen.queryByText('Copied!')).not.toBeInTheDocument(); | ||
await userEvent.click(screen.getByRole('button', { name: buttonTitle })); | ||
expect(screen.getByText('Copied!')).toBeInTheDocument(); | ||
expect(screen.getByText('Copied!')).toHaveAttribute('aria-live', 'polite'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { useState } from 'react'; | ||
import * as OutlineIcons from '@heroicons/react/24/outline'; | ||
|
||
import * as Components from '@/components'; | ||
import * as Stores from '@/stores'; | ||
|
||
type Props = { | ||
id?: string; | ||
className?: string; | ||
textToCopy: string; | ||
title: string; | ||
}; | ||
|
||
const CopyButton: React.FC<Props> = (props): React.ReactElement => { | ||
const [copied, setCopied] = useState(false); | ||
const setToast = Stores.useToastStore((state) => state.setToast); | ||
|
||
const confirmationMessage = 'Copied!'; | ||
|
||
const handleCopy = () => { | ||
navigator.clipboard.writeText(props.textToCopy).then(() => { | ||
setCopied(true); | ||
setToast({ | ||
visible: true, | ||
title: confirmationMessage, | ||
message: null, | ||
icon: <OutlineIcons.DocumentDuplicateIcon className="h-6 w-6 text-teal-400" />, | ||
dismiss: false | ||
}); | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<span className="sr-only" aria-live="polite"> | ||
{copied && confirmationMessage} | ||
</span> | ||
<Components.IconButton | ||
className={props.className} | ||
icon={<OutlineIcons.DocumentDuplicateIcon className="h-4 w-4" />} | ||
title={props.title} | ||
onClick={handleCopy} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default CopyButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters