This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
utils.test.jsx
797 lines (727 loc) · 28.3 KB
/
utils.test.jsx
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import assert from 'assert';
import {GeneralTypes} from 'mattermost-redux/action_types';
import store from 'stores/redux_store.jsx';
import Constants from 'utils/constants';
import * as Utils from 'utils/utils.jsx';
import * as lineBreakHelpers from 'tests/helpers/line_break_helpers.js';
import * as ua from 'tests/helpers/user_agent_mocks';
describe('Utils.getDisplayNameByUser', () => {
afterEach(() => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RESET,
data: {},
});
});
const userA = {username: 'a_user', nickname: 'a_nickname', first_name: 'a_first_name', last_name: ''};
const userB = {username: 'b_user', nickname: 'b_nickname', first_name: '', last_name: 'b_last_name'};
const userC = {username: 'c_user', nickname: '', first_name: 'c_first_name', last_name: 'c_last_name'};
const userD = {username: 'd_user', nickname: 'd_nickname', first_name: 'd_first_name', last_name: 'd_last_name'};
const userE = {username: 'e_user', nickname: '', first_name: 'e_first_name', last_name: 'e_last_name'};
const userF = {username: 'f_user', nickname: 'f_nickname', first_name: 'f_first_name', last_name: 'f_last_name'};
const userG = {username: 'g_user', nickname: '', first_name: 'g_first_name', last_name: 'g_last_name'};
const userH = {username: 'h_user', nickname: 'h_nickname', first_name: '', last_name: 'h_last_name'};
const userI = {username: 'i_user', nickname: 'i_nickname', first_name: 'i_first_name', last_name: ''};
const userJ = {username: 'j_user', nickname: '', first_name: 'j_first_name', last_name: ''};
test('Show display name of user with TeammateNameDisplay set to username', () => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RECEIVED,
data: {
TeammateNameDisplay: 'username',
},
});
[userA, userB, userC, userD, userE, userF, userG, userH, userI, userJ].forEach((user) => {
expect(Utils.getDisplayNameByUser(store.getState(), user)).toEqual(user.username);
});
});
test('Show display name of user with TeammateNameDisplay set to nickname_full_name', () => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RECEIVED,
data: {
TeammateNameDisplay: 'nickname_full_name',
},
});
for (const data of [
{user: userA, result: userA.nickname},
{user: userB, result: userB.nickname},
{user: userC, result: `${userC.first_name} ${userC.last_name}`},
{user: userD, result: userD.nickname},
{user: userE, result: `${userE.first_name} ${userE.last_name}`},
{user: userF, result: userF.nickname},
{user: userG, result: `${userG.first_name} ${userG.last_name}`},
{user: userH, result: userH.nickname},
{user: userI, result: userI.nickname},
{user: userJ, result: userJ.first_name},
]) {
expect(Utils.getDisplayNameByUser(store.getState(), data.user)).toEqual(data.result);
}
});
test('Show display name of user with TeammateNameDisplay set to username', () => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RECEIVED,
data: {
TeammateNameDisplay: 'full_name',
},
});
for (const data of [
{user: userA, result: userA.first_name},
{user: userB, result: userB.last_name},
{user: userC, result: `${userC.first_name} ${userC.last_name}`},
{user: userD, result: `${userD.first_name} ${userD.last_name}`},
{user: userE, result: `${userE.first_name} ${userE.last_name}`},
{user: userF, result: `${userF.first_name} ${userF.last_name}`},
{user: userG, result: `${userG.first_name} ${userG.last_name}`},
{user: userH, result: userH.last_name},
{user: userI, result: userI.first_name},
{user: userJ, result: userJ.first_name},
]) {
expect(Utils.getDisplayNameByUser(store.getState(), data.user)).toEqual(data.result);
}
});
});
describe('Utils.sortUsersByStatusAndDisplayName', () => {
afterEach(() => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RESET,
data: {},
});
});
const userA = {id: 'a', username: 'a_user', nickname: 'ja_nickname', first_name: 'a_first_name', last_name: 'ja_last_name'};
const userB = {id: 'b', username: 'b_user', nickname: 'ib_nickname', first_name: 'a_first_name', last_name: 'ib_last_name'};
const userC = {id: 'c', username: 'c_user', nickname: 'hc_nickname', first_name: 'a_first_name', last_name: 'hc_last_name'};
const userD = {id: 'd', username: 'd_user', nickname: 'gd_nickname', first_name: 'a_first_name', last_name: 'gd_last_name'};
const userE = {id: 'e', username: 'e_user', nickname: 'fe_nickname', first_name: 'b_first_name', last_name: 'fe_last_name'};
const userF = {id: 'f', username: 'f_user', nickname: 'ef_nickname', first_name: 'b_first_name', last_name: 'ef_last_name'};
const userG = {id: 'g', username: 'g_user', nickname: 'dg_nickname', first_name: 'b_first_name', last_name: 'dg_last_name'};
const userH = {id: 'h', username: 'h_user', nickname: 'ch_nickname', first_name: 'c_first_name', last_name: 'ch_last_name'};
const userI = {id: 'i', username: 'i_user', nickname: 'bi_nickname', first_name: 'c_first_name', last_name: 'bi_last_name'};
const userJ = {id: 'j', username: 'j_user', nickname: 'aj_nickname', first_name: 'c_first_name', last_name: 'aj_last_name'};
const userK = {id: 'k', username: 'k_bot', nickname: 'ak_nickname', first_name: 'a_first_name', last_name: 'aABot_last_name', is_bot: true};
const userL = {id: 'l', username: 'l_bot', nickname: 'al_nickname', first_name: 'b_first_name', last_name: 'aBBot_last_name', is_bot: true};
const userM = {id: 'm', username: 'm_bot', nickname: 'am_nickname', first_name: 'c_first_name', last_name: 'aCBot_last_name', is_bot: true};
const statusesByUserId = {
a: 'dnd',
b: 'away',
c: 'offline',
d: 'online',
e: 'online',
f: 'online',
g: 'dnd',
h: 'away',
i: 'offline',
j: 'online',
k: 'offline',
l: 'offline',
m: 'offline',
};
test('Users sort by status and displayname, TeammateNameDisplay set to username', () => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RECEIVED,
data: {
TeammateNameDisplay: 'username',
},
});
for (const data of [
{
users: [userM, userK, userL, userF, userA, userB, userC, userD, userE],
result: [userD, userE, userF, userB, userA, userC, userK, userL, userM],
},
{
users: [userM, userL, userK, userJ, userI, userH, userG, userF, userE],
result: [userE, userF, userJ, userH, userG, userI, userK, userL, userM],
},
{
users: [userL, userM, userK, userJ, userF, userE, userD],
result: [userD, userE, userF, userJ, userK, userL, userM],
},
]) {
const sortedUsers = Utils.sortUsersByStatusAndDisplayName(data.users, statusesByUserId, 'username');
for (let i = 0; i < sortedUsers.length; i++) {
expect(sortedUsers[i]).toEqual(data.result[i]);
}
}
});
test('Users sort by status and displayname, TeammateNameDisplay set to nickname_full_name', () => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RECEIVED,
data: {
TeammateNameDisplay: 'nickname_full_name',
},
});
for (const data of [
{
users: [userM, userK, userL, userF, userA, userB, userC, userD, userE],
result: [userF, userE, userD, userB, userA, userC, userK, userL, userM],
},
{
users: [userM, userL, userK, userJ, userI, userH, userG, userF, userE],
result: [userJ, userF, userE, userH, userG, userI, userK, userL, userM],
},
{
users: [userL, userM, userK, userJ, userF, userE, userD],
result: [userJ, userF, userE, userD, userK, userL, userM],
},
]) {
const sortedUsers = Utils.sortUsersByStatusAndDisplayName(data.users, statusesByUserId, 'nickname_full_name');
for (let i = 0; i < sortedUsers.length; i++) {
expect(sortedUsers[i]).toEqual(data.result[i]);
}
}
});
test('Users sort by status and displayname, TeammateNameDisplay set to full_name', () => {
store.dispatch({
type: GeneralTypes.CLIENT_CONFIG_RECEIVED,
data: {
TeammateNameDisplay: 'full_name',
},
});
for (const data of [
{
users: [userM, userK, userL, userF, userA, userB, userC, userD, userE],
result: [userD, userF, userE, userB, userA, userC, userK, userL, userM],
},
{
users: [userM, userL, userK, userJ, userI, userH, userG, userF, userE],
result: [userF, userE, userJ, userH, userG, userI, userK, userL, userM],
},
{
users: [userL, userM, userK, userJ, userF, userE, userD],
result: [userD, userF, userE, userJ, userK, userL, userM],
},
]) {
const sortedUsers = Utils.sortUsersByStatusAndDisplayName(data.users, statusesByUserId, 'full_name');
for (let i = 0; i < sortedUsers.length; i++) {
expect(sortedUsers[i]).toEqual(data.result[i]);
}
}
});
});
describe('Utils.isValidPassword', () => {
test('Minimum length enforced', () => {
for (const data of [
{
password: 'tooshort',
config: {
minimumLength: 10,
requireLowercase: false,
requireUppercase: false,
requireNumber: false,
requireSymbol: false,
},
valid: false,
},
{
password: 'longenoughpassword',
config: {
minimumLength: 10,
requireLowercase: false,
requireUppercase: false,
requireNumber: false,
requireSymbol: false,
},
valid: true,
},
]) {
const {valid} = Utils.isValidPassword(data.password, data.config);
expect(data.valid).toEqual(valid);
}
});
test('Require lowercase enforced', () => {
for (const data of [
{
password: 'UPPERCASE',
config: {
minimumLength: 5,
requireLowercase: true,
requireUppercase: false,
requireNumber: false,
requireSymbol: false,
},
valid: false,
},
{
password: 'SOMELowercase',
config: {
minimumLength: 5,
requireLowercase: true,
requireUppercase: false,
requireNumber: false,
requireSymbol: false,
},
valid: true,
},
]) {
const {valid} = Utils.isValidPassword(data.password, data.config);
expect(data.valid).toEqual(valid);
}
});
test('Require uppercase enforced', () => {
for (const data of [
{
password: 'lowercase',
config: {
minimumLength: 5,
requireLowercase: false,
requireUppercase: true,
requireNumber: false,
requireSymbol: false,
},
valid: false,
},
{
password: 'SOMEUppercase',
config: {
minimumLength: 5,
requireLowercase: false,
requireUppercase: true,
requireNumber: false,
requireSymbol: false,
},
valid: true,
},
]) {
const {valid} = Utils.isValidPassword(data.password, data.config);
expect(data.valid).toEqual(valid);
}
});
test('Require number enforced', () => {
for (const data of [
{
password: 'NoNumbers',
config: {
minimumLength: 5,
requireLowercase: true,
requireUppercase: true,
requireNumber: true,
requireSymbol: false,
},
valid: false,
},
{
password: 'S0m3Numb3rs',
config: {
minimumLength: 5,
requireLowercase: true,
requireUppercase: true,
requireNumber: true,
requireSymbol: false,
},
valid: true,
},
]) {
const {valid} = Utils.isValidPassword(data.password, data.config);
expect(data.valid).toEqual(valid);
}
});
test('Require symbol enforced', () => {
for (const data of [
{
password: 'N0Symb0ls',
config: {
minimumLength: 5,
requireLowercase: true,
requireUppercase: true,
requireNumber: true,
requireSymbol: true,
},
valid: false,
},
{
password: 'S0m3Symb0!s',
config: {
minimumLength: 5,
requireLowercase: true,
requireUppercase: true,
requireNumber: true,
requireSymbol: true,
},
valid: true,
},
]) {
const {valid} = Utils.isValidPassword(data.password, data.config);
expect(data.valid).toEqual(valid);
}
});
});
describe('Utils.isKeyPressed', () => {
test('Key match is used over keyCode if it exists', () => {
for (const data of [
{
event: new KeyboardEvent('keydown', {key: '/', keyCode: 55}),
key: ['/', 191, 'Slash'],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'ù', keyCode: 191}),
key: ['/', 191, 'Slash'],
valid: true,
},
]) {
expect(Utils.isKeyPressed(data.event, data.key)).toEqual(data.valid);
}
});
test('Key match works for both uppercase and lower case', () => {
for (const data of [
{
event: new KeyboardEvent('keydown', {key: 'A', keyCode: 65, code: 'KeyA'}),
key: ['a', 65, 'KeyA'],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'a', keyCode: 65, code: 'KeyA'}),
key: ['a', 65, 'KeyA'],
valid: true,
},
]) {
expect(Utils.isKeyPressed(data.event, data.key)).toEqual(data.valid);
}
});
test('KeyCode is used for dead letter keys', () => {
for (const data of [
{
event: new KeyboardEvent('keydown', {key: 'Dead', keyCode: 222}),
key: ['', 222],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'Dead', keyCode: 222}),
key: ['not-used-field', 222],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'Dead', keyCode: 222}),
key: [null, 222],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'Dead', keyCode: 222}),
key: [null, 223],
valid: false,
},
]) {
expect(Utils.isKeyPressed(data.event, data.key)).toEqual(data.valid);
}
});
test('KeyCode is used for unidentified keys', () => {
for (const data of [
{
event: new KeyboardEvent('keydown', {key: 'Unidentified', keyCode: 2220, code: 'Unidentified'}),
key: ['', 2220],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'Unidentified', keyCode: 2220, code: 'Unidentified'}),
key: ['not-used-field', 2220],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'Unidentified', keyCode: 2220, code: 'Unidentified'}),
key: [null, 2220],
valid: true,
},
{
event: new KeyboardEvent('keydown', {key: 'Unidentified', keyCode: 2220, code: 'Unidentified'}),
key: [null, 2221],
valid: false,
},
]) {
expect(Utils.isKeyPressed(data.event, data.key)).toEqual(data.valid);
}
});
test('KeyCode is used for undefined keys', () => {
for (const data of [
{
event: {keyCode: 2221},
key: ['', 2221],
valid: true,
},
{
event: {keyCode: 2221},
key: ['not-used-field', 2221],
valid: true,
},
{
event: {keyCode: 2221},
key: [null, 2221],
valid: true,
},
{
event: {keyCode: 2221},
key: [null, 2222],
valid: false,
},
]) {
expect(Utils.isKeyPressed(data.event, data.key)).toEqual(data.valid);
}
});
test('keyCode is used for determining if it exists', () => {
for (const data of [
{
event: {key: 'a', keyCode: 65},
key: ['k', 65],
valid: true,
},
{
event: {key: 'b', keyCode: 66},
key: ['y', 66],
valid: true,
},
]) {
expect(Utils.isKeyPressed(data.event, data.key)).toEqual(data.valid);
}
});
test('key should be tested as fallback for different layout of english keyboards', () => {
//key will be k for keyboards like dvorak but code will be keyV as `v` is pressed
const event = {key: 'k', code: 'KeyV'};
const key = ['k', 2221];
expect(Utils.isKeyPressed(event, key)).toEqual(true);
});
});
describe('Utils.localizeMessage', () => {
const originalGetState = store.getState;
afterAll(() => {
store.getState = originalGetState;
});
const entities = {
general: {
config: {},
},
users: {
currentUserId: 'abcd',
profiles: {
abcd: {
locale: 'fr',
},
},
},
};
describe('with translations', () => {
beforeAll(() => {
store.getState = () => ({
entities,
views: {
i18n: {
translations: {
fr: {
'test.hello_world': 'Bonjour tout le monde!',
},
},
},
},
});
});
test('with translations', () => {
expect(Utils.localizeMessage('test.hello_world', 'Hello, World!')).toEqual('Bonjour tout le monde!');
});
test('with missing string in translations', () => {
expect(Utils.localizeMessage('test.hello_world2', 'Hello, World 2!')).toEqual('Hello, World 2!');
});
test('with missing string in translations and no default', () => {
expect(Utils.localizeMessage('test.hello_world2')).toEqual('test.hello_world2');
});
});
describe('without translations', () => {
beforeAll(() => {
store.getState = () => ({
entities,
views: {
i18n: {
translations: {},
},
},
});
});
test('without translations', () => {
expect(Utils.localizeMessage('test.hello_world', 'Hello, World!')).toEqual('Hello, World!');
});
test('without translations and no default', () => {
expect(Utils.localizeMessage('test.hello_world')).toEqual('test.hello_world');
});
});
});
describe('Utils.isDevMode', () => {
const originalGetState = store.getState;
afterAll(() => {
store.getState = originalGetState;
});
describe('dev mode off', () => {
beforeAll(() => {
store.getState = () => ({
entities: {
general: {
config: {},
},
},
});
});
test('with missing EnableDeveloper field', () => {
expect(Utils.isDevMode()).toEqual(false);
});
test('with EnableDeveloper field set to false', () => {
store.getState = () => ({
entities: {
general: {
config: {
EnableDeveloper: 'false',
},
},
},
});
expect(Utils.isDevMode()).toEqual(false);
});
test('with EnableDeveloper field set to false bool', () => {
store.getState = () => ({
entities: {
general: {
config: {
EnableDeveloper: false,
},
},
},
});
expect(Utils.isDevMode()).toEqual(false);
});
test('with EnableDeveloper field set to true bool', () => {
store.getState = () => ({
entities: {
general: {
config: {
EnableDeveloper: true,
},
},
},
});
expect(Utils.isDevMode()).toEqual(false);
});
test('with EnableDeveloper field set to null', () => {
store.getState = () => ({
entities: {
general: {
config: {
EnableDeveloper: null,
},
},
},
});
expect(Utils.isDevMode()).toEqual(false);
});
});
describe('dev mode on', () => {
beforeAll(() => {
store.getState = () => ({
entities: {
general: {
config: {},
},
},
});
});
test('with EnableDeveloper field set to true text', () => {
store.getState = () => ({
entities: {
general: {
config: {
EnableDeveloper: 'true',
},
},
},
});
expect(Utils.isDevMode()).toEqual(true);
});
});
});
describe('Utils.enableDevModeFeatures', () => {
const cleanUp = () => {
delete Map.prototype.length;
delete Set.prototype.length;
};
beforeEach(cleanUp);
afterEach(cleanUp);
describe('with DevModeFeatures', () => {
beforeEach(cleanUp);
afterEach(cleanUp);
test('invoke Map.Length', () => {
Utils.enableDevModeFeatures();
expect(() => new Map().length).toThrow(Error);
});
test('invoke Set.Length', () => {
Utils.enableDevModeFeatures();
expect(() => new Set().length).toThrow(Error);
});
});
describe('without DevModeFeatures', () => {
test('invoke Map.Length', () => {
expect(new Map().length).toEqual(undefined);
});
test('invoke Set.Length', () => {
expect(new Set().length).toEqual(undefined);
});
});
});
describe('Utils.getSortedUsers', () => {
test('should sort users by who reacted first', () => {
const baseDate = Date.now();
const currentUserId = 'user_id_1';
const profiles = [{id: 'user_id_1', username: 'username_1'}, {id: 'user_id_2', username: 'username_2'}, {id: 'user_id_3', username: 'username_3'}];
const reactions = [
{user_id: 'user_id_2', create_at: baseDate}, // Will be sorted 2nd, after the logged-in user
{user_id: 'user_id_1', create_at: baseDate + 5000}, // Logged-in user, will be sorted first although 2nd user reacted first
{user_id: 'user_id_3', create_at: baseDate + 8000}, // Last to react, will be sorted last
];
const {currentUserReacted, users} = Utils.getSortedUsers(reactions, currentUserId, profiles, 'username');
expect(currentUserReacted).toEqual(true);
assert.deepEqual(
users,
['You', 'username_2', 'username_3'],
);
});
});
describe('Utils.imageURLForUser', () => {
test('should return url when user id and last_picture_update is given', () => {
const imageUrl = Utils.imageURLForUser('foobar-123', 123456);
expect(imageUrl).toEqual('/api/v4/users/foobar-123/image?_=123456');
});
test('should return url when user id is given without last_picture_update', () => {
const imageUrl = Utils.imageURLForUser('foobar-123');
expect(imageUrl).toEqual('/api/v4/users/foobar-123/image?_=0');
});
});
describe('Utils.isUnhandledLineBreakKeyCombo', () => {
test('isUnhandledLineBreakKeyCombo returns true for alt + enter for Chrome UA', () => {
ua.mockChrome();
expect(Utils.isUnhandledLineBreakKeyCombo(lineBreakHelpers.getAltKeyEvent())).toBe(true);
});
test('isUnhandledLineBreakKeyCombo returns false for alt + enter for Safari UA', () => {
ua.mockSafari();
expect(Utils.isUnhandledLineBreakKeyCombo(lineBreakHelpers.getAltKeyEvent())).toBe(false);
});
test('isUnhandledLineBreakKeyCombo returns false for shift + enter', () => {
expect(Utils.isUnhandledLineBreakKeyCombo(lineBreakHelpers.getShiftKeyEvent())).toBe(false);
});
test('isUnhandledLineBreakKeyCombo returns false for ctrl/command + enter', () => {
expect(Utils.isUnhandledLineBreakKeyCombo(lineBreakHelpers.getCtrlKeyEvent())).toBe(false);
expect(Utils.isUnhandledLineBreakKeyCombo(lineBreakHelpers.getMetaKeyEvent())).toBe(false);
});
test('isUnhandledLineBreakKeyCombo returns false for just enter', () => {
expect(Utils.isUnhandledLineBreakKeyCombo(lineBreakHelpers.BASE_EVENT)).toBe(false);
});
test('isUnhandledLineBreakKeyCombo returns false for f (random key)', () => {
const e = {
...lineBreakHelpers.BASE_EVENT,
key: Constants.KeyCodes.F[0],
keyCode: Constants.KeyCodes.F[1],
};
expect(Utils.isUnhandledLineBreakKeyCombo(e)).toBe(false);
});
// restore initial user agent
afterEach(ua.reset);
});
describe('Utils.insertLineBreakFromKeyEvent', () => {
test('insertLineBreakFromKeyEvent returns with line break appending (no selection range)', () => {
expect(Utils.insertLineBreakFromKeyEvent(lineBreakHelpers.getAppendEvent())).toBe(lineBreakHelpers.OUTPUT_APPEND);
});
test('insertLineBreakFromKeyEvent returns with line break replacing (with selection range)', () => {
expect(Utils.insertLineBreakFromKeyEvent(lineBreakHelpers.getReplaceEvent())).toBe(lineBreakHelpers.OUTPUT_REPLACE);
});
});