|
1 | 1 | import { html } from 'lit'; |
2 | 2 | import { fixture, expect } from '@open-wc/testing'; |
| 3 | +import { restore, fake, SinonSpy } from 'sinon'; |
| 4 | + |
| 5 | +import { ListItem } from '@material/mwc-list/mwc-list-item.js'; |
3 | 6 |
|
4 | 7 | import TemplateGenerator from './oscd-template-generator.js'; |
5 | 8 |
|
6 | | -customElements.define('oscd-template-generator', TemplateGenerator); |
| 9 | +customElements.define('template-generator', TemplateGenerator); |
| 10 | + |
| 11 | +export const sclDocString = `<?xml version="1.0" encoding="UTF-8"?> |
| 12 | +<SCL version="2007" revision="B" xmlns="http://www.iec.ch/61850/2003/SCL"> |
| 13 | + <DataTypeTemplates></DataTypeTemplates> |
| 14 | +</SCL>`; |
7 | 15 |
|
8 | 16 | describe('TemplateGenerator', () => { |
9 | 17 | let element: TemplateGenerator; |
10 | 18 | beforeEach(async () => { |
11 | | - element = await fixture( |
12 | | - html`<oscd-template-generator></oscd-template-generator>` |
13 | | - ); |
| 19 | + element = await fixture(html`<template-generator></template-generator>`); |
| 20 | + }); |
| 21 | + |
| 22 | + it('displays no action button', () => |
| 23 | + expect(element.shadowRoot?.querySelector('mwc-fab')).to.not.exist); |
| 24 | + |
| 25 | + it('starts with LPHD selected', () => { |
| 26 | + expect(element).to.have.property('lNodeType', 'LPHD'); |
| 27 | + expect(element).shadowDom.to.equalSnapshot(); |
14 | 28 | }); |
15 | 29 |
|
16 | | - it('exists', () => expect(element).to.exist); |
| 30 | + describe('given a loaded document', () => { |
| 31 | + let listener: SinonSpy; |
| 32 | + afterEach(restore); |
| 33 | + beforeEach(async () => { |
| 34 | + listener = fake(); |
| 35 | + element.addEventListener('oscd-edit', listener); |
| 36 | + element.doc = new DOMParser().parseFromString( |
| 37 | + sclDocString, |
| 38 | + 'application/xml' |
| 39 | + ); |
| 40 | + await element.updateComplete; |
| 41 | + }); |
| 42 | + |
| 43 | + it('displays an action button', () => |
| 44 | + expect(element.shadowRoot?.querySelector('mwc-fab')).to.exist); |
| 45 | + |
| 46 | + it('adds Templates on action button click', () => { |
| 47 | + element.shadowRoot?.querySelector('mwc-fab')?.click(); |
| 48 | + |
| 49 | + /* expect five calls for |
| 50 | + - LPHD and its mandatory DOTypes |
| 51 | + - PhyHealth and its mandatory EnumType |
| 52 | + - stVal |
| 53 | + - PhyNam |
| 54 | + - Proxy |
| 55 | + */ |
| 56 | + expect(listener).property('args').to.have.lengthOf(5); |
| 57 | + listener.args.forEach(args => { |
| 58 | + expect(args[0]) |
| 59 | + .property('detail') |
| 60 | + .to.have.property( |
| 61 | + 'parent', |
| 62 | + element.doc?.querySelector('DataTypeTemplates') |
| 63 | + ); |
| 64 | + expect(args[0]).property('detail').to.have.property('node'); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('adds missing DataTypeTemplates section on action button click', () => { |
| 69 | + element.doc?.querySelector('DataTypeTemplates')?.remove(); |
| 70 | + element.shadowRoot?.querySelector('mwc-fab')?.click(); |
| 71 | + |
| 72 | + // expect one more call for the DTT section |
| 73 | + expect(listener).property('args').to.have.lengthOf(6); |
| 74 | + expect(listener.args[0][0]) |
| 75 | + .property('detail') |
| 76 | + .to.have.property('parent', element.doc?.documentElement); |
| 77 | + expect(listener.args[0][0]) |
| 78 | + .property('detail') |
| 79 | + .property('node') |
| 80 | + .to.have.property('tagName', 'DataTypeTemplates'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('adds LNodeTypes, DOTypes, DATypes, and EnumTypes as requested', async () => { |
| 84 | + element.lNodeType = 'LLN0'; |
| 85 | + await element.lNodeTypeUI?.updateComplete; |
| 86 | + await element.updateComplete; |
| 87 | + |
| 88 | + async function selectAll(column: number) { |
| 89 | + const item = element.treeUI.shadowRoot?.querySelector<ListItem>( |
| 90 | + `mwc-list:nth-of-type(${column + 1}) > mwc-list-item:first-of-type` |
| 91 | + ); |
| 92 | + item?.click(); |
| 93 | + await element.treeUI.updateComplete; |
| 94 | + await element.updateComplete; |
| 95 | + } |
| 96 | + |
| 97 | + await selectAll(1); |
| 98 | + await selectAll(2); |
| 99 | + await selectAll(3); |
| 100 | + await selectAll(4); |
| 101 | + await selectAll(5); |
| 102 | + |
| 103 | + element.shadowRoot?.querySelector('mwc-fab')?.click(); |
| 104 | + |
| 105 | + /* expect 30 calls for |
| 106 | + LNodeType LLN0 |
| 107 | + DOType Beh |
| 108 | + Diag |
| 109 | + GrRef |
| 110 | + Health |
| 111 | + InRef |
| 112 | + LEDRs |
| 113 | + Loc |
| 114 | + LocKey |
| 115 | + LocSta |
| 116 | + MltLev |
| 117 | + Mod |
| 118 | + NamPlt |
| 119 | + SwModKey |
| 120 | + DAType origin |
| 121 | + pulseConfig |
| 122 | + SBOw |
| 123 | + Oper |
| 124 | + Cancel |
| 125 | + SBOw |
| 126 | + Oper |
| 127 | + Cancel |
| 128 | + EnumType stVal |
| 129 | + subVal |
| 130 | + orCat |
| 131 | + cmdQual |
| 132 | + ctlModel |
| 133 | + sboClass |
| 134 | + stVal |
| 135 | + subVal |
| 136 | + */ |
| 137 | + expect(listener).property('args').to.have.lengthOf(30); |
| 138 | + const elms = listener.args.map(args => args[0].detail.node); |
| 139 | + expect(elms.filter(e => e.tagName === 'LNodeType')).to.have.lengthOf(1); |
| 140 | + expect(elms.filter(e => e.tagName === 'DOType')).to.have.lengthOf(13); |
| 141 | + expect(elms.filter(e => e.tagName === 'DAType')).to.have.lengthOf(8); |
| 142 | + expect(elms.filter(e => e.tagName === 'EnumType')).to.have.lengthOf(8); |
| 143 | + }).timeout(10000); // selecting 550 paths for a full LLN0 is rather slow. |
| 144 | + }); |
17 | 145 | }); |
0 commit comments