-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction-tree.spec.ts
More file actions
210 lines (178 loc) · 6.46 KB
/
action-tree.spec.ts
File metadata and controls
210 lines (178 loc) · 6.46 KB
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
import { expect, fixture, html } from '@open-wc/testing';
import { type SinonSpy, spy } from 'sinon';
import './action-tree.js';
import type { ActionTree, TreeNode } from './ActionTree.js';
function timeout(ms: number) {
return new Promise(res => {
setTimeout(res, ms * 1);
});
}
mocha.timeout(4000 * 1);
const editCallbackSpy = spy();
const testTree: TreeNode = {
name: 'IED1',
children: [{
name: 'LD1',
children: [
{
name: 'LLN0',
children: [
{
name: 'Beh',
leaf: [
{ val: 0 },
{ edit: editCallbackSpy, val: 1 },
{ val: 4 }
],
}
]
},
{
name: 'MMXU1',
children: [
{
name: 'A',
children: [
{
name: 'phsA',
leaf: [
{ val: 33.44 },
{ edit: editCallbackSpy, val: 55.44 },
{ val: 66.44 }
],
}
]
}
]
}
]
}, {
name: 'LD2',
children: [
{
name: 'LLN0',
children: [
{
name: 'Beh',
leaf: [{ val: 0 }]
},
{
name: 'EmptyNode',
children: [] // Node with NO leaf and empty children - should fire unfold event
}
]
}
]
}, {
name: 'LD3',
children: [] // Empty children - should fire unfold event
}]
};
describe('ActionTree Component', () => {
describe('custom events', () => {
let tree: ActionTree;
let unfoldEventSpy: SinonSpy;
beforeEach(async () => {
tree = await fixture(
html`<action-tree
.data=${testTree}
></action-tree>`
);
unfoldEventSpy = spy();
tree.addEventListener('unfold', unfoldEventSpy);
editCallbackSpy.resetHistory();
await timeout(200);
});
it('fires unfold event when expanding empty children array', async () => {
// Find the fold icon for LD3 which has empty children array
const ld3FoldIcon = tree.shadowRoot?.querySelector('.tree-row:last-child .tree-fold');
// eslint-disable-next-line no-unused-expressions
expect(ld3FoldIcon).to.exist;
// Click to unfold the empty children
ld3FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
// eslint-disable-next-line no-unused-expressions
expect(unfoldEventSpy).to.have.been.calledOnce;
expect(unfoldEventSpy.firstCall.args[0].detail.name).to.equal('LD3');
});
it('does not fire unfold event when expanding nodes with actual children', async () => {
unfoldEventSpy.resetHistory();
// Click on LD1 which has actual children (not empty array)
const ld1FoldIcon = tree.shadowRoot?.querySelectorAll('.tree-fold')[1]; // LD1 fold icon
ld1FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
// Should not fire unfold event since LD1 has actual children
// eslint-disable-next-line no-unused-expressions
expect(unfoldEventSpy).to.not.have.been.called;
});
it('event detail contains the correct node information', async () => {
// Click on LD3 which has empty children
const ld3FoldIcon = tree.shadowRoot?.querySelector('.tree-row:last-child .tree-fold');
ld3FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
const eventDetail = unfoldEventSpy.firstCall.args[0].detail;
expect(eventDetail.name).to.equal('LD3');
expect(eventDetail.children).to.deep.equal([]);
// eslint-disable-next-line no-unused-expressions
expect(eventDetail.leaf).to.be.undefined;
});
});
describe('edit callbacks', () => {
let tree: ActionTree;
beforeEach(async () => {
tree = await fixture(
html`<action-tree
.data=${testTree}
></action-tree>`
);
editCallbackSpy.resetHistory();
await timeout(200);
});
it('triggers edit callback when edit button is clicked', async () => {
// First expand to make edit buttons visible
// Expand LD1
const ld1FoldIcon = tree.shadowRoot?.querySelectorAll('.tree-fold')[1];
ld1FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
// Expand LLN0 under LD1
const lln0FoldIcon = tree.shadowRoot?.querySelectorAll('.tree-fold')[2];
lln0FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
// Find and click the edit button for Beh's second value
const editButton = tree.shadowRoot?.querySelector('icon-button');
// eslint-disable-next-line no-unused-expressions
expect(editButton).to.exist;
editButton?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
// eslint-disable-next-line no-unused-expressions
expect(editCallbackSpy).to.have.been.calledOnce;
});
it('edit callbacks are called independently for different values', async () => {
// Expand LD1 -> LLN0 to access Beh
const ld1FoldIcon = tree.shadowRoot?.querySelectorAll('.tree-fold')[1];
ld1FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
const lln0FoldIcon = tree.shadowRoot?.querySelectorAll('.tree-fold')[2];
lln0FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
// Expand MMXU1 -> A to access phsA
const mmxu1FoldIcon = tree.shadowRoot?.querySelectorAll('.tree-fold')[4]; // MMXU1 under LD1
mmxu1FoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
const aFoldIcon = tree.shadowRoot?.querySelectorAll('.tree-fold')[5]; // A under MMXU1
aFoldIcon?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(100);
// Find all edit buttons
const editButtons = tree.shadowRoot?.querySelectorAll('icon-button');
expect(editButtons?.length).to.be.greaterThan(1);
// Click first edit button
editButtons?.[0]?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(50);
// Click second edit button
editButtons?.[1]?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
await timeout(50);
// Both callbacks should have been called
expect(editCallbackSpy.callCount).to.equal(2);
});
});
});