-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Reduce.spec.js
executable file
·258 lines (223 loc) · 7.41 KB
/
Reduce.spec.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
import { mount, shallowMount } from "@vue/test-utils";
import VueSelect from "../../src/components/Select";
describe("When reduce prop is defined", () => {
it("can accept an array of objects and pre-selected value (single)", () => {
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.value,
value: "foo",
options: [{ label: "This is Foo", value: "foo" }]
}
});
expect(Select.vm.selectedValue).toEqual([{ label: "This is Foo", value: "foo" }]);
});
it("can determine if an object is pre-selected", () => {
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.id,
value: "foo",
options: [
{
id: "foo",
label: "This is Foo"
}
]
}
});
expect(
Select.vm.isOptionSelected({
id: "foo",
label: "This is Foo"
})
).toEqual(true);
});
it('can determine if an object is selected after its been chosen', () => {
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.id,
options: [{id: 'foo', label: 'FooBar'}],
},
});
Select.vm.select({id: 'foo', label: 'FooBar'});
expect(Select.vm.isOptionSelected({
id: 'foo',
label: 'This is FooBar',
})).toEqual(true);
});
it("can accept an array of objects and pre-selected values (multiple)", () => {
const Select = shallowMount(VueSelect, {
propsData: {
multiple: true,
reduce: option => option.value,
value: ["foo"],
options: [
{ label: "This is Foo", value: "foo" },
{ label: "This is Bar", value: "bar" }
]
}
});
expect(Select.vm.selectedValue).toEqual([{ label: "This is Foo", value: "foo" }]);
});
it("can deselect a pre-selected object", () => {
const Select = shallowMount(VueSelect, {
propsData: {
multiple: true,
reduce: option => option.value,
options: [
{ label: "This is Foo", value: "foo" },
{ label: "This is Bar", value: "bar" }
]
}
});
Select.vm.$data._value = ['foo', 'bar'];
Select.vm.deselect("foo");
expect(Select.vm.selectedValue).toEqual(["bar"]);
});
it("can deselect an option when multiple is false", () => {
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.value,
options: [
{ label: "This is Foo", value: "foo" },
{ label: "This is Bar", value: "bar" }
]
}
});
Select.vm.deselect("foo");
expect(Select.vm.selectedValue).toEqual([]);
});
it("can use v-model syntax for a two way binding to a parent component", () => {
const Parent = mount({
data: () => ({
reduce: option => option.value,
value: "foo",
options: [
{ label: "This is Foo", value: "foo" },
{ label: "This is Bar", value: "bar" },
{ label: "This is Baz", value: "baz" }
]
}),
template: `<div><v-select :reduce="option => option.value" :options="options" v-model="value"></v-select></div>`,
components: { "v-select": VueSelect }
});
const Select = Parent.vm.$children[0];
expect(Select.value).toEqual("foo");
expect(Select.selectedValue).toEqual([{ label: "This is Foo", value: "foo" }]);
Select.select({ label: "This is Bar", value: "bar" });
expect(Parent.vm.value).toEqual("bar");
});
it("can generate labels using a custom label key", () => {
const Select = shallowMount(VueSelect, {
propsData: {
multiple: true,
reduce: option => option.value,
value: ["CA"],
label: "name",
options: [{ value: "CA", name: "Canada" }, { value: "US", name: "United States" }]
}
});
expect(Select.find(".vs__selected").text()).toContain("Canada");
});
it("can find the original option within this.options", () => {
const optionToFind = { id: 1, label: "Foo" };
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.id,
options: [optionToFind, { id: 2, label: "Bar" }]
}
});
expect(Select.vm.findOptionFromReducedValue(1)).toEqual(optionToFind);
expect(Select.vm.findOptionFromReducedValue(optionToFind)).toEqual(
optionToFind
);
});
it('can work with falsey values', () => {
const option = {value: 0, label: 'No'};
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.value,
options: [option, {value: 1, label: 'Yes'}],
value: 0,
},
});
expect(Select.vm.findOptionFromReducedValue(option)).toEqual(option);
expect(Select.vm.selectedValue).toEqual([option]);
});
it('works with null values', () => {
const option = {value: null, label: 'No'};
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.value,
options: [option, {value: 1, label: 'Yes'}],
value: null,
},
});
expect(Select.vm.findOptionFromReducedValue(option)).toEqual(option);
expect(Select.vm.selectedValue).toEqual([option]);
});
describe("And when a reduced option is a nested object", () => {
it("can determine if an object is pre-selected", () => {
const nestedOption = { value: { nested: true }, label: "foo" };
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.value,
value: {
nested: true
},
options: [nestedOption]
}
});
expect(Select.vm.selectedValue).toEqual([nestedOption]);
});
it("can determine if an object is selected after it is chosen", () => {
const nestedOption = { value: { nested: true }, label: "foo" };
const Select = shallowMount(VueSelect, {
propsData: {
reduce: option => option.value,
options: [nestedOption]
}
});
Select.vm.select(nestedOption);
expect(Select.vm.isOptionSelected(nestedOption)).toEqual(true);
});
});
it("reacts correctly when value property changes", async () => {
const optionToChangeTo = { id: 1, label: "Foo" };
const Select = shallowMount(VueSelect, {
propsData: {
value: 2,
reduce: option => option.id,
options: [optionToChangeTo, { id: 2, label: "Bar" }]
}
});
Select.setProps({ value: optionToChangeTo.id });
await Select.vm.$nextTick();
expect(Select.vm.selectedValue).toEqual([optionToChangeTo]);
});
describe('Reducing Tags', () => {
it('tracks values that have been created by the user', async () => {
const Parent = mount({
data: () => ({selected: null, options: []}),
template: `
<v-select
v-model="selected"
:options="options"
taggable
:reduce="name => name.value"
:create-option="label => ({ label, value: -1 })"
/>
`,
components: {'v-select': VueSelect},
});
const Select = Parent.vm.$children[0];
// When
Select.search = 'hello';
Select.typeAheadSelect();
await Select.$nextTick();
// Then
expect(Select.selectedValue).toEqual([{label: 'hello', value: -1}]);
expect(Select.$refs.selectedOptions.textContent.trim()).toEqual('hello');
expect(Parent.vm.selected).toEqual(-1);
});
});
});