-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Creatable-test.js
228 lines (201 loc) · 7.21 KB
/
Creatable-test.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
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
'use strict';
/* global describe, it, beforeEach */
/* eslint react/jsx-boolean-value: 0 */
// Copied from Async-test verbatim; may need to be reevaluated later.
var jsdomHelper = require('../testHelpers/jsdomHelper');
jsdomHelper();
var unexpected = require('unexpected');
var unexpectedDom = require('unexpected-dom');
var unexpectedReact = require('unexpected-react');
var expect = unexpected
.clone()
.installPlugin(unexpectedDom)
.installPlugin(unexpectedReact);
var React = require('react');
var ReactDOM = require('react-dom');
var TestUtils = require('react-addons-test-utils');
var Select = require('../src/Select');
describe('Creatable', () => {
let creatableInstance, creatableNode, filterInputNode, innserSelectInstance, renderer;
beforeEach(() => renderer = TestUtils.createRenderer());
const defaultOptions = [
{ value: 'one', label: 'One' },
{ value: 'two', label: '222' },
{ value: 'three', label: 'Three' },
{ value: 'four', label: 'AbcDef' }
];
function createControl (props = {}) {
props.options = props.options || defaultOptions;
creatableInstance = TestUtils.renderIntoDocument(
<Select.Creatable {...props} />
);
creatableNode = ReactDOM.findDOMNode(creatableInstance);
innserSelectInstance = creatableInstance.select;
findAndFocusInputControl();
};
function findAndFocusInputControl () {
filterInputNode = creatableNode.querySelector('input');
if (filterInputNode) {
TestUtils.Simulate.focus(filterInputNode);
}
};
function typeSearchText (text) {
TestUtils.Simulate.change(filterInputNode, { target: { value: text } });
};
it('should render a decorated Select (with passed through properties)', () => {
createControl({
inputProps: {
className: 'foo'
}
});
expect(creatableNode.querySelector('.Select-input'), 'to have attributes', {
class: ['foo']
});
});
it('should add a placeholder "create..." prompt when filter text is entered that does not match any existing options', () => {
createControl();
typeSearchText('foo');
expect(creatableNode.querySelector('.Select-create-option-placeholder'), 'to have text', Select.Creatable.promptTextCreator('foo'));
});
it('should not show a "create..." prompt if current filter text is an exact match for an existing option', () => {
createControl({
isOptionUnique: () => false
});
typeSearchText('existing');
expect(creatableNode.querySelector('.Select-menu-outer').textContent, 'not to equal', Select.Creatable.promptTextCreator('existing'));
});
it('should filter the "create..." prompt using both filtered options and currently-selected options', () => {
let isOptionUniqueParams;
createControl({
filterOptions: () => [
{ value: 'one', label: 'One' }
],
isOptionUnique: (params) => {
isOptionUniqueParams = params;
},
multi: true,
options: [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' }
],
value: [
{ value: 'three', label: 'Three' }
]
});
typeSearchText('test');
const { options } = isOptionUniqueParams;
const values = options.map(option => option.value);
expect(values, 'to have length', 2);
expect(values, 'to contain', 'one');
expect(values, 'to contain', 'three');
});
it('should not show a "create..." prompt if current filter text is not a valid option (as determined by :isValidNewOption prop)', () => {
createControl({
isValidNewOption: () => false
});
typeSearchText('invalid');
expect(creatableNode.querySelector('.Select-menu-outer').textContent, 'not to equal', Select.Creatable.promptTextCreator('invalid'));
});
it('should create (and auto-select) a new option when placeholder option is clicked', () => {
let selectedOption;
const options = [];
createControl({
onChange: (option) => selectedOption = option,
options
});
typeSearchText('foo');
TestUtils.Simulate.mouseDown(creatableNode.querySelector('.Select-create-option-placeholder'));
expect(options, 'to have length', 1);
expect(options[0].label, 'to equal', 'foo');
expect(selectedOption, 'to be', options[0]);
});
it('should create (and auto-select) a new option when ENTER is pressed while placeholder option is selected', () => {
let selectedOption;
const options = [];
createControl({
onChange: (option) => selectedOption = option,
options,
shouldKeyDownEventCreateNewOption: () => true
});
typeSearchText('foo');
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 13 });
expect(options, 'to have length', 1);
expect(options[0].label, 'to equal', 'foo');
expect(selectedOption, 'to be', options[0]);
});
it('should not create a new option if the placeholder option is not selected but should select the focused option', () => {
const options = [{ label: 'One', value: 1 }];
createControl({
options,
shouldKeyDownEventCreateNewOption: ({ keyCode }) => keyCode === 13
});
typeSearchText('on'); // ['Create option "on"', 'One']
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 40, key: 'ArrowDown' }); // Select 'One'
TestUtils.Simulate.keyDown(filterInputNode, { keyCode: 13 });
expect(options, 'to have length', 1);
});
it('should allow a custom select type to be rendered via the :children property', () => {
let childProps;
createControl({
children: (props) => {
childProps = props;
return <div>faux select</div>;
}
});
expect(creatableNode.textContent, 'to equal', 'faux select');
expect(childProps.allowCreate, 'to equal', true);
});
it('default :children function renders a Select component', () => {
createControl();
expect(creatableNode.className, 'to contain', 'Select');
});
it('default :isOptionUnique function should do a simple equality check for value and label', () => {
const options = [
newOption('foo', 1),
newOption('bar', 2),
newOption('baz', 3)
];
function newOption (label, value) {
return { label, value };
};
function test (option) {
return Select.Creatable.isOptionUnique({
labelKey: 'label',
option,
options,
valueKey: 'value'
});
};
expect(test(newOption('foo', 0)), 'to be', false);
expect(test(newOption('qux', 1)), 'to be', false);
expect(test(newOption('qux', 4)), 'to be', true);
expect(test(newOption('Foo', 11)), 'to be', true);
});
it('default :isValidNewOption function should just ensure a non-empty string is provided', () => {
function test (label) {
return Select.Creatable.isValidNewOption({ label });
};
expect(test(''), 'to be', false);
expect(test('a'), 'to be', true);
expect(test(' '), 'to be', true);
});
it('default :newOptionCreator function should create an option with a :label and :value equal to the label string', () => {
const option = Select.Creatable.newOptionCreator({
label: 'foo',
labelKey: 'label',
valueKey: 'value'
});
expect(option.className, 'to equal', 'Select-create-option-placeholder');
expect(option.label, 'to equal', 'foo');
expect(option.value, 'to equal', 'foo');
});
it('default :shouldKeyDownEventCreateNewOption function should accept TAB, ENTER, and comma keys', () => {
function test (keyCode) {
return Select.Creatable.shouldKeyDownEventCreateNewOption({ keyCode });
};
expect(test(9), 'to be', true);
expect(test(13), 'to be', true);
expect(test(188), 'to be', true);
expect(test(1), 'to be', false);
});
});