-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.jsx
513 lines (441 loc) · 17.1 KB
/
index.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
/**
* @format
* @jest-environment jsdom
*/
/**
* External dependencies
*/
import { expect } from 'chai';
import { mount } from 'enzyme';
import { filter, map } from 'lodash';
import React from 'react';
import { test } from 'sinon';
/**
* Internal dependencies
*/
import fixtures from './lib/fixtures';
import TokenFieldWrapper from './lib/token-field-wrapper';
jest.mock( 'components/tooltip', () => require( 'components/empty-component' ) );
/**
* Module variables
*/
const keyCodes = {
backspace: 8,
tab: 9,
enter: 13,
leftArrow: 37,
upArrow: 38,
rightArrow: 39,
downArrow: 40,
delete: 46,
comma: 188,
};
const charCodes = {
comma: 44,
};
describe( 'TokenField', function() {
let wrapper, tokenFieldNode, textInputNode;
function setText( text ) {
textInputNode.simulate( 'change', { target: { value: text } } );
}
function sendKeyDown( keyCode, shiftKey ) {
tokenFieldNode.simulate( 'keyDown', {
keyCode: keyCode,
shiftKey: !! shiftKey,
} );
}
function sendKeyPress( charCode ) {
tokenFieldNode.simulate( 'keyPress', {
charCode: charCode,
} );
}
function getNodeInnerHtml( node ) {
const div = document.createElement( 'div' );
div.innerHTML = node.html();
return div.firstChild.innerHTML;
}
function getTokensHTML() {
const textNodes = tokenFieldNode.find( '.token-field__token-text' );
return textNodes.map( getNodeInnerHtml );
}
function getSuggestionsText( selector ) {
const suggestionNodes = tokenFieldNode.find( selector || '.token-field__suggestion' );
return suggestionNodes.map( getSuggestionNodeText );
}
function getSuggestionNodeText( node ) {
if ( ! node.find( 'span' ).length ) {
return getNodeInnerHtml( node );
}
// This suggestion is part of a partial match; return up to three
// sections of the suggestion (before match, match, and after
// match)
const div = document.createElement( 'div' );
div.innerHTML = node.find( 'span' ).html();
return map(
filter(
div.firstChild.childNodes,
childNode => childNode.nodeType !== window.Node.COMMENT_NODE
),
childNode => childNode.textContent
);
}
function getSelectedSuggestion() {
const selectedSuggestions = getSuggestionsText( '.token-field__suggestion.is-selected' );
return selectedSuggestions[ 0 ] || null;
}
beforeEach( function() {
wrapper = mount( <TokenFieldWrapper /> );
tokenFieldNode = wrapper.ref( 'tokenField' );
textInputNode = tokenFieldNode.find( '.token-field__input' );
textInputNode.simulate( 'focus' );
} );
describe( 'displaying tokens', function() {
it( 'should render default tokens', function() {
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar' ] );
} );
it( 'should display tokens with escaped special characters properly', function() {
wrapper.setState( {
tokens: fixtures.specialTokens.textEscaped,
} );
expect( getTokensHTML() ).to.deep.equal( fixtures.specialTokens.htmlEscaped );
} );
it( 'should display tokens with special characters properly', function() {
// This test is not as realistic as the previous one: if a WP site
// contains tag names with special characters, the API will always
// return the tag names already escaped. However, this is still
// worth testing, so we can be sure that token values with
// dangerous characters in them don't have these characters carried
// through unescaped to the HTML.
wrapper.setState( {
tokens: fixtures.specialTokens.textUnescaped,
} );
expect( getTokensHTML() ).to.deep.equal( fixtures.specialTokens.htmlUnescaped );
} );
} );
describe( 'suggestions', function() {
it( 'should render default suggestions', function() {
// limited by maxSuggestions (default 100 so doesn't matter here)
expect( getSuggestionsText() ).to.deep.equal( wrapper.state( 'tokenSuggestions' ) );
} );
it( 'should remove already added tags from suggestions', function() {
wrapper.setState( {
tokens: Object.freeze( [ 'of', 'and' ] ),
} );
expect( getSuggestionsText() ).to.not.include.members( getTokensHTML() );
} );
it( 'should suggest partial matches', function() {
setText( 't' );
expect( getSuggestionsText() ).to.deep.equal( fixtures.matchingSuggestions.t );
} );
it( 'suggestions that begin with match are boosted', function() {
setText( 's' );
expect( getSuggestionsText() ).to.deep.equal( fixtures.matchingSuggestions.s );
} );
it( 'should display suggestions with escaped special characters properly', function() {
wrapper.setState( {
tokenSuggestions: fixtures.specialSuggestions.textEscaped,
} );
expect( getSuggestionsText() ).to.deep.equal( fixtures.specialSuggestions.htmlEscaped );
} );
it( 'should display suggestions with special characters properly', function() {
wrapper.setState( {
tokenSuggestions: fixtures.specialSuggestions.textUnescaped,
} );
expect( getSuggestionsText() ).to.deep.equal( fixtures.specialSuggestions.htmlUnescaped );
} );
it( 'should match against the unescaped values of suggestions with special characters', function() {
setText( '&' );
wrapper.setState( {
tokenSuggestions: fixtures.specialSuggestions.textUnescaped,
} );
expect( getSuggestionsText() ).to.deep.equal(
fixtures.specialSuggestions.matchAmpersandUnescaped
);
} );
it( 'should match against the unescaped values of suggestions with special characters (including spaces)', function() {
setText( 's &' );
wrapper.setState( {
tokenSuggestions: fixtures.specialSuggestions.textUnescaped,
} );
expect( getSuggestionsText() ).to.deep.equal(
fixtures.specialSuggestions.matchAmpersandSequence
);
} );
it( 'should not match against the escaped values of suggestions with special characters', function() {
setText( 'amp' );
wrapper.setState( {
tokenSuggestions: fixtures.specialSuggestions.textUnescaped,
} );
expect( getSuggestionsText() ).to.deep.equal(
fixtures.specialSuggestions.matchAmpersandEscaped
);
} );
it( 'should match suggestions even with trailing spaces', function() {
setText( ' at ' );
expect( getSuggestionsText() ).to.deep.equal( fixtures.matchingSuggestions.at );
} );
it(
'should manage the selected suggestion based on both keyboard and mouse events',
test( function() {
setText( 't' );
expect( getSuggestionsText() ).to.deep.equal( fixtures.matchingSuggestions.t );
expect( getSelectedSuggestion() ).to.equal( null );
sendKeyDown( keyCodes.downArrow ); // 'the'
expect( getSelectedSuggestion() ).to.deep.equal( [ 't', 'he' ] );
sendKeyDown( keyCodes.downArrow ); // 'to'
expect( getSelectedSuggestion() ).to.deep.equal( [ 't', 'o' ] );
const hoverSuggestion = tokenFieldNode.find( '.token-field__suggestion' ).at( 5 ); // 'it'
expect( getSuggestionNodeText( hoverSuggestion ) ).to.deep.equal( [ 'i', 't' ] );
// before sending a hover event, we need to wait for
// SuggestionList#_scrollingIntoView to become false
this.clock.tick( 100 );
hoverSuggestion.simulate( 'mouseEnter' );
expect( getSelectedSuggestion() ).to.deep.equal( [ 'i', 't' ] );
sendKeyDown( keyCodes.upArrow );
expect( getSelectedSuggestion() ).to.deep.equal( [ 'wi', 't', 'h' ] );
sendKeyDown( keyCodes.upArrow );
expect( getSelectedSuggestion() ).to.deep.equal( [ 't', 'his' ] );
hoverSuggestion.simulate( 'click' );
expect( getSelectedSuggestion() ).to.equal( null );
expect( getTokensHTML() ).to.deep.equal( [ 'foo', 'bar', 'it' ] );
} )
);
} );
describe( 'adding tokens', function() {
it( 'should add a token when Tab pressed', function() {
setText( 'baz' );
sendKeyDown( keyCodes.tab );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( '' );
} );
it( 'should not allow adding blank tokens with Tab', function() {
sendKeyDown( keyCodes.tab );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar' ] );
} );
it( 'should not allow adding whitespace tokens with Tab', function() {
setText( ' ' );
sendKeyDown( keyCodes.tab );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar' ] );
} );
it( 'should add a token when Enter pressed', function() {
setText( 'baz' );
sendKeyDown( keyCodes.enter );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( '' );
} );
it( 'should not allow adding blank tokens with Enter', function() {
sendKeyDown( keyCodes.enter );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar' ] );
} );
it( 'should not allow adding whitespace tokens with Enter', function() {
setText( ' ' );
sendKeyDown( keyCodes.enter );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar' ] );
} );
it( 'should not allow adding whitespace tokens with comma', function() {
setText( ' ' );
sendKeyPress( charCodes.comma );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar' ] );
} );
it( 'should add a token when comma pressed', function() {
setText( 'baz' );
sendKeyPress( charCodes.comma );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ] );
} );
it( 'should not add a token when < pressed', function() {
setText( 'baz' );
sendKeyDown( keyCodes.comma, true );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar' ] );
// The text input does not register the < keypress when it is sent this way.
expect( textInputNode.prop( 'value' ) ).to.equal( 'baz' );
} );
it( 'should trim token values when adding', function() {
setText( ' baz ' );
sendKeyDown( keyCodes.enter );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ] );
} );
function testOnBlur( initialText, selectSuggestion, expectedSuggestion, expectedTokens ) {
setText( initialText );
if ( selectSuggestion ) {
sendKeyDown( keyCodes.downArrow ); // 'the'
sendKeyDown( keyCodes.downArrow ); // 'to'
}
expect( getSelectedSuggestion() ).to.deep.equal( expectedSuggestion );
function testSavedState( isActive ) {
expect( wrapper.state( 'tokens' ) ).to.deep.equal( expectedTokens );
expect( textInputNode.prop( 'value' ) ).to.equal( '' );
expect( getSelectedSuggestion() ).to.equal( null );
expect(
tokenFieldNode
.find( 'div' )
.first()
.hasClass( 'is-active' )
).to.equal( isActive );
}
document.activeElement.blur();
textInputNode.simulate( 'blur' );
testSavedState( false );
textInputNode.simulate( 'focus' );
testSavedState( true );
}
it(
'should add the current text when the input field loses focus',
test( function() {
testOnBlur(
't', // initialText
false, // selectSuggestion
null, // expectedSuggestion
[ 'foo', 'bar', 't' ] // expectedTokens
);
} )
);
it(
'should add the suggested token when the (non-blank) input field loses focus',
test( function() {
testOnBlur(
't', // initialText
true, // selectSuggestion
[ 't', 'o' ], // expectedSuggestion
[ 'foo', 'bar', 'to' ] // expectedTokens
);
} )
);
it(
'should not add the suggested token when the (blank) input field loses focus',
test( function() {
testOnBlur(
'', // initialText
true, // selectSuggestion
'of', // expectedSuggestion
[ 'foo', 'bar' ], // expectedTokens
this.clock
);
} )
);
it(
'should not lose focus when a suggestion is clicked',
test( function() {
// prevents regression of https://github.com/Automattic/wp-calypso/issues/1884
const firstSuggestion = tokenFieldNode.find( '.token-field__suggestion' ).at( 0 );
firstSuggestion.simulate( 'click' );
// wait for setState call
this.clock.tick( 10 );
expect(
tokenFieldNode
.find( 'div' )
.first()
.hasClass( 'is-active' )
).to.equal( true );
} )
);
it( 'should add tokens in the middle of the current tokens', function() {
sendKeyDown( keyCodes.leftArrow );
setText( 'baz' );
sendKeyDown( keyCodes.tab );
setText( 'quux' );
sendKeyDown( keyCodes.tab );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'baz', 'quux', 'bar' ] );
} );
it( 'should add tokens from the selected matching suggestion using Tab', function() {
setText( 't' );
expect( getSelectedSuggestion() ).to.equal( null );
sendKeyDown( keyCodes.downArrow ); // 'the'
expect( getSelectedSuggestion() ).to.deep.equal( [ 't', 'he' ] );
sendKeyDown( keyCodes.downArrow ); // 'to'
expect( getSelectedSuggestion() ).to.deep.equal( [ 't', 'o' ] );
sendKeyDown( keyCodes.tab );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'to' ] );
expect( getSelectedSuggestion() ).to.equal( null );
} );
it( 'should add tokens from the selected matching suggestion using Enter', function() {
setText( 't' );
expect( getSelectedSuggestion() ).to.equal( null );
sendKeyDown( keyCodes.downArrow ); // 'the'
expect( getSelectedSuggestion() ).to.deep.equal( [ 't', 'he' ] );
sendKeyDown( keyCodes.downArrow ); // 'to'
expect( getSelectedSuggestion() ).to.deep.equal( [ 't', 'o' ] );
sendKeyDown( keyCodes.enter );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'to' ] );
expect( getSelectedSuggestion() ).to.equal( null );
} );
it( 'should add tokens from the selected suggestion using Tab', function() {
expect( getSelectedSuggestion() ).to.equal( null );
sendKeyDown( keyCodes.downArrow ); // 'the'
expect( getSelectedSuggestion() ).to.equal( 'the' );
sendKeyDown( keyCodes.downArrow ); // 'of'
expect( getSelectedSuggestion() ).to.equal( 'of' );
sendKeyDown( keyCodes.tab );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'of' ] );
expect( getSelectedSuggestion() ).to.equal( null );
} );
it( 'should add tokens from the selected suggestion using Enter', function() {
expect( getSelectedSuggestion() ).to.equal( null );
sendKeyDown( keyCodes.downArrow ); // 'the'
expect( getSelectedSuggestion() ).to.equal( 'the' );
sendKeyDown( keyCodes.downArrow ); // 'of'
expect( getSelectedSuggestion() ).to.equal( 'of' );
sendKeyDown( keyCodes.enter );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'of' ] );
expect( getSelectedSuggestion() ).to.equal( null );
} );
} );
describe( 'adding multiple tokens when pasting', function() {
it( 'should add multiple comma-separated tokens when pasting', function() {
setText( 'baz, quux, wut' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz', 'quux' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( ' wut' );
setText( 'wut,' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz', 'quux', 'wut' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( '' );
} );
it( 'should add multiple tab-separated tokens when pasting', function() {
setText( 'baz\tquux\twut' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz', 'quux' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( 'wut' );
} );
it( 'should not duplicate tokens when pasting', function() {
setText( 'baz \tbaz, quux \tquux,quux , wut \twut, wut' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz', 'quux', 'wut' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( ' wut' );
} );
it( 'should skip empty tokens at the beginning of a paste', function() {
setText( ', ,\t \t ,,baz, quux' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( ' quux' );
} );
it( 'should skip empty tokens at the beginning of a paste', function() {
setText( ', ,\t \t ,,baz, quux' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( ' quux' );
} );
it( 'should skip empty tokens in the middle of a paste', function() {
setText( 'baz, ,\t \t ,,quux' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( 'quux' );
} );
it( 'should skip empty tokens at the end of a paste', function() {
setText( 'baz, quux, ,\t \t ,, ' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo', 'bar', 'baz', 'quux' ] );
expect( textInputNode.prop( 'value' ) ).to.equal( ' ' );
} );
} );
describe( 'removing tokens', function() {
it( 'should remove tokens when X icon clicked', function() {
tokenFieldNode
.find( '.token-field__remove-token' )
.first()
.simulate( 'click' );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'bar' ] );
} );
it( 'should remove the token to the left when backspace pressed', function() {
sendKeyDown( keyCodes.backspace );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'foo' ] );
} );
it( 'should remove the token to the right when delete pressed', function() {
sendKeyDown( keyCodes.leftArrow );
sendKeyDown( keyCodes.leftArrow );
sendKeyDown( keyCodes.delete );
expect( wrapper.state( 'tokens' ) ).to.deep.equal( [ 'bar' ] );
} );
} );
} );