-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathcommunityPrivateChat.test.tsx
More file actions
181 lines (166 loc) · 5.42 KB
/
Copy pathcommunityPrivateChat.test.tsx
File metadata and controls
181 lines (166 loc) · 5.42 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
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {render} from 'solid-js/web';
import '@helpers/peerIdPolyfill';
const mocks = vi.hoisted(() => ({
peers: {} as Record<PeerId, any>,
popupFactory: undefined as (() => any) | undefined,
setInnerPeer: vi.fn()
}));
vi.mock('@lib/apiManagerProxy', () => ({
default: {
getPeer: (peerId: PeerId) => mocks.peers[peerId]
}
}));
vi.mock('@lib/appImManager', () => ({
default: {
setInnerPeer: mocks.setInnerPeer
}
}));
vi.mock('@lib/langPack', () => ({
i18n: (key: string, args?: unknown[]) => {
return args?.length ? `${key}:${args.join(',')}` : key;
}
}));
vi.mock('@components/avatarNew', () => ({
AvatarNewTsx: (props: any) => (
<span data-testid="private-chat-avatar" data-peer-id={props.peerId} />
)
}));
vi.mock('@components/iconTsx', () => ({
IconTsx: (props: any) => (
<span class={props.class} data-icon={props.icon} />
)
}));
vi.mock('@components/peerTitleTsx', () => ({
PeerTitleTsx: () => <span>Private Channel</span>
}));
vi.mock('@components/mediaHeader', () => {
const MediaHeader = (props: any) => (
<div data-testid="media-header">{props.children}</div>
);
MediaHeader.Sticker = (props: any) => (
<div data-testid="media-header-sticker">{props.element}</div>
);
MediaHeader.Title = (props: any) => (
<div data-testid="media-header-title">{props.children}</div>
);
MediaHeader.Subtitle = (props: any) => (
<div data-testid="media-header-subtitle">{props.children}</div>
);
return {default: MediaHeader};
});
vi.mock('@components/popups/indexTsx', () => {
const PopupElement = (props: any) => <div>{props.children}</div>;
PopupElement.Header = (props: any) => <div>{props.children}</div>;
PopupElement.CloseButton = () => <button data-testid="close" />;
PopupElement.Body = (props: any) => <div>{props.children}</div>;
PopupElement.Footer = (props: any) => <div>{props.children}</div>;
PopupElement.FooterButton = (props: any) => (
<button
data-testid="footer-button"
data-cancel={String(!!props.cancel)}
data-color={props.color || 'primary'}
disabled={props.disabled}
onClick={props.callback}
>
{props.children || props.langKey}
</button>
);
return {
default: PopupElement,
createPopup: (factory: () => any) => {
mocks.popupFactory = factory;
}
};
});
import showCommunityPrivateChat from '@components/popups/communityPrivateChat';
const channelId = 10 as ChatId;
const peerId = channelId.toPeerId(true);
describe('Community private chat sheet', () => {
let dispose: () => void;
beforeEach(() => {
mocks.popupFactory = undefined;
mocks.setInnerPeer.mockClear();
mocks.peers = {
[peerId]: {
_: 'channel',
id: channelId,
pFlags: {broadcast: true}
}
};
});
afterEach(() => {
dispose?.();
document.body.replaceChildren();
});
it('shows an accessible channel with Message Owner and Open Channel', () => {
showCommunityPrivateChat({
peerId,
requestedByPeerId: 20 as PeerId,
memberCount: 42,
canOpenChat: true
});
const container = document.createElement('div');
document.body.append(container);
dispose = render(
() => mocks.popupFactory!(),
container
);
expect(container.textContent).toContain('Subscribers:42');
expect(container.querySelector('[data-testid="media-header"]')).not.toBeNull();
expect(container.querySelector('[data-testid="media-header-sticker"]'))
.not.toBeNull();
const buttons = [...container.querySelectorAll<HTMLButtonElement>(
'[data-testid="footer-button"]'
)];
expect(buttons.map((button) => button.textContent)).toEqual([
'OpenChannel2',
'Community.MessageChannelOwner'
]);
expect(buttons[0].dataset.color).toBe('secondary');
expect(buttons.some((button) => button.dataset.cancel === 'true'))
.toBe(false);
expect(container.textContent).toContain(
'Community.PrivateChannelInfo'
);
expect(container.textContent).not.toContain(
'Community.SuggestedChannel'
);
const infoIcon = container.querySelector('[data-icon="eye2_filled"]');
expect(infoIcon).not.toBeNull();
expect(infoIcon.classList).toContain('inline-icon');
expect(infoIcon.classList).toContain('inline-icon-left');
buttons[0].click();
expect(mocks.setInnerPeer).toHaveBeenCalledWith({peerId});
});
it('uses Open Chat for a group', () => {
mocks.peers[peerId].pFlags = {};
showCommunityPrivateChat({
peerId,
requestedByPeerId: 20 as PeerId,
canOpenChat: true
});
const container = document.createElement('div');
document.body.append(container);
dispose = render(() => mocks.popupFactory!(), container);
expect(container.querySelector<HTMLButtonElement>(
'[data-testid="footer-button"]'
).textContent).toBe('OpenChat');
});
it('does not offer Open Chat for an inaccessible request', () => {
showCommunityPrivateChat({
peerId,
requestedByPeerId: 20 as PeerId,
memberCount: 42,
canOpenChat: false
});
const container = document.createElement('div');
document.body.append(container);
dispose = render(() => mocks.popupFactory!(), container);
expect([...container.querySelectorAll<HTMLButtonElement>(
'[data-testid="footer-button"]'
)].map((button) => button.textContent)).toEqual([
'Community.MessageChannelOwner'
]);
});
});