Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions src/apps/golf/components/PermalinkDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,9 @@ const PermalinkDisplay = ({ label, url, onCopy }: PermalinkDisplayProps) => {
setCopyStatus('copying')

try {
// Try modern Clipboard API first
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(url)
setCopyStatus('success')
onCopy?.()
} else {
// Fallback method for older browsers or non-secure contexts
const textArea = document.createElement('textarea')
textArea.value = url
textArea.style.position = 'fixed'
textArea.style.left = '-999999px'
textArea.style.top = '-999999px'
document.body.appendChild(textArea)
textArea.focus()
textArea.select()

const successful = document.execCommand('copy')
document.body.removeChild(textArea)

if (successful) {
setCopyStatus('success')
onCopy?.()
} else {
throw new Error('Fallback copy method failed')
}
}
await navigator.clipboard.writeText(url)
setCopyStatus('success')
onCopy?.()
} catch (error) {
console.error('Failed to copy permalink:', error)
setCopyStatus('error')
Expand Down
86 changes: 0 additions & 86 deletions src/apps/golf/components/__tests__/PermalinkDisplay.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const mockClipboard = {
writeText: vi.fn()
}

// Mock document.execCommand
const mockExecCommand = vi.fn()

describe('PermalinkDisplay', () => {
const defaultProps = {
label: 'Share Room',
Expand All @@ -31,12 +28,6 @@ describe('PermalinkDisplay', () => {
value: true,
writable: true
})

// Mock document.execCommand
Object.defineProperty(document, 'execCommand', {
value: mockExecCommand,
writable: true
})
})

afterEach(() => {
Expand Down Expand Up @@ -128,83 +119,6 @@ describe('PermalinkDisplay', () => {
})
})

describe('Copy Functionality - Fallback Method', () => {
it('attempts fallback method when clipboard API is not available', async () => {
// Simulate no clipboard API or insecure context
Object.defineProperty(navigator, 'clipboard', {
value: undefined,
writable: true
})
Object.defineProperty(window, 'isSecureContext', {
value: false,
writable: true
})

mockExecCommand.mockReturnValue(true)

render(<PermalinkDisplay {...defaultProps} />)

const copyButton = screen.getByRole('button', { name: /copy share room/i })
fireEvent.click(copyButton)

// Should attempt to use execCommand
await waitFor(() => {
expect(mockExecCommand).toHaveBeenCalledWith('copy')
})

await waitFor(() => {
expect(copyButton).toHaveTextContent('Copied!')
})
})

it('handles fallback method failure', async () => {
// Simulate no clipboard API or insecure context
Object.defineProperty(navigator, 'clipboard', {
value: undefined,
writable: true
})
Object.defineProperty(window, 'isSecureContext', {
value: false,
writable: true
})

mockExecCommand.mockReturnValue(false)

render(<PermalinkDisplay {...defaultProps} />)

const copyButton = screen.getByRole('button', { name: /copy share room/i })
fireEvent.click(copyButton)

await waitFor(() => {
expect(copyButton).toHaveTextContent('Failed')
})
})

it('calls onCopy callback when fallback succeeds', async () => {
// Simulate no clipboard API or insecure context
Object.defineProperty(navigator, 'clipboard', {
value: undefined,
writable: true
})
Object.defineProperty(window, 'isSecureContext', {
value: false,
writable: true
})

const onCopy = vi.fn()
mockExecCommand.mockReturnValue(true)

render(<PermalinkDisplay {...defaultProps} onCopy={onCopy} />)

const copyButton = screen.getByRole('button', { name: /copy share room/i })
fireEvent.click(copyButton)

await waitFor(() => {
expect(onCopy).toHaveBeenCalled()
})
})
})

describe('Button States and Interactions', () => {
it('disables button while copying', async () => {
mockClipboard.writeText.mockImplementation(() => new Promise(resolve => setTimeout(resolve, 100)))
Expand Down