|
1 | 1 | /* eslint-disable no-unused-expressions */ |
2 | 2 | import { expect } from '@open-wc/testing'; |
3 | | -import { removeDOsNotInSelection } from './utils.js'; |
| 3 | +import { removeDOsNotInSelection, computeOrphanedRemoves } from './utils.js'; |
| 4 | +import { nsdSpeced } from '../scl-template-update.testfiles.js'; |
4 | 5 |
|
5 | 6 | describe('foundation/utils', () => { |
6 | 7 | describe('removeDOsNotInSelection', () => { |
@@ -85,4 +86,83 @@ describe('foundation/utils', () => { |
85 | 86 | expect(result.tagName).to.equal('LNodeType'); |
86 | 87 | }); |
87 | 88 | }); |
| 89 | + |
| 90 | + describe('computeOrphanedRemoves', () => { |
| 91 | + const mmxuId = 'MMXU$oscd$_c53e78191fabefa3'; |
| 92 | + let dataTypeTemplates: Element; |
| 93 | + |
| 94 | + beforeEach(() => { |
| 95 | + dataTypeTemplates = new DOMParser() |
| 96 | + .parseFromString(nsdSpeced, 'application/xml') |
| 97 | + .querySelector('DataTypeTemplates')!; |
| 98 | + }); |
| 99 | + |
| 100 | + function makeNewMmxu(doNames: string[]): Element { |
| 101 | + const doElements = doNames |
| 102 | + .map(name => { |
| 103 | + const existing = dataTypeTemplates.querySelector( |
| 104 | + `LNodeType[id="${mmxuId}"] > DO[name="${name}"]` |
| 105 | + ); |
| 106 | + return existing ? existing.outerHTML : ''; |
| 107 | + }) |
| 108 | + .join(''); |
| 109 | + return new DOMParser().parseFromString( |
| 110 | + `<LNodeType xmlns="http://www.iec.ch/61850/2003/SCL" lnClass="MMXU" id="${mmxuId}">${doElements}</LNodeType>`, |
| 111 | + 'application/xml' |
| 112 | + ).documentElement; |
| 113 | + } |
| 114 | + |
| 115 | + it('returns the old LNodeType as the first remove', () => { |
| 116 | + const result = computeOrphanedRemoves(dataTypeTemplates, mmxuId, [ |
| 117 | + makeNewMmxu(['Beh']), |
| 118 | + ]); |
| 119 | + |
| 120 | + expect(result).to.have.length.greaterThan(0); |
| 121 | + expect((result[0] as any).node as Element).to.equal( |
| 122 | + dataTypeTemplates.querySelector(`LNodeType[id="${mmxuId}"]`) |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('cascades orphans when A DO is removed: removes A DOType and all its exclusive descendants', () => { |
| 127 | + const result = computeOrphanedRemoves(dataTypeTemplates, mmxuId, [ |
| 128 | + makeNewMmxu(['Beh']), |
| 129 | + ]); |
| 130 | + |
| 131 | + const orphanedIds = result |
| 132 | + .slice(1) // skip old LNodeType remove |
| 133 | + .map((edit: any) => edit.node.getAttribute('id')) |
| 134 | + .filter(id => id !== null); |
| 135 | + expect(orphanedIds).to.include('A$oscd$_41824603f63b26ac'); |
| 136 | + expect(orphanedIds).to.include('phsA$oscd$_995aad120f12c815'); |
| 137 | + expect(orphanedIds).to.include('cVal$oscd$_80272042468595d1'); |
| 138 | + expect(orphanedIds).to.include('units$oscd$_3f2e10def85bfeac'); |
| 139 | + expect(orphanedIds).to.include('mag$oscd$_ed49c2f7a55ad05a'); |
| 140 | + expect(orphanedIds).to.include('SIUnit$oscd$_39f6ca400c633081'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('does not orphan the Beh DOType which is shared with LLN0', () => { |
| 144 | + const result = computeOrphanedRemoves(dataTypeTemplates, mmxuId, [ |
| 145 | + makeNewMmxu(['Beh']), |
| 146 | + ]); |
| 147 | + |
| 148 | + const orphanedIds = result |
| 149 | + .slice(1) // skip old LNodeType remove |
| 150 | + .map((edit: any) => edit.node.getAttribute('id')) |
| 151 | + .filter(id => id !== null); |
| 152 | + |
| 153 | + expect(orphanedIds).to.not.include('Beh$oscd$_c6ed035c8137b35a'); |
| 154 | + expect(orphanedIds).to.not.include('stVal$oscd$_48ba16345b8e7f5b'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('returns only the old LNodeType when the new version keeps all DOs', () => { |
| 158 | + const result = computeOrphanedRemoves(dataTypeTemplates, mmxuId, [ |
| 159 | + makeNewMmxu(['Beh', 'A']), |
| 160 | + ]); |
| 161 | + |
| 162 | + expect(result).to.have.lengthOf(1); |
| 163 | + expect((result[0] as any).node as Element).to.equal( |
| 164 | + dataTypeTemplates.querySelector(`LNodeType[id="${mmxuId}"]`) |
| 165 | + ); |
| 166 | + }); |
| 167 | + }); |
88 | 168 | }); |
0 commit comments