forked from OpenEnergyTools/scl-template-update
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscd-editor-template-update.spec.ts
More file actions
383 lines (327 loc) · 13.1 KB
/
oscd-editor-template-update.spec.ts
File metadata and controls
383 lines (327 loc) · 13.1 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { fixture, expect, html, waitUntil } from '@open-wc/testing';
import { restore, SinonSpy, spy } from 'sinon';
import {
extension,
lln0Selection,
mmxuExceptSelection,
mmxuSelection,
nsdSpeced,
customDataObjectInvalidCDC,
} from './oscd-editor-template-update.testfiles.js';
import './oscd-editor-template-update.js';
import OscdEditorTemplateUpdate from './oscd-editor-template-update.js';
import { EditEventV2, Insert, Remove } from '@omicronenergy/oscd-api';
import { TreeGrid } from '@openenergytools/tree-grid';
import { XMLEditor } from '@omicronenergy/oscd-editor';
customElements.define('oscd-editor-template-update', OscdEditorTemplateUpdate);
describe('NsdTemplateUpdater', () => {
let plugin: OscdEditorTemplateUpdate;
let editEventListener: SinonSpy;
const editor = new XMLEditor();
beforeEach(async () => {
plugin = await fixture(
html`<oscd-editor-template-update></oscd-editor-template-update>`,
);
editEventListener = spy();
plugin.addEventListener('oscd-edit-v2', (event: EditEventV2) => {
const { edit, title, squash } = event.detail;
editor.commit(edit, { title, squash });
editEventListener(event);
});
await new Promise(res => {
setTimeout(res, 200);
});
});
it('shows notification without loaded doc', () => {
expect(plugin.shadowRoot?.querySelector('h1')).to.exist;
expect(plugin.shadowRoot?.querySelector('tree-grid')).to.not.exist;
expect(plugin.shadowRoot?.querySelector('md-fab')).to.not.exist;
});
describe('given a nsd specced document', () => {
afterEach(restore);
beforeEach(async () => {
const doc = new DOMParser().parseFromString(nsdSpeced, 'application/xml');
plugin.doc = doc;
await plugin.updateComplete;
});
it('displays an action button', () =>
expect(plugin.shadowRoot?.querySelector('md-fab')).to.exist);
it('updates MMXU on action button click', async () => {
const event = {
detail: { id: 'MMXU$oscd$_c53e78191fabefa3' },
} as CustomEvent;
await plugin.onLNodeTypeSelect(event);
await new Promise(resolve => {
setTimeout(resolve, 0);
});
plugin.treeUI.selection = mmxuSelection;
(plugin.shadowRoot?.querySelector('md-fab') as HTMLElement).click();
await plugin.updateComplete;
const inserts = editEventListener.args[0][0].detail.edit;
const removes = editEventListener.args[1][0].detail.edit;
expect(inserts).to.have.lengthOf(5);
expect(removes).to.have.lengthOf(2);
expect(
((inserts[0] as Insert).node as Element).getAttribute('id'),
).to.equal('MMXU$oscd$_b96484e663b92760');
expect(
((inserts[1] as Insert).node as Element).getAttribute('id'),
).to.equal('Beh$oscd$_954939784529ca3d');
expect(
((inserts[2] as Insert).node as Element).getAttribute('id'),
).to.equal('phsB$oscd$_65ee65af9248ae5d');
expect(
((inserts[3] as Insert).node as Element).getAttribute('id'),
).to.equal('A$oscd$_ad714f2a7845e863');
expect(
((inserts[4] as Insert).node as Element).getAttribute('id'),
).to.equal('stVal$oscd$_2ff6286b1710bcc1');
expect(
((removes[0] as Remove).node as Element).getAttribute('id'),
).to.equal('MMXU$oscd$_c53e78191fabefa3');
expect(
((removes[1] as Remove).node as Element).getAttribute('id'),
).to.equal('A$oscd$_41824603f63b26ac');
}).timeout(5000);
it('updates LLN0 on action button click', async () => {
const event = {
detail: { id: 'LLN0$oscd$_85c7ffbe25d80e63' },
} as CustomEvent;
await plugin.onLNodeTypeSelect(event);
plugin.treeUI.selection = lln0Selection; // change selection
await plugin.updateComplete;
(plugin.shadowRoot?.querySelector('md-fab') as HTMLElement).click();
await new Promise(res => {
setTimeout(res, 200);
});
const inserts = editEventListener.args[0][0].detail.edit;
const removes = editEventListener.args[1][0].detail.edit;
expect(inserts).to.have.lengthOf(9);
expect(removes).to.have.lengthOf(5);
expect(
((inserts[0] as Insert).node as Element).getAttribute('id'),
).to.equal('LLN0$oscd$_70973585614987f4');
expect(
((inserts[1] as Insert).node as Element).getAttribute('id'),
).to.equal('Mod$oscd$_ca3ec0d8276151d7');
expect(
((inserts[2] as Insert).node as Element).getAttribute('id'),
).to.equal('origin$oscd$_8c586402c5f97d31');
expect(
((inserts[3] as Insert).node as Element).getAttribute('id'),
).to.equal('SBOw$oscd$_59a179d1c87265eb');
expect(
((inserts[4] as Insert).node as Element).getAttribute('id'),
).to.equal('origin$oscd$_a128160f5df91cfa');
expect(
((inserts[5] as Insert).node as Element).getAttribute('id'),
).to.equal('Oper$oscd$_1c003786901c1473');
expect(
((inserts[6] as Insert).node as Element).getAttribute('id'),
).to.equal('ctlModel$oscd$_40d881a91fe5c769');
expect(
((inserts[7] as Insert).node as Element).getAttribute('id'),
).to.equal('orCat$oscd$_677850ccf85aee7a');
expect(
((inserts[8] as Insert).node as Element).getAttribute('id'),
).to.equal('orCat$oscd$_8f842fc78e972b98');
expect(
((removes[0] as Remove).node as Element).getAttribute('id'),
).to.equal('LLN0$oscd$_85c7ffbe25d80e63');
expect(
((removes[1] as Remove).node as Element).getAttribute('id'),
).to.equal('Mod$oscd$_d63dba598ea9104c');
expect(
((removes[2] as Remove).node as Element).getAttribute('id'),
).to.equal('Oper$oscd$_4974a5c5ec541314');
expect(
((removes[3] as Remove).node as Element).getAttribute('id'),
).to.equal('SBOw$oscd$_4974a5c5ec541314');
expect(
((removes[4] as Remove).node as Element).getAttribute('id'),
).to.equal('ctlModel$oscd$_f80264355419aeff');
}).timeout(5000);
it('does not update with same selection', async () => {
const event = {
detail: { id: 'LLN0$oscd$_85c7ffbe25d80e63' },
} as CustomEvent;
await plugin.onLNodeTypeSelect(event);
expect(editEventListener.callCount, 'call count before').to.equal(0);
(plugin.shadowRoot?.querySelector('md-fab') as HTMLElement).click();
await new Promise(res => {
setTimeout(res, 200);
});
expect(editEventListener.callCount, 'call count after').to.equal(0);
});
it('shows the data loss dialog if (part of) selection is not in tree', async () => {
plugin.selectedLNodeType = plugin.doc!.querySelector('LNodeType')!;
plugin.treeUI.tree = { foo: {} };
plugin.treeUI.selection = { foo: {}, bar: {} };
plugin.treeUI.requestUpdate = () => {};
plugin.nsdSelection = { foo: {} };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(plugin as any).handleUpdateTemplate();
await waitUntil(() => plugin.choiceDialog?.open);
expect(plugin.choiceDialog?.open).to.be.true;
expect(plugin.choiceDialog).shadowDom.to.equalSnapshot();
});
});
describe("given a non nsd spec'd document", () => {
afterEach(restore);
beforeEach(async () => {
const doc = new DOMParser().parseFromString(extension, 'application/xml');
plugin.doc = doc;
await plugin.updateComplete;
});
it('does not load non NSD ln classes', async () => {
const event = { detail: { id: 'invalidLnClass' } } as CustomEvent;
plugin.onLNodeTypeSelect(event);
await new Promise(res => {
setTimeout(res, 50);
});
expect(JSON.stringify(plugin.treeUI.tree)).to.equal('{}');
expect(plugin.warningDialog?.getAttribute('open')).to.not.be.null;
});
it('notifies with LNodeType is referenced', async () => {
const event = {
detail: { id: 'LLN0$oscd$_85c7ffbe25d80e63' },
} as CustomEvent;
plugin.onLNodeTypeSelect(event);
await new Promise(res => {
setTimeout(res, 200);
});
expect(plugin.warningDialog?.getAttribute('open')).to.not.be.null;
expect(
plugin.warningDialog?.querySelector('form')?.textContent,
).to.include(
`The selected logical node type is referenced. This plugin should be used during specification only.`,
);
});
it('updates MMXU on action button click', async () => {
const event = {
detail: { id: 'MMXU$oscd$_c53e78191fabefa3' },
} as CustomEvent;
await plugin.onLNodeTypeSelect(event);
await new Promise(res => {
setTimeout(res, 0);
});
plugin.treeUI.selection = mmxuExceptSelection;
await plugin.updateComplete;
(plugin.shadowRoot?.querySelector('md-fab') as HTMLElement).click();
await plugin.updateComplete;
(
plugin.choiceDialog?.querySelector('.button.proceed') as HTMLElement
).click();
await plugin.updateComplete;
const inserts = editEventListener.args[0][0].detail.edit;
const removes = editEventListener.args[1][0].detail.edit;
expect(inserts).to.have.lengthOf(5);
expect(removes).to.have.lengthOf(1);
expect(
((inserts[0] as Insert).node as Element).getAttribute('id'),
).to.equal('MMXU$oscd$_3027abc2662ec638');
expect(
((inserts[1] as Insert).node as Element).getAttribute('id'),
).to.equal('Beh$oscd$_954939784529ca3d');
expect(
((inserts[2] as Insert).node as Element).getAttribute('id'),
).to.equal('phsB$oscd$_65ee65af9248ae5d');
expect(
((removes[0] as Remove).node as Element).getAttribute('id'),
).to.equal('MMXU$oscd$_c53e78191fabefa3');
}).timeout(5000);
it('updates the selected LNodeType when the description is changed', async () => {
const event = {
detail: { id: 'LLN0$oscd$_85c7ffbe25d80e63' },
} as CustomEvent;
plugin.onLNodeTypeSelect(event);
await new Promise(res => {
setTimeout(res, 0);
});
plugin.lnodeTypeDesc.value = 'New Description';
await plugin.updateComplete;
(plugin.shadowRoot?.querySelector('md-fab') as HTMLElement).click();
await plugin.updateComplete;
expect(editEventListener).to.have.been.called;
});
});
describe('given a document with unsupported CDC', () => {
afterEach(restore);
beforeEach(async () => {
const doc = new DOMParser().parseFromString(
customDataObjectInvalidCDC,
'application/xml',
);
plugin.doc = doc;
await plugin.updateComplete;
const treeUI = plugin.shadowRoot?.querySelector('tree-grid') as TreeGrid;
if (treeUI) {
treeUI.tree = {};
treeUI.requestUpdate = () => {};
}
await treeUI.updateComplete;
});
it('shows a warning dialog when a lnode type has user defined DOs with unsupported CDC', async () => {
const event = {
detail: { id: 'MMXU$oscd$_c53e78191fabefa3' },
} as CustomEvent;
plugin.onLNodeTypeSelect(event);
await new Promise(res => {
setTimeout(res, 0);
});
plugin.treeUI.selection = mmxuSelection;
await plugin.updateComplete;
(plugin.shadowRoot?.querySelector('md-fab') as HTMLElement).click();
await plugin.updateComplete;
expect(plugin.warningDialog?.getAttribute('open')).to.not.be.null;
expect(
plugin.warningDialog?.querySelector('form')?.textContent,
).to.include(
'The selected logical node type contains user-defined data objects with unsupported CDCs.',
);
});
it('adds a data object to the tree when form is valid', async () => {
const dialog = plugin.shadowRoot?.querySelector(
'add-data-object-dialog',
) as HTMLElement & { show: () => void };
dialog.show();
const cdcSelect = dialog.shadowRoot?.querySelector(
'#cdc-type',
) as HTMLSelectElement;
const doNameField = dialog.shadowRoot?.querySelector(
'#do-name',
) as HTMLInputElement;
cdcSelect.value = 'WYE';
doNameField.value = 'TestDO';
cdcSelect.dispatchEvent(new Event('input', { bubbles: true }));
doNameField.dispatchEvent(new Event('input', { bubbles: true }));
// Submit the form
const form = dialog.shadowRoot!.querySelector('form')!;
form.dispatchEvent(
new Event('submit', { bubbles: true, cancelable: true }),
);
await plugin.updateComplete;
expect(plugin.treeUI.tree).to.have.property('TestDO');
expect(plugin.treeUI.tree.TestDO).to.include({
type: 'WYE',
tagName: 'DataObject',
descID: '',
presCond: 'O',
});
});
it('does not add a data object if form is invalid', async () => {
const dialog = plugin.shadowRoot?.querySelector(
'add-data-object-dialog',
) as HTMLElement & { show: () => void };
dialog.show();
await plugin.updateComplete;
const form = dialog.shadowRoot!.querySelector('form')!;
form.dispatchEvent(
new Event('submit', { bubbles: true, cancelable: true }),
);
await plugin.updateComplete;
expect(plugin.treeUI.tree).to.not.have.property('TestDO');
});
});
});