-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathThreadHeader.test.js
More file actions
186 lines (154 loc) · 5.01 KB
/
ThreadHeader.test.js
File metadata and controls
186 lines (154 loc) · 5.01 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import '@testing-library/jest-dom';
import { cleanup, render, screen } from '@testing-library/react';
import React from 'react';
import { ChannelStateProvider } from '../../../context/ChannelStateContext';
import { ChatProvider } from '../../../context/ChatContext';
import { TranslationProvider } from '../../../context/TranslationContext';
import { ThreadHeader } from '../ThreadHeader';
jest.mock('../../ChannelPreview/hooks/useChannelPreviewInfo', () => ({
useChannelPreviewInfo: jest.fn(() => ({ displayTitle: undefined })),
}));
jest.mock('../../../store', () => ({
useStateStore: jest.fn(() => undefined),
}));
jest.mock('../../../context/TypingContext', () => ({
useTypingContext: jest.fn(() => ({ typing: {} })),
}));
jest.mock('../../TypingIndicator/TypingIndicatorHeader', () => ({
TypingIndicatorHeader: () => <div>Typing...</div>,
}));
jest.mock('../../Button/ToggleSidebarButton', () => ({
ToggleSidebarButton: ({ children }) => (
<div data-testid='toggle-sidebar-button'>{children}</div>
),
}));
jest.mock('../../Threads', () => ({
useThreadContext: jest.fn(() => undefined),
}));
jest.mock('../../ChatView', () => ({
useChatViewContext: jest.fn(() => ({ activeChatView: 'channels' })),
}));
const {
useChannelPreviewInfo,
} = require('../../ChannelPreview/hooks/useChannelPreviewInfo');
const { useChatViewContext } = require('../../ChatView');
const { useThreadContext } = require('../../Threads');
const alice = { id: 'alice', name: 'Alice' };
const bob = { id: 'bob', name: 'Bob' };
const createThread = (user) => ({
id: `${user?.id ?? 'thread'}-message`,
reply_count: 2,
user,
});
const createChannel = (overrides = {}) => ({
data: undefined,
getClient: () => ({ userID: alice.id }),
state: {
members: {
[alice.id]: { user: alice },
[bob.id]: { user: bob },
},
},
...overrides,
});
const renderComponent = ({
activeChatView = 'channels',
channelOverrides = {},
props = {},
threadContext = undefined,
} = {}) => {
const client = { off: jest.fn(), on: jest.fn(), user: alice, userID: alice.id };
const thread = createThread(alice);
const channel = createChannel(channelOverrides);
useChatViewContext.mockReturnValue({
activeChatView,
setActiveChatView: jest.fn(),
});
useThreadContext.mockReturnValue(threadContext);
return render(
<ChatProvider
value={{
client,
closeMobileNav: jest.fn(),
latestMessageDatesByChannels: {},
navOpen: false,
openMobileNav: jest.fn(),
}}
>
<ChannelStateProvider value={{ channel, thread }}>
<TranslationProvider
value={{
t: (key, options) => {
if (key === 'Thread') return 'Thread';
if (key === 'replyCount') return `${options.count} replies`;
if (key === 'aria/Close thread') return 'Close thread';
return key;
},
}}
>
<ThreadHeader closeThread={jest.fn()} thread={thread} {...props} />
</TranslationProvider>
</ChannelStateProvider>
</ChatProvider>,
);
};
describe('ThreadHeader', () => {
afterEach(() => {
cleanup();
jest.clearAllMocks();
});
it('renders the channel display title in the subtitle', () => {
useChannelPreviewInfo.mockReturnValue({ displayTitle: 'Bob' });
renderComponent();
expect(screen.getByText('Bob · 2 replies')).toBeInTheDocument();
});
it('falls back to the parent message author when the channel has no display title', () => {
useChannelPreviewInfo.mockReturnValue({ displayTitle: undefined });
renderComponent({
channelOverrides: {
state: {
members: {
[alice.id]: { user: alice },
},
},
},
props: {
thread: createThread(alice),
},
});
expect(screen.getByText('Alice · 2 replies')).toBeInTheDocument();
});
it('renders only the reply count when no title source is available', () => {
useChannelPreviewInfo.mockReturnValue({ displayTitle: undefined });
renderComponent({
channelOverrides: {
state: {
members: {
[alice.id]: { user: alice },
},
},
},
props: {
thread: createThread({ id: 'alice' }),
},
});
expect(screen.getByText('2 replies')).toBeInTheDocument();
expect(screen.queryByText(/^undefined ·/)).not.toBeInTheDocument();
});
it('does not render the sidebar toggle in the channels view', () => {
useChannelPreviewInfo.mockReturnValue({ displayTitle: 'Bob' });
renderComponent({
activeChatView: 'channels',
threadContext: { id: 'thread-1' },
});
expect(screen.queryByTestId('toggle-sidebar-button')).not.toBeInTheDocument();
});
it('renders the sidebar toggle in the threads view', () => {
useChannelPreviewInfo.mockReturnValue({ displayTitle: 'Bob' });
renderComponent({
activeChatView: 'threads',
threadContext: { id: 'thread-1' },
});
expect(screen.getByTestId('toggle-sidebar-button')).toBeInTheDocument();
});
});