generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
CCM-12890: copy to clipboard #801
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
01c53ac
CCM-12890: add copy to clipboard button
bhansell1 766d1e2
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 fc2b369
CCM-12890: simplify copying
bhansell1 7d18d61
CCM-12890: move test from next to code location to __test__ location …
bhansell1 85d16b2
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 6959dd9
CCM-12890: self-review
bhansell1 22272a7
CCM-12890: add error state when failing to copy to clipboard
bhansell1 b1bb89d
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 f3922aa
CCM-12890: add component test
bhansell1 7674d73
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 8160fb2
CCM-12890: fix automated test
bhansell1 7fb0919
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 ccb0edc
Merge branch 'main' into feature/CCM-12890_copy-to-clipboard
bhansell1 e10a9f2
CCM-12890: fix grammar
bhansell1 65fbd44
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 ea7566e
Merge branch 'feature/CCM-12890_copy-to-clipboard' of https://github.…
bhansell1 dc7ce0c
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 d0b723d
CCM-12890: clear permissions on test
bhansell1 4188f2d
Merge branch 'main' of https://github.com/NHSDigital/nhs-notify-web-t…
bhansell1 aec1458
CCM-12890: linting
bhansell1 0d98303
Merge branch 'main' into feature/CCM-12890_copy-to-clipboard
bhansell1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
178 changes: 178 additions & 0 deletions
178
frontend/src/__tests__/hooks/use-copy-table-to-clipboard.hook.test.tsx
This file contains hidden or 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,178 @@ | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { useCopyTableToClipboard } from '../../hooks/use-copy-table-to-clipboard.hook'; | ||
|
|
||
| type TestData = { | ||
| name: string; | ||
| id: string; | ||
| value: string; | ||
| }; | ||
|
|
||
| describe('useCopyTableToClipboard', () => { | ||
| let mockClipboardWrite: jest.Mock; | ||
|
|
||
| beforeEach(() => { | ||
| mockClipboardWrite = jest.fn().mockResolvedValue(undefined); | ||
|
|
||
| Object.defineProperty(navigator, 'clipboard', { | ||
| value: { write: mockClipboardWrite }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| global.ClipboardItem = jest.fn( | ||
| (data) => data | ||
| ) as unknown as typeof ClipboardItem; | ||
|
|
||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| act(() => { | ||
| jest.runOnlyPendingTimers(); | ||
| }); | ||
| jest.useRealTimers(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should copy data in both CSV and HTML formats to clipboard', async () => { | ||
nicki-nhs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { result } = renderHook(() => useCopyTableToClipboard<TestData>()); | ||
|
|
||
| const testData: TestData[] = [ | ||
| { name: 'Test "quoted" value', id: 'id-1', value: '100' }, | ||
| { name: '<template test name>', id: 'id & value', value: '200' }, | ||
| ]; | ||
|
|
||
| await act(async () => { | ||
| await result.current.copyToClipboard({ | ||
| data: testData, | ||
| columns: [ | ||
| { key: 'name', header: 'Name' }, | ||
| { key: 'id', header: 'ID' }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| expect(mockClipboardWrite).toHaveBeenCalledTimes(1); | ||
|
|
||
| const [clipboardItem] = mockClipboardWrite.mock.calls[0][0]; | ||
| const csv = clipboardItem['text/plain']; | ||
| const html = clipboardItem['text/html']; | ||
|
|
||
| const expectedCSV = [ | ||
| 'Name,ID', | ||
| '"Test ""quoted"" value","id-1"', | ||
bhansell1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| '"<template test name>","id & value"', | ||
| ].join('\n'); | ||
|
|
||
| expect(csv).toEqual(expectedCSV); | ||
|
|
||
| const expectedHTML = ` | ||
| <table> | ||
| <thead> | ||
| <tr> | ||
| <th>Name</th> | ||
| <th>ID</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| <tr> | ||
| <td>Test "quoted" value</td> | ||
| <td>id-1</td> | ||
| </tr> | ||
| <tr> | ||
| <td><template test name></td> | ||
| <td>id & value</td> | ||
| </tr> | ||
| </tbody> | ||
| </table>` | ||
| .replaceAll(/>\s+</g, '><') | ||
| .trim(); | ||
|
|
||
| expect(html).toEqual(expectedHTML); | ||
|
|
||
| expect(result.current.copied).toBe(true); | ||
| expect(result.current.copyError).toBeNull(); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(5000); | ||
| }); | ||
|
|
||
| expect(result.current.copied).toBe(false); | ||
| }); | ||
|
|
||
| it('should handle clipboard write failures', async () => { | ||
| mockClipboardWrite.mockRejectedValueOnce(new Error('Permission denied')); | ||
|
|
||
| const { result } = renderHook(() => useCopyTableToClipboard<TestData>()); | ||
|
|
||
| await act(async () => { | ||
| await result.current.copyToClipboard({ | ||
| data: [{ name: 'Test', id: 'id-1', value: '100' }], | ||
| columns: [{ key: 'name', header: 'Name' }], | ||
| }); | ||
| }); | ||
|
|
||
| expect(result.current.copyError).toEqual(new Error('Permission denied')); | ||
| expect(result.current.copied).toBe(false); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(5000); | ||
| }); | ||
|
|
||
| expect(result.current.copyError).toBeNull(); | ||
|
|
||
| mockClipboardWrite.mockResolvedValueOnce(undefined); | ||
|
|
||
| await act(async () => { | ||
| await result.current.copyToClipboard({ | ||
| data: [{ name: 'Test', id: 'id-1', value: '100' }], | ||
| columns: [{ key: 'name', header: 'Name' }], | ||
| }); | ||
| }); | ||
|
|
||
| expect(result.current.copyError).toBeNull(); | ||
| expect(result.current.copied).toBe(true); | ||
| }); | ||
|
|
||
| it('should clear previous timeout when copying multiple times', async () => { | ||
| const { result } = renderHook(() => useCopyTableToClipboard<TestData>()); | ||
|
|
||
| const testData: TestData[] = [{ name: 'Test 1', id: 'id-1', value: '100' }]; | ||
|
|
||
| await act(async () => { | ||
| await result.current.copyToClipboard({ | ||
| data: testData, | ||
| columns: [{ key: 'name', header: 'Name' }], | ||
| }); | ||
| }); | ||
|
|
||
| expect(result.current.copied).toBe(true); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(2500); | ||
| }); | ||
|
|
||
| expect(result.current.copied).toBe(true); | ||
|
|
||
| await act(async () => { | ||
| await result.current.copyToClipboard({ | ||
| data: testData, | ||
| columns: [{ key: 'name', header: 'Name' }], | ||
| }); | ||
| }); | ||
|
|
||
| expect(result.current.copied).toBe(true); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(2500); | ||
| }); | ||
|
|
||
| expect(result.current.copied).toBe(true); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(2500); | ||
| }); | ||
|
|
||
| expect(result.current.copied).toBe(false); | ||
| }); | ||
| }); | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.