-
Notifications
You must be signed in to change notification settings - Fork 518
Expand file tree
/
Copy pathcopy-button.test.ts
More file actions
157 lines (129 loc) · 4.47 KB
/
copy-button.test.ts
File metadata and controls
157 lines (129 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { createMockTimers } from '@codebuff/common/testing/mocks/timers'
import { describe, test, expect, beforeEach, afterEach } from 'bun:test'
import {
getCopyIconText,
copyButtonHandlers,
COPIED_RESET_DELAY_MS,
COPY_ICON_COLLAPSED,
COPY_ICON_EXPANDED,
COPY_ICON_COPIED,
} from '../../components/copy-button'
import { initializeThemeStore } from '../../hooks/use-theme'
import type { MockTimers } from '@codebuff/common/testing/mocks/timers'
// Initialize theme before tests
initializeThemeStore()
/**
* Tests for CopyButton component logic.
*
* These tests use the exported utilities from copy-button.tsx:
* - getCopyIconText: determines what text to display
* - copyButtonHandlers: pure functions for state transitions
* - COPIED_RESET_DELAY_MS: the timeout constant
*/
describe('CopyButton - CopyIcon text rendering', () => {
describe('with leadingSpace=true (default)', () => {
test('renders collapsed icon when not hovered or copied', () => {
const text = getCopyIconText(false, false, true)
expect(text).toBe(' ⎘')
})
test('renders expanded text when hovered', () => {
const text = getCopyIconText(false, true, true)
expect(text).toBe(' [⎘ copy]')
})
test('renders copied text when copied', () => {
const text = getCopyIconText(true, false, true)
expect(text).toBe(' [✔ copied]')
})
test('renders copied text even when hovered (copied takes priority)', () => {
const text = getCopyIconText(true, true, true)
expect(text).toBe(' [✔ copied]')
})
})
describe('with leadingSpace=false', () => {
test('renders collapsed icon without leading space', () => {
const text = getCopyIconText(false, false, false)
expect(text).toBe('⎘')
})
test('renders expanded text without leading space', () => {
const text = getCopyIconText(false, true, false)
expect(text).toBe('[⎘ copy]')
})
test('renders copied text without leading space', () => {
const text = getCopyIconText(true, false, false)
expect(text).toBe('[✔ copied]')
})
})
})
describe('CopyButton - copyButtonHandlers (from component)', () => {
describe('handleMouseOver', () => {
test('returns true (should hover) when not copied', () => {
expect(copyButtonHandlers.handleMouseOver(false)).toBe(true)
})
test('returns false (block hover) when copied', () => {
expect(copyButtonHandlers.handleMouseOver(true)).toBe(false)
})
})
describe('handleMouseOut', () => {
test('always returns false to clear hover', () => {
expect(copyButtonHandlers.handleMouseOut()).toBe(false)
})
})
describe('handleCopy', () => {
test('returns copied=true and clears hover', () => {
const result = copyButtonHandlers.handleCopy()
expect(result).toEqual({ isCopied: true, isHovered: false })
})
})
})
describe('CopyButton - exported constants', () => {
test('COPIED_RESET_DELAY_MS is 2000ms', () => {
expect(COPIED_RESET_DELAY_MS).toBe(2000)
})
test('icon constants are defined', () => {
expect(COPY_ICON_COLLAPSED).toBe('⎘')
expect(COPY_ICON_EXPANDED).toBe('[⎘ copy]')
expect(COPY_ICON_COPIED).toBe('[✔ copied]')
})
})
describe('CopyButton - copied state reset timing', () => {
let mockTimers: MockTimers
beforeEach(() => {
mockTimers = createMockTimers()
mockTimers.install()
})
afterEach(() => {
mockTimers.restore()
})
test('uses the exported COPIED_RESET_DELAY_MS constant (2000ms)', () => {
let isCopied = false
// Simulate handleCopy using the exported constant
const handleCopy = () => {
const newState = copyButtonHandlers.handleCopy()
isCopied = newState.isCopied
setTimeout(() => {
isCopied = false
}, COPIED_RESET_DELAY_MS)
}
handleCopy()
expect(isCopied).toBe(true)
expect(mockTimers.getPendingCount()).toBe(1)
const nextTimer = mockTimers.getNext()
expect(nextTimer?.ms).toBe(COPIED_RESET_DELAY_MS)
mockTimers.runAll()
expect(isCopied).toBe(false)
})
test('multiple rapid clicks only create one active timer', () => {
let currentTimerId: number | null = null
const handleCopy = () => {
if (currentTimerId !== null) {
clearTimeout(currentTimerId)
}
copyButtonHandlers.handleCopy()
currentTimerId = setTimeout(() => {}, COPIED_RESET_DELAY_MS) as unknown as number
}
handleCopy()
handleCopy()
handleCopy()
expect(mockTimers.getPendingCount()).toBe(1)
})
})