@estiva-app/ui 0.75.0 → 0.76.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,265 @@
1
+ // @vitest-environment jsdom
2
+ /**
3
+ * The shared comment box (COMM-2): what its page says it owns — the keys,
4
+ * sending held or shown at once, a refusal putting the words back, the edit
5
+ * mode, drafts, files and the read-only line.
6
+ */
7
+ import { act } from 'react'
8
+ import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
9
+ import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'
10
+ import type { Editor } from '@tiptap/core'
11
+ import type { JSONContent } from '@tiptap/react'
12
+ import { CommentBox, commentBoxRows, type CommentBoxDraft, type CommentBoxEdit, type CommentBoxProps, type SendAnswer, type UploadAnswer } from './CommentBox'
13
+ import type { MentionPerson } from './ComposerTriggers'
14
+
15
+ const PEOPLE: MentionPerson[] = [{ id: 'one', name: 'Person one', pubkey: 'b'.repeat(64) }]
16
+ const INSERT = [commentBoxRows.file, commentBoxRows.mention, commentBoxRows.urgent, commentBoxRows.reference]
17
+
18
+ beforeAll(() => {
19
+ Element.prototype.scrollIntoView = vi.fn()
20
+ Element.prototype.getClientRects = () => [] as unknown as DOMRectList
21
+ Range.prototype.getClientRects = () => [] as unknown as DOMRectList
22
+ Range.prototype.getBoundingClientRect = () => new DOMRect()
23
+ ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
24
+ // Only the clock: a list closing gives Enter 100ms of grace (suggestionPopup), which must not reach the next test.
25
+ vi.useFakeTimers({ toFake: ['Date'] })
26
+ })
27
+
28
+ afterEach(() => {
29
+ cleanup()
30
+ vi.setSystemTime(Date.now() + 1000)
31
+ })
32
+
33
+ /** A promise the test answers. */
34
+ function later<T>() {
35
+ let answer!: (value: T) => void
36
+ const promise = new Promise<T>((resolve) => {
37
+ answer = resolve
38
+ })
39
+ return { promise, answer }
40
+ }
41
+
42
+ const editorOf = (): Editor => (document.querySelector('.ProseMirror') as unknown as { editor: Editor }).editor
43
+
44
+ const textOf = (body: JSONContent): string => (body.text ?? '') + (body.content ?? []).map(textOf).join('')
45
+
46
+ async function type(text: string) {
47
+ await act(async () => {
48
+ editorOf().chain().focus('end').insertContent(text).run()
49
+ })
50
+ }
51
+
52
+ async function press(key: string, init: KeyboardEventInit = {}) {
53
+ await act(async () => {
54
+ const editor = editorOf()
55
+ editor.view.someProp('handleKeyDown', (handle) => handle(editor.view, new KeyboardEvent('keydown', { key, ...init })))
56
+ })
57
+ }
58
+
59
+ /** Lets an answer reach the box: it passes through a few promises on the way. */
60
+ async function settle() {
61
+ await act(async () => {
62
+ for (let i = 0; i < 5; i++) await Promise.resolve()
63
+ })
64
+ }
65
+
66
+ function Box(props: Partial<CommentBoxProps>) {
67
+ return <CommentBox people={PEOPLE} insert={INSERT} label="Write" placeholder="Write…" send={async () => ({ accepted: true })} {...props} />
68
+ }
69
+
70
+ describe('sending, held until the relay answers', () => {
71
+ it('Enter sends the words and the box holds them, shut, until it is accepted; then it is empty', async () => {
72
+ const answer = later<SendAnswer>()
73
+ const send = vi.fn<CommentBoxProps['send']>(() => answer.promise)
74
+ render(<Box send={send} />)
75
+ await type('Hello there')
76
+ await press('Enter')
77
+ expect(send).toHaveBeenCalledTimes(1)
78
+ expect(textOf(send.mock.calls[0][0])).toBe('Hello there')
79
+ expect(screen.getByText('Sending…')).toBeTruthy()
80
+ expect(editorOf().isEditable).toBe(false)
81
+ expect(editorOf().getText()).toBe('Hello there')
82
+ answer.answer({ accepted: true })
83
+ await settle()
84
+ expect(editorOf().getText()).toBe('')
85
+ expect(screen.queryByText('Sending…')).toBeNull()
86
+ })
87
+
88
+ it('a refusal keeps the words in the box and says why under it', async () => {
89
+ render(<Box send={async () => ({ accepted: false, reason: 'rate-limited' })} />)
90
+ await type('Hello there')
91
+ await press('Enter')
92
+ await settle()
93
+ expect(editorOf().getText()).toBe('Hello there')
94
+ expect(screen.getByText('Not sent: rate-limited. Your text is back in the box.')).toBeTruthy()
95
+ expect(editorOf().isEditable).toBe(true)
96
+ })
97
+
98
+ it('Ctrl/Cmd+Enter sends as Enter does; Shift+Enter does not send', async () => {
99
+ const send = vi.fn<CommentBoxProps['send']>(async () => ({ accepted: true }))
100
+ render(<Box send={send} />)
101
+ await type('One')
102
+ await press('Enter', { shiftKey: true })
103
+ expect(send).not.toHaveBeenCalled()
104
+ await press('Enter', { ctrlKey: true })
105
+ expect(send).toHaveBeenCalledTimes(1)
106
+ })
107
+
108
+ it('an empty box sends nothing', async () => {
109
+ const send = vi.fn<CommentBoxProps['send']>(async () => ({ accepted: true }))
110
+ render(<Box send={send} />)
111
+ await type(' ')
112
+ await press('Enter')
113
+ expect(send).not.toHaveBeenCalled()
114
+ })
115
+ })
116
+
117
+ describe('sending, shown at once', () => {
118
+ it('the box empties at once; a refusal puts the words back, says why, and tells the app', async () => {
119
+ const answer = later<SendAnswer>()
120
+ const onSent = vi.fn()
121
+ const onRefused = vi.fn()
122
+ render(<Box showsAtOnce send={() => answer.promise} onSent={onSent} onRefused={onRefused} />)
123
+ await type('Hello there')
124
+ await press('Enter')
125
+ expect(editorOf().getText()).toBe('')
126
+ expect(onSent).toHaveBeenCalledTimes(1)
127
+ answer.answer({ accepted: false, reason: 'rate-limited' })
128
+ await settle()
129
+ expect(editorOf().getText()).toBe('Hello there')
130
+ expect(onRefused).toHaveBeenCalledTimes(1)
131
+ expect(screen.getByText('Not sent: rate-limited. Your text is back in the box.')).toBeTruthy()
132
+ })
133
+ })
134
+
135
+ describe('editing', () => {
136
+ const body: JSONContent = { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Said before' }] }] }
137
+
138
+ it('opens with what it says; Enter saves it, and Escape cancels', async () => {
139
+ const onSave = vi.fn<CommentBoxEdit['onSave']>(async () => ({ accepted: true }))
140
+ const onCancel = vi.fn()
141
+ render(<Box edit={{ body, onSave, onCancel }} />)
142
+ expect(editorOf().getText()).toBe('Said before')
143
+ expect(screen.getByText(/to cancel/).textContent).toBe('Esc to cancel · Enter to save')
144
+ await type(', and now')
145
+ await press('Enter')
146
+ expect(textOf(onSave.mock.calls[0][0])).toBe('Said before, and now')
147
+ await press('Escape')
148
+ expect(onCancel).toHaveBeenCalledTimes(1)
149
+ })
150
+
151
+ it('!@ opens no urgent list in an edit, and @ still does', async () => {
152
+ render(<Box edit={{ body, onSave: async () => ({ accepted: true }), onCancel: () => {} }} />)
153
+ await type(' !@')
154
+ expect(screen.queryByRole('listbox', { name: 'Urgent mention' })).toBeNull()
155
+ await type(' @')
156
+ expect(screen.getByRole('listbox', { name: 'People' })).toBeTruthy()
157
+ await press('Escape')
158
+ })
159
+
160
+ it('Escape right after closing a list cancels the edit', async () => {
161
+ const onCancel = vi.fn()
162
+ render(<Box edit={{ body, onSave: async () => ({ accepted: true }), onCancel }} />)
163
+ await type(' @')
164
+ expect(screen.getByRole('listbox')).toBeTruthy()
165
+ await press('Escape')
166
+ expect(onCancel).not.toHaveBeenCalled()
167
+ await press('Escape')
168
+ expect(onCancel).toHaveBeenCalledTimes(1)
169
+ })
170
+
171
+ it('a refused save keeps the words and says it was not saved', async () => {
172
+ render(<Box edit={{ body, onSave: async () => ({ accepted: false, reason: 'gone' }), onCancel: () => {} }} />)
173
+ await press('Enter')
174
+ await settle()
175
+ expect(screen.getByText('Not saved: gone. Your changes are still in the box.')).toBeTruthy()
176
+ expect(editorOf().getText()).toBe('Said before')
177
+ })
178
+
179
+ it('/ offers no File and no Urgent in an edit', async () => {
180
+ render(<Box upload={async () => ({ accepted: true, value: 1 })} edit={{ body, onSave: async () => ({ accepted: true }), onCancel: () => {} }} />)
181
+ await type(' /')
182
+ const rows = within(screen.getByRole('listbox')).getAllByRole('option').map((o) => o.textContent ?? '')
183
+ expect(rows.some((r) => r.startsWith('Mention'))).toBe(true)
184
+ expect(rows.some((r) => r.startsWith('File') || r.startsWith('Urgent'))).toBe(false)
185
+ // Closed here: the editor is destroyed on a timer, and a list it closes then would hold the next test's Enter.
186
+ await press('Escape')
187
+ })
188
+ })
189
+
190
+ describe('drafts', () => {
191
+ it('keeps what is written, opens with it again, and forgets it once sent', async () => {
192
+ let kept: JSONContent | undefined
193
+ const draft: CommentBoxDraft = { read: () => kept, write: (b) => (kept = b), clear: () => (kept = undefined) }
194
+ const first = render(<Box draft={draft} />)
195
+ await type('Half written')
196
+ first.unmount()
197
+ expect(textOf(kept!)).toBe('Half written')
198
+ const send = vi.fn<CommentBoxProps['send']>(async () => ({ accepted: true }))
199
+ render(<Box draft={draft} send={send} />)
200
+ expect(editorOf().getText()).toBe('Half written')
201
+ await press('Enter')
202
+ await settle()
203
+ expect(send).toHaveBeenCalledTimes(1)
204
+ expect(kept).toBeUndefined()
205
+ })
206
+ })
207
+
208
+ describe('files', () => {
209
+ it('a picked file uploads, Send waits for it, and it goes out with the words', async () => {
210
+ const uploaded = later<UploadAnswer>()
211
+ const send = vi.fn<CommentBoxProps['send']>(async () => ({ accepted: true }))
212
+ render(<Box send={send} upload={() => uploaded.promise} />)
213
+ const input = document.querySelector('input[type=file]') as HTMLInputElement
214
+ await act(async () => {
215
+ fireEvent.change(input, { target: { files: [new File(['x'], 'plan.pdf', { type: 'application/pdf' })] } })
216
+ })
217
+ expect(screen.getByText('Uploading… Send waits for the file.')).toBeTruthy()
218
+ await type('See this')
219
+ await press('Enter')
220
+ expect(send).not.toHaveBeenCalled()
221
+ uploaded.answer({ accepted: true, value: 'the-record' })
222
+ await settle()
223
+ await press('Enter')
224
+ expect(send).toHaveBeenCalledTimes(1)
225
+ expect(send.mock.calls[0][1]).toMatchObject([{ name: 'plan.pdf', state: 'ready', value: 'the-record' }])
226
+ })
227
+
228
+ it('a failed upload says why, and nothing waits for it', async () => {
229
+ render(<Box upload={async () => ({ accepted: false, reason: 'too large' })} />)
230
+ const input = document.querySelector('input[type=file]') as HTMLInputElement
231
+ await act(async () => {
232
+ fireEvent.change(input, { target: { files: [new File(['x'], 'big.mov', { type: 'video/quicktime' })] } })
233
+ })
234
+ await settle()
235
+ expect(screen.getByText('too large')).toBeTruthy()
236
+ expect(screen.queryByText('Uploading… Send waits for the file.')).toBeNull()
237
+ })
238
+
239
+ it('an upload that arrives after the box opened offers File in /', async () => {
240
+ const { rerender } = render(<Box />)
241
+ rerender(<Box upload={async () => ({ accepted: true, value: 1 })} />)
242
+ await type('/')
243
+ const rows = within(screen.getByRole('listbox')).getAllByRole('option').map((o) => o.textContent ?? '')
244
+ expect(rows.some((r) => r.startsWith('File'))).toBe(true)
245
+ await press('Escape')
246
+ })
247
+
248
+ it('no upload: no paperclip and no File row', async () => {
249
+ render(<Box />)
250
+ expect(screen.queryByRole('button', { name: 'Attach a file' })).toBeNull()
251
+ await type('/')
252
+ const rows = within(screen.getByRole('listbox')).getAllByRole('option').map((o) => o.textContent ?? '')
253
+ expect(rows.some((r) => r.startsWith('File'))).toBe(false)
254
+ await press('Escape')
255
+ })
256
+ })
257
+
258
+ describe('read-only', () => {
259
+ it('a line saying why takes the box’s place', () => {
260
+ render(<Box closed={{ reason: 'Archived. It takes no new comments.' }} />)
261
+ expect(screen.getByText('Archived. It takes no new comments.')).toBeTruthy()
262
+ expect(document.querySelector('.ProseMirror')).toBeNull()
263
+ expect(screen.queryByRole('button', { name: 'Send' })).toBeNull()
264
+ })
265
+ })