-
Notifications
You must be signed in to change notification settings - Fork 1
MM-70344: Confirm the copy in the Share modal and Space info #39
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
Open
nang2049
wants to merge
2
commits into
master
Choose a base branch
from
MM-70344-copy-link-feedback
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| import {act, fireEvent, screen} from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import {copyToClipboard} from 'utils/clipboard'; | ||
|
|
||
| import {makeSpace} from 'store/test_fixtures'; | ||
|
|
||
| import SpaceInfoMenu from './space_info_menu'; | ||
|
|
||
| import {renderWithContext} from '../../../tests/react_testing_utils'; | ||
|
|
||
| jest.mock('hooks/navigation', () => ({ | ||
| useDocsNavigation: () => ({paths: {space: (id: string) => `/team/spaces/${id}`}}), | ||
| })); | ||
|
|
||
| jest.mock('hooks/permissions', () => ({ | ||
| useCanManageSpaceMembers: () => true, | ||
| })); | ||
|
|
||
| jest.mock('utils/clipboard', () => ({copyToClipboard: jest.fn(() => Promise.resolve(true))})); | ||
|
|
||
| const space = makeSpace('space-1', 'Engineering'); | ||
|
|
||
| const renderMenu = () => renderWithContext( | ||
| <SpaceInfoMenu | ||
| space={space} | ||
| memberCount={3} | ||
| onShowMembers={jest.fn()} | ||
| />, | ||
| ); | ||
|
|
||
| describe('SpaceInfoMenu', () => { | ||
| beforeEach(() => jest.clearAllMocks()); | ||
|
|
||
| it('copies the space link', async () => { | ||
| renderMenu(); | ||
|
|
||
| await act(async () => { | ||
| fireEvent.click(screen.getByRole('button', {name: 'Copy link'})); | ||
| }); | ||
|
|
||
| expect(copyToClipboard).toHaveBeenCalledWith('/team/spaces/space-1'); | ||
| }); | ||
|
|
||
| // MM-70344: the click gave no sign that anything happened. | ||
| it('confirms the copy on the item itself', async () => { | ||
| renderMenu(); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', {name: 'Copy link'})); | ||
|
|
||
| expect(await screen.findByRole('button', {name: 'Copied'})).toBeInTheDocument(); | ||
| expect(screen.queryByRole('button', {name: 'Copy link'})).not.toBeInTheDocument(); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| import {act, renderHook} from '@testing-library/react'; | ||
| import {copyToClipboard} from 'utils/clipboard'; | ||
|
|
||
| import {clearReadout, getReadoutMessage} from 'components/readout/readout_store'; | ||
|
|
||
| import {useCopyText} from './copy_text'; | ||
|
|
||
| jest.mock('utils/clipboard', () => ({copyToClipboard: jest.fn()})); | ||
|
|
||
| const mockCopy = copyToClipboard as jest.MockedFunction<typeof copyToClipboard>; | ||
|
|
||
| describe('useCopyText', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| jest.clearAllMocks(); | ||
| mockCopy.mockResolvedValue(true); | ||
| clearReadout(); | ||
| }); | ||
|
|
||
| afterEach(() => jest.useRealTimers()); | ||
|
|
||
| it('copies and confirms for a moment', async () => { | ||
| const {result} = renderHook(() => useCopyText('https://example.com/space')); | ||
|
|
||
| expect(result.current.copied).toBe(false); | ||
|
|
||
| await act(async () => result.current.copy()); | ||
|
|
||
| expect(mockCopy).toHaveBeenCalledWith('https://example.com/space'); | ||
| expect(result.current.copied).toBe(true); | ||
|
|
||
| act(() => jest.advanceTimersByTime(2000)); | ||
|
|
||
| expect(result.current.copied).toBe(false); | ||
| }); | ||
|
|
||
| // The clipboard write can fail on permissions or an insecure context, and | ||
| // confirming a copy that never happened is worse than staying quiet. | ||
| it('says nothing when the copy fails', async () => { | ||
| mockCopy.mockResolvedValue(false); | ||
|
|
||
| const {result} = renderHook(() => useCopyText('link', {announcement: 'Copied'})); | ||
|
|
||
| await act(async () => result.current.copy()); | ||
|
|
||
| expect(result.current.copied).toBe(false); | ||
| expect(getReadoutMessage()).toBe(''); | ||
| }); | ||
|
|
||
| it('announces the confirmation through the live region', async () => { | ||
| const {result} = renderHook(() => useCopyText('link', {announcement: 'Copied'})); | ||
|
|
||
| await act(async () => result.current.copy()); | ||
|
|
||
| expect(getReadoutMessage()).toBe('Copied'); | ||
| }); | ||
|
|
||
| it('holds the confirmation open when copied again', async () => { | ||
| const {result} = renderHook(() => useCopyText('link')); | ||
|
|
||
| await act(async () => result.current.copy()); | ||
| act(() => jest.advanceTimersByTime(1500)); | ||
| await act(async () => result.current.copy()); | ||
| act(() => jest.advanceTimersByTime(1500)); | ||
|
|
||
| expect(result.current.copied).toBe(true); | ||
| }); | ||
| }); |
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,59 @@ | ||
| // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| import {useCallback, useEffect, useRef, useState} from 'react'; | ||
| import {copyToClipboard} from 'utils/clipboard'; | ||
|
|
||
| import {announce} from 'components/readout/readout_store'; | ||
|
|
||
| const COPIED_TIMEOUT = 2000; | ||
|
|
||
| type CopyOptions = { | ||
| announcement?: string; | ||
| timeout?: number; | ||
| }; | ||
|
|
||
| type CopyText = { | ||
|
|
||
| /** True for a moment after a copy, for controls that confirm in place. */ | ||
| copied: boolean; | ||
| copy: () => void; | ||
| }; | ||
|
|
||
| // Core's useCopyText, which plugins can't import. Controls own the confirmation: | ||
| // core swaps the label and icon rather than raising a toast. | ||
| export function useCopyText(text: string, {announcement, timeout = COPIED_TIMEOUT}: CopyOptions = {}): CopyText { | ||
| const [copied, setCopied] = useState(false); | ||
| const timer = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
| const mounted = useRef(true); | ||
|
|
||
| useEffect(() => { | ||
| mounted.current = true; | ||
|
|
||
| return () => { | ||
| mounted.current = false; | ||
| if (timer.current) { | ||
| clearTimeout(timer.current); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| const copy = useCallback(async () => { | ||
| const done = await copyToClipboard(text); | ||
| if (!done || !mounted.current) { | ||
| return; | ||
| } | ||
|
|
||
| if (announcement) { | ||
| announce(announcement); | ||
| } | ||
|
|
||
| if (timer.current) { | ||
| clearTimeout(timer.current); | ||
| } | ||
| setCopied(true); | ||
| timer.current = setTimeout(() => setCopied(false), timeout); | ||
| }, [announcement, text, timeout]); | ||
|
|
||
| return {copied, copy}; | ||
| } |
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.
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.