forked from olahol/react-tagsinput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
308 lines (242 loc) · 8.8 KB
/
index.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
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
const jsdom = require("jsdom");
global.document = jsdom.jsdom("");
global.window = document.defaultView;
global.navigator = window.navigator;
const TagsInput = require("../src");
const React = require("react");
const TestUtils = require("react-addons-test-utils");
const assert = require("assert");
class TestComponent extends React.Component {
constructor() {
super()
this.state = {tags: []}
this.change = this.change.bind(this);
this.input = this.input.bind(this);
this.tagsinput = this.tagsinput.bind(this);
}
input() {
return this.refs.tagsinput.refs.input;
}
tagsinput() {
return this.refs.tagsinput;
}
change(tags) {
this.setState({tags});
}
len() {
return this.state.tags.length;
}
tag(i) {
return this.state.tags[i];
}
render() {
return <TagsInput ref="tagsinput" value={this.state.tags} onChange={this.change} {...this.props} />
}
}
function randstring() {
return +new Date() + "";
}
function change(comp, value) {
TestUtils.Simulate.change(comp.input(), {target: {value: value}});
}
function keyDown(comp, code) {
TestUtils.Simulate.keyDown(comp.input(), {keyCode: code});
}
function blur(comp) {
TestUtils.Simulate.blur(comp.input());
}
function click(comp) {
TestUtils.Simulate.click(comp);
}
function add(comp, tag) {
change(comp, tag);
keyDown(comp, 13);
}
function remove(comp) {
change(comp, "");
keyDown(comp, 8);
}
function allTag(comp, tagName) {
return TestUtils.scryRenderedDOMComponentsWithTag(comp, tagName);
}
function allClass(comp, className) {
return TestUtils.scryRenderedDOMComponentsWithClass(comp, className);
}
describe("TagsInput", () => {
describe("basic", () => {
it("should add a tag", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
let tag = randstring();
change(comp, tag);
keyDown(comp, 13);
assert.equal(comp.len(), 1, "there should be one tag");
assert.equal(comp.tag(0), tag, "it should be the tag that was added");
});
it("should remove a tag", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
let tag = randstring();
add(comp, tag);
assert.equal(comp.len(), 1, "there should be one tag");
keyDown(comp, 8);
assert.equal(comp.len(), 0, "there should be no tags");
});
it("should remove a tag by clicking", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
let tag = randstring();
add(comp, tag + "1");
add(comp, tag + "2");
assert.equal(comp.len(), 2, "there should be two tags");
let removes = allTag(comp, "a");
assert.equal(removes.length, 2, "there should be two remove buttons");
click(removes[0]);
assert.equal(comp.len(), 1, "there should be one tag");
});
it("should focus on input when clicking on component div", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
click(comp.tagsinput().refs.div);
});
it("should not add empty tag", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
change(comp, "");
keyDown(comp, 13);
assert.equal(comp.len(), 0, "there should be no tag");
});
});
describe("props", () => {
it("should not add a tag twice if onlyUnique is true", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent onlyUnique={true} />);
let tag = randstring();
change(comp, tag);
keyDown(comp, 13);
change(comp, tag);
keyDown(comp, 13);
assert.equal(comp.len(), 1, "there should be one tag");
});
it("should add a tag twice if onlyUnique is false", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent onlyUnique={false} />);
let tag = randstring();
change(comp, tag);
keyDown(comp, 13);
change(comp, tag);
keyDown(comp, 13);
assert.equal(comp.len(), 2, "there should be two tags");
});
it("should add a tag on key code 44", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent addKeys={[44]} />);
let tag = randstring();
change(comp, tag);
keyDown(comp, 44);
assert.equal(comp.len(), 1, "there should be one tag");
assert.equal(comp.tag(0), tag, "it should be the tag that was added");
});
it("should add a tag on blur, if `this.props.addOnBlur` is true", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent addOnBlur={true} />);
let tag = randstring();
change(comp, tag);
blur(comp);
assert.equal(comp.len(), 1, "there should be one tag");
assert.equal(comp.tag(0), tag, "it should be the tag that was added");
});
it("should not add a tag on blur, if `this.props.addOnBlur` is false", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent addOnBlur={false} />);
let tag = randstring();
change(comp, tag);
blur(comp);
assert.equal(comp.len(), 0, "there should be no tag");
});
it("should not add a tag on blur, if `this.props.addOnBlur` is not defined", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
let tag = randstring();
change(comp, tag);
blur(comp);
assert.equal(comp.len(), 0, "there should be no tag");
});
it("should remove a tag on key code 44", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent removeKeys={[44]} />);
let tag = randstring();
add(comp, tag);
assert.equal(comp.len(), 1, "there should be one tag");
keyDown(comp, 44);
assert.equal(comp.len(), 0, "there should be no tags");
});
it("should limit tags added to 0", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent maxTags={0} />);
let tag = randstring();
add(comp, tag);
add(comp, tag);
assert.equal(comp.len(), 0, "there should be 0 tags");
});
it("should limit tags added to 1", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent maxTags={1} />);
let tag = randstring();
add(comp, tag);
add(comp, tag);
assert.equal(comp.len(), 1, "there should be 1 tags");
});
it("should add props to tag", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent tagProps={{className: "test"}} />);
let tag = randstring();
add(comp, tag);
assert.equal(comp.len(), 1, "there should be one tag");
let tags = allClass(comp, "test");
assert.equal(comp.len(), tags.length, "there should be one tag");
});
it("should add props to input", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent inputProps={{className: "test"}} />);
let inputs = allTag(comp, "input");
assert.equal(inputs[0].className, "test", "class name should be test");
});
it("should fire onChange on input", (done) => {
let tag = randstring()
let onChange = (e) => {
assert.equal(tag, e.target.value, "input tag should be equal");
done();
}
let comp = TestUtils.renderIntoDocument(<TestComponent inputProps={{onChange: onChange}} />);
let inputs = allTag(comp, "input");
change(comp, tag);
});
it("should render tags with renderTag", () => {
let renderTag = (props) => {
return <div key={props.key} className="test"></div>;
};
let comp = TestUtils.renderIntoDocument(<TestComponent renderTag={renderTag} />);
let tag = randstring();
add(comp, tag);
assert.equal(comp.len(), 1, "there should be one tag");
let tags = allClass(comp, "test");
assert.equal(comp.len(), tags.length, "there should be one tag");
});
it("should render input with renderInput", () => {
let renderInput = (props) => {
return <input key={props.key} className="test" />;
};
let comp = TestUtils.renderIntoDocument(<TestComponent renderInput={renderInput} />);
let inputs = allTag(comp, "input");
assert.equal(inputs[0].className, "test", "class name should be test");
});
it("should accept tags only matching validationRegex", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent validationRegex={/a+/} />);
add(comp, 'b');
assert.equal(comp.len(), 0, "there should be no tags");
add(comp, 'a');
assert.equal(comp.len(), 1, "there should be one tag");
});
});
describe("methods", () => {
it("should focus input", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
comp.tagsinput().focus();
});
it("should blur input", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
comp.tagsinput().blur();
});
});
describe("coverage", () => {
it("not remove no existant index", () => {
let comp = TestUtils.renderIntoDocument(<TestComponent />);
comp.tagsinput()._removeTag(1);
});
});
});