-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-channel-panel.test.tsx
More file actions
138 lines (125 loc) · 4.24 KB
/
command-channel-panel.test.tsx
File metadata and controls
138 lines (125 loc) · 4.24 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
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom/vitest'
import { CommandChannelPanel } from '../components/CommandChannelPanel.js'
import { httpsCallable } from 'firebase/functions'
const { mockOnSnapshot, mockHttpsCallable, mockGetFunctions, mockDb } = vi.hoisted(() => ({
mockOnSnapshot: vi.fn(),
mockHttpsCallable: vi.fn(),
mockGetFunctions: vi.fn(() => ({})),
mockDb: {},
}))
interface MockQueryRef {
type: string
filters: { type: string; field?: string }[]
}
vi.mock('firebase/firestore', () => {
const collection = vi.fn((_db, name) => ({ type: 'collection', name }))
const where = vi.fn((field, _op, value) => ({ type: 'where', field, value }))
const orderBy = vi.fn((field, dir) => ({ type: 'orderBy', field, dir }))
const limit = vi.fn((n) => ({ type: 'limit', n }))
const query = vi.fn((...args) => ({ type: 'query', filters: args.slice(1) }))
return {
getFirestore: vi.fn(() => mockDb),
collection,
query,
where,
orderBy,
limit,
onSnapshot: mockOnSnapshot,
doc: vi.fn(),
}
})
vi.mock('firebase/functions', () => ({
httpsCallable: vi.fn(() => mockHttpsCallable),
getFunctions: mockGetFunctions,
}))
const threadSnap = {
docs: [
{
id: 'th1',
data: () => ({
threadType: 'agency_assistance',
subject: 'Test thread',
participantUids: { u1: true, u2: true },
lastMessageAt: 1000,
}),
},
],
empty: false,
}
const msgSnap = {
docs: [
{
id: 'm1',
data: () => ({
authorUid: 'u2',
body: 'Hello world',
createdAt: 1000,
}),
},
],
empty: false,
}
beforeEach(() => {
mockOnSnapshot.mockReset()
mockHttpsCallable.mockReset()
mockHttpsCallable.mockResolvedValue({ data: { status: 'sent' } })
mockOnSnapshot.mockImplementation((ref: MockQueryRef, cb) => {
if (ref.type === 'query') {
const isMessages = ref.filters.some((f) => f.type === 'where' && f.field === 'threadId')
cb(isMessages ? msgSnap : threadSnap)
}
return vi.fn()
})
})
describe('CommandChannelPanel', () => {
it('renders nothing when no threads', () => {
mockOnSnapshot.mockImplementation((ref: MockQueryRef, cb) => {
if (ref.type === 'query') {
cb({ docs: [], empty: true })
}
return vi.fn()
})
const { container } = render(<CommandChannelPanel reportId="r1" currentUserUid="u1" />)
expect(container.firstChild).toBeNull()
})
it('renders thread tabs and messages', () => {
render(<CommandChannelPanel reportId="r1" currentUserUid="u1" />)
expect(screen.getByText('🏥 Agency')).toBeInTheDocument()
expect(screen.getByText('Test thread')).toBeInTheDocument()
expect(screen.getByText('Hello world')).toBeInTheDocument()
})
it('sends a message when clicking Send', async () => {
render(<CommandChannelPanel reportId="r1" currentUserUid="u1" />)
const textarea = screen.getByPlaceholderText('Type a message...')
fireEvent.change(textarea, { target: { value: 'Units dispatched' } })
fireEvent.click(screen.getByText('Send'))
await waitFor(() => {
expect(httpsCallable).toHaveBeenCalledWith(expect.any(Object), 'addCommandChannelMessage')
expect(mockHttpsCallable).toHaveBeenCalledWith(
expect.objectContaining({
threadId: 'th1',
body: 'Units dispatched',
idempotencyKey: expect.any(String),
}),
)
})
expect(textarea).toHaveValue('')
})
it('displays an error when send fails', async () => {
mockHttpsCallable.mockRejectedValue(new Error('Network error'))
render(<CommandChannelPanel reportId="r1" currentUserUid="u1" />)
const textarea = screen.getByPlaceholderText('Type a message...')
fireEvent.change(textarea, { target: { value: 'Fail me' } })
fireEvent.click(screen.getByText('Send'))
await waitFor(() => {
expect(screen.getByText('Network error')).toBeInTheDocument()
})
})
it('enforces max length of 2000', () => {
render(<CommandChannelPanel reportId="r1" currentUserUid="u1" />)
const textarea = screen.getByPlaceholderText('Type a message...')
expect(textarea).toHaveAttribute('maxLength', '2000')
})
})