-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathslots.spec.ts
211 lines (195 loc) · 6.65 KB
/
slots.spec.ts
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
import { mount } from '@vue/test-utils';
import { RuleSet, Rule, QueryBuilderConfig } from '@/types';
import QueryBuilder from '@/QueryBuilder.vue';
import QueryBuilderGroup from '@/QueryBuilderGroup.vue';
import QueryBuilderRule from '@/QueryBuilderRule.vue';
import Component from '../components/Component.vue';
describe('Testing slot related features', () => {
const propsData: { value: RuleSet, config: QueryBuilderConfig } = {
value: {
operatorIdentifier: 'OR',
children: [
{
identifier: 'txt',
value: 'A',
},
{
identifier: 'txt',
value: 'B',
},
{
identifier: 'txt',
value: 'C',
},
],
},
config: {
operators: [
{
name: 'and',
identifier: 'AND',
},
{
name: 'or',
identifier: 'OR',
},
],
rules: [
{
identifier: 'txt',
name: 'Text Selection',
component: Component,
initialValue: 'foo',
},
{
identifier: 'num',
name: 'Number Selection',
component: Component,
initialValue: 10,
},
],
},
};
it('tests the `groupOperator` slot', () => {
const app = mount(QueryBuilder, {
propsData,
scopedSlots: {
groupOperator: `
<div
slot-scope="props"
class="slot-wrapper"
>
SLOT Operator
<select
class="slot-select"
:value="props.currentOperator"
@change="props.updateCurrentOperator($event.target.value)"
>
<option
v-for="operator in props.operators"
:key="operator.identifier"
:value="operator.identifier"
v-text="operator.name"
/>
</select>
</div>
`,
},
});
const group = app.findComponent(QueryBuilderGroup);
const slot = group.find('.slot-wrapper');
// Check if current operator is selected
const select = slot.find('.slot-select');
expect((select.element as HTMLSelectElement).value).toBe('OR');
// Check if operators are properly rendered
const options = select.findAll('option');
expect(options).toHaveLength(2);
expect((options.at(0).element as HTMLOptionElement).value).toBe('AND');
expect((options.at(0).element as HTMLOptionElement).text).toBe('and');
expect((options.at(1).element as HTMLOptionElement).value).toBe('OR');
expect((options.at(1).element as HTMLOptionElement).text).toBe('or');
// Update operator
options.at(0).setSelected();
expect((group.emitted('query-update') as any)[0][0]).toStrictEqual({ operatorIdentifier: 'AND', children: propsData.value.children });
const expected: RuleSet = JSON.parse(JSON.stringify(propsData.value));
expected.operatorIdentifier = 'AND';
expect((app.emitted('input') as any)[0][0]).toStrictEqual(expected);
});
it('tests the `groupControl` slot', async () => {
const app = mount(QueryBuilder, {
propsData,
scopedSlots: {
groupControl: `
<div
slot-scope="props"
class="slot-wrapper"
>
SLOT
<select>
<option
v-for="rule in props.rules"
:key="rule.identifier"
:value="rule.identifier"
v-text="rule.name"
/>
</select>
<button
@click="props.addRule('txt')"
class="slot-new-rule"
>
Add Rule
</button>
<button
@click="props.newGroup"
class="slot-new-group"
>
Add Group
</button>
</div>
`,
},
});
const slot = app.find('.slot-wrapper');
const group = app.findComponent(QueryBuilderGroup);
// Some data we'll be using for our assertions
const query: RuleSet = JSON.parse(JSON.stringify(propsData.value));
const children = [...propsData.value.children];
// check if rules are properly rendered
const options = slot.findAll('option');
expect(options).toHaveLength(2);
expect((options.at(0).element as HTMLOptionElement).value).toBe('txt');
expect((options.at(0).element as HTMLOptionElement).text).toBe('Text Selection');
expect((options.at(1).element as HTMLOptionElement).value).toBe('num');
expect((options.at(1).element as HTMLOptionElement).text).toBe('Number Selection');
// Add a new rule
slot.find('.slot-new-rule').trigger('click');
children.push({ identifier: 'txt', value: 'foo' });
query.children = [...children];
expect((group.emitted('query-update') as any)[0][0]).toStrictEqual({ operatorIdentifier: 'OR', children });
expect((app.emitted('input') as any)[0][0]).toStrictEqual(query);
// Add new group
app.setProps({ value: { ...query }, config: { ...propsData.config } });
await app.vm.$nextTick();
slot.find('.slot-new-group').trigger('click');
children.push({ operatorIdentifier: 'AND', children: [] });
query.children = [...children];
expect((group.emitted('query-update') as any)[1][0]).toStrictEqual({ operatorIdentifier: 'OR', children });
expect((app.emitted('input') as any)[1][0]).toStrictEqual(query);
});
it('tests the `rule` slot', () => {
const app = mount(QueryBuilder, {
propsData,
scopedSlots: {
rule: `
<div
slot-scope="props"
class="slot-wrapper"
>
SLOT
<component
:is="props.ruleComponent"
:value="props.ruleData"
:identifier="props.ruleIdentifier"
@input="props.updateRuleData"
class="slot-rule"
/>
</div>
`,
},
});
const rule = app.findComponent(QueryBuilderRule);
const slot = rule.find('.slot-wrapper');
const ruleComponent = slot.find('.slot-rule');
// Verify rule slot is properly rendered
expect(ruleComponent.is(Component)).toBeTruthy();
expect(ruleComponent.vm.$props.value).toBe('A');
expect(rule.vm.$props.query.identifier).toBe('txt');
expect(ruleComponent.vm.$props.identifier).toBe('txt');
ruleComponent.vm.$emit('input', 'a');
expect((rule.emitted('query-update') as any)[0][0]).toStrictEqual({ identifier: 'txt', value: 'a' });
// Verify update event propagates
const expected: RuleSet = JSON.parse(JSON.stringify(propsData.value));
(expected.children[0] as Rule).value = 'a';
expect((app.emitted('input') as any)[0][0]).toStrictEqual(expected);
});
});