-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathcommunityLeave.test.ts
More file actions
137 lines (121 loc) · 3.51 KB
/
Copy pathcommunityLeave.test.ts
File metadata and controls
137 lines (121 loc) · 3.51 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
import {beforeEach, describe, expect, it, vi} from 'vitest';
import '@helpers/peerIdPolyfill';
const mocks = vi.hoisted(() => ({
confirm: vi.fn(),
getPeerTitle: vi.fn(),
leaveCommunity: vi.fn(),
toast: vi.fn()
}));
vi.mock('@components/confirmationPopup', () => ({
default: mocks.confirm
}));
vi.mock('@components/toast', () => ({
toastNew: mocks.toast
}));
vi.mock('@components/wrappers/getPeerTitle', () => ({
default: mocks.getPeerTitle
}));
import leaveCommunityWithConfirmation, {
canLeaveCommunity
} from '@components/communities/leaveCommunity';
describe('Community leave flow', () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.confirm.mockResolvedValue(undefined);
mocks.getPeerTitle.mockResolvedValue('QA Community');
mocks.leaveCommunity.mockResolvedValue(undefined);
});
it('allows joined non-creators, including admins, to leave', () => {
expect(canLeaveCommunity({
_: 'community',
id: 1,
access_hash: '1',
title: 'Community',
photo: {_: 'chatPhotoEmpty'},
date: 1,
pFlags: {}
})).toBe(true);
expect(canLeaveCommunity({
_: 'community',
id: 1,
access_hash: '1',
title: 'Community',
photo: {_: 'chatPhotoEmpty'},
date: 1,
pFlags: {},
admin_rights: {
_: 'chatAdminRights',
pFlags: {change_info: true}
}
})).toBe(true);
expect(canLeaveCommunity({
_: 'community',
id: 1,
access_hash: '1',
title: 'Community',
photo: {_: 'chatPhotoEmpty'},
date: 1,
pFlags: {creator: true}
})).toBe(false);
expect(canLeaveCommunity({
_: 'community',
id: 1,
access_hash: '1',
title: 'Community',
photo: {_: 'chatPhotoEmpty'},
date: 1,
pFlags: {left: true}
})).toBe(false);
});
it('confirms with a danger action and leaves only the Community', async() => {
const managers = {
appChatsManager: {
leave: mocks.leaveCommunity
}
} as any;
await expect(leaveCommunityWithConfirmation({
communityId: 123 as ChatId,
managers
})).resolves.toBe(true);
expect(mocks.confirm).toHaveBeenCalledWith({
titleLangKey: 'Community.Leave',
descriptionLangKey: 'Community.LeaveConfirm',
descriptionLangArgs: ['QA Community'],
button: {
langKey: 'Community.Leave',
isDanger: true
}
});
expect(mocks.leaveCommunity).toHaveBeenCalledWith(123);
});
it('does not leave after canceling the confirmation', async() => {
mocks.confirm.mockRejectedValue(undefined);
await expect(leaveCommunityWithConfirmation({
communityId: 123 as ChatId,
managers: {
appChatsManager: {
leave: mocks.leaveCommunity
}
} as any
})).resolves.toBe(false);
expect(mocks.leaveCommunity).not.toHaveBeenCalled();
});
it('reports a failed leave without closing the surface', async() => {
const error = new Error('LEAVE_FAILED');
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
mocks.leaveCommunity.mockRejectedValue(error);
await expect(leaveCommunityWithConfirmation({
communityId: 123 as ChatId,
managers: {
appChatsManager: {
leave: mocks.leaveCommunity
}
} as any
})).resolves.toBe(false);
expect(consoleError).toHaveBeenCalledWith('leave community error', error);
expect(mocks.toast).toHaveBeenCalledWith({
langPackKey: 'Error.AnError'
});
consoleError.mockRestore();
});
});