-
-
Notifications
You must be signed in to change notification settings - Fork 771
/
actions.js
159 lines (138 loc) · 6.72 KB
/
actions.js
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
/*global mock, converse */
const { Strophe, u, stx } = converse.env;
describe("A Groupchat Message", function () {
it("Can be copied using a message action",
mock.initConverse([], {}, async function (_converse) {
const muc_jid = 'lounge@montague.lit';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const stanza = stx`
<presence
to="romeo@montague.lit/_converse.js-29092160"
from="coven@chat.shakespeare.lit/newguy"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="newguy@montague.lit/_converse.js-290929789" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
const view = _converse.chatboxviews.get(muc_jid);
const textarea = await u.waitUntil(() => view.querySelector('textarea.chat-textarea'));
const message_form = view.querySelector('converse-muc-message-form');
const spyClipboard = spyOn(navigator.clipboard, 'writeText');
const firstMessageText = 'But soft, what light through yonder airlock breaks?';
const msg_id = u.getUniqueId();
await model.handleMessageStanza(stx`
<message
from="lounge@montague.lit/newguy"
to="${_converse.api.connection.get().jid}"
type="groupchat"
id="${msg_id}"
xmlns="jabber:client">
<body>${firstMessageText}</body>
</message>`);
await u.waitUntil(() => view.querySelectorAll('.chat-msg').length === 1);
let firstAction = view.querySelector('.chat-msg__action-copy');
expect(firstAction).not.toBeNull();
firstAction.click();
expect(spyClipboard).toHaveBeenCalledOnceWith(firstMessageText);
const secondMessageText = 'Hello';
textarea.value = secondMessageText;
message_form.onKeyDown({
target: textarea,
preventDefault: function preventDefault () {},
keyCode: 13 // Enter
});
await u.waitUntil(() => view.querySelectorAll('.chat-msg').length === 2);
const copyActions = view.querySelectorAll('.chat-msg__action-copy');
expect(copyActions.length).toBe(2);
let secondAction = copyActions[copyActions.length - 1];
expect(secondAction).not.toBeNull();
secondAction.click();
expect(spyClipboard).toHaveBeenCalledWith(secondMessageText);
}));
it("Can be quoted using a message action",
mock.initConverse([], {}, async function (_converse) {
const muc_jid = 'lounge@montague.lit';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const stanza = stx`
<presence
to="romeo@montague.lit/_converse.js-29092160"
from="coven@chat.shakespeare.lit/newguy"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="newguy@montague.lit/_converse.js-290929789" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
const firstMessageText = 'But soft, what light through yonder airlock breaks?';
const msg_id = u.getUniqueId();
await model.handleMessageStanza(stx`
<message
from="lounge@montague.lit/newguy"
to="${_converse.api.connection.get().jid}"
type="groupchat"
id="${msg_id}"
xmlns="jabber:client">
<body>${firstMessageText}</body>
</message>`);
const view = _converse.chatboxviews.get(muc_jid);
const textarea = await u.waitUntil(() => view.querySelector('textarea.chat-textarea'));
// Quote with empty text area
expect(textarea.value).toBe('');
let firstAction = await u.waitUntil(() => view.querySelector('.chat-msg__action-quote'));
expect(firstAction).not.toBeNull();
firstAction.click();
expect(textarea.value).toBe('> ' + firstMessageText + '\n');
// Quote with already-present text
textarea.value = 'Hi!';
firstAction.click();
expect(textarea.value).toBe('Hi!\n> ' + firstMessageText + '\n');
// Quote with already-present text
textarea.value = 'Hi!';
firstAction.click();
expect(textarea.value).toBe('Hi!\n> ' + firstMessageText + '\n');
}));
it("Cannot be quoted without permission to speak",
mock.initConverse([], {}, async function (_converse) {
const muc_jid = 'lounge@montague.lit';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo', ['muc_moderated']);
const stanza = stx`
<presence
to="romeo@montague.lit/_converse.js-29092160"
from="coven@chat.shakespeare.lit/newguy"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="newguy@montague.lit/_converse.js-290929789" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
const view = _converse.chatboxviews.get(muc_jid);
const msg_id = u.getUniqueId();
await model.handleMessageStanza(stx`
<message
from="lounge@montague.lit/newguy"
to="${_converse.api.connection.get().jid}"
type="groupchat"
id="${msg_id}"
xmlns="jabber:client">
<body>But soft, what light through yonder airlock breaks?</body>
</message>`);
await u.waitUntil(() => view.querySelectorAll('.chat-msg').length === 1);
// Quoting should be available before losing permission to speak
expect(view.querySelector('.chat-msg__action-quote')).not.toBeNull();
const presence = stx`
<presence
to="romeo@montague.lit/orchard"
from="${muc_jid}/romeo"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" role="visitor"/>
</x>
<status code="110"/>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
const occupant = view.model.occupants.findWhere({'jid': _converse.bare_jid});
await u.waitUntil(() => occupant.get('role') === 'visitor');
expect(view.querySelector('.chat-msg__action-quote')).toBeNull();
}));
});