|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-expressions */ |
| 2 | +import { expect } from '@open-wc/testing'; |
| 3 | + |
| 4 | +import { Insert, Remove, Update } from './editv1.js'; |
| 5 | + |
| 6 | +import { convertEdit } from './convertEdit.js'; |
| 7 | +import { SetAttributes } from './editv2.js'; |
| 8 | + |
| 9 | +const doc = new DOMParser().parseFromString( |
| 10 | + '<SCL><Substation name="AA1"></Substation></SCL>', |
| 11 | + 'application/xml', |
| 12 | +); |
| 13 | + |
| 14 | +const subSt = doc.querySelector('Substation')!; |
| 15 | + |
| 16 | +const removeV1: Remove = { node: subSt }; |
| 17 | + |
| 18 | +const insertV1: Insert = { |
| 19 | + parent: subSt, |
| 20 | + node: doc.createAttribute('VoltageLevel'), |
| 21 | + reference: null, |
| 22 | +}; |
| 23 | + |
| 24 | +const update: Update = { |
| 25 | + element: subSt, |
| 26 | + attributes: { |
| 27 | + name: 'A2', |
| 28 | + desc: null, |
| 29 | + ['__proto__']: 'a string', |
| 30 | + 'myns:attr': { |
| 31 | + value: 'value1', |
| 32 | + namespaceURI: 'http://example.org/myns', |
| 33 | + }, |
| 34 | + 'myns:attr2': { |
| 35 | + value: 'value1', |
| 36 | + namespaceURI: 'http://example.org/myns', |
| 37 | + }, |
| 38 | + attr: { |
| 39 | + value: 'value2', |
| 40 | + namespaceURI: 'http://example.org/myns2', |
| 41 | + }, |
| 42 | + attr2: { |
| 43 | + value: 'value2', |
| 44 | + namespaceURI: 'http://example.org/myns2', |
| 45 | + }, |
| 46 | + attr3: { |
| 47 | + value: 'value3', |
| 48 | + namespaceURI: null, |
| 49 | + }, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +const setAttributes: SetAttributes = { |
| 54 | + element: subSt, |
| 55 | + attributes: { |
| 56 | + name: 'A2', |
| 57 | + desc: null, |
| 58 | + }, |
| 59 | + attributesNS: { |
| 60 | + 'http://example.org/myns': { |
| 61 | + 'myns:attr': 'value1', |
| 62 | + 'myns:attr2': 'value1', |
| 63 | + }, |
| 64 | + 'http://example.org/myns2': { |
| 65 | + attr: 'value2', |
| 66 | + attr2: 'value2', |
| 67 | + }, |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +const invalidEdit = { someWrongKey: 'someValue' } as unknown as Update; |
| 72 | + |
| 73 | +describe('convertEditToEditV2', () => { |
| 74 | + it('passes through a Remove edit', () => |
| 75 | + expect(convertEdit(removeV1)).to.deep.equal(removeV1)); |
| 76 | + |
| 77 | + it('passes through a Insert edit', () => |
| 78 | + expect(convertEdit(insertV1)).to.deep.equal(insertV1)); |
| 79 | + |
| 80 | + it('converts Update to SetAttributes', () => |
| 81 | + expect(convertEdit(update)).to.deep.equal(setAttributes)); |
| 82 | + |
| 83 | + it('converts complex edits', () => { |
| 84 | + const editsV1 = [removeV1, insertV1, update]; |
| 85 | + |
| 86 | + const editsV2 = convertEdit(editsV1); |
| 87 | + |
| 88 | + const [removeV2, insertV2, updateV2] = Array.from( |
| 89 | + editsV2 as Array<Remove | Insert | SetAttributes>, |
| 90 | + ); |
| 91 | + |
| 92 | + expect(removeV1).to.deep.equal(removeV2); |
| 93 | + expect(insertV1).to.deep.equal(insertV2); |
| 94 | + expect(updateV2).to.deep.equal(setAttributes); |
| 95 | + }); |
| 96 | + |
| 97 | + it('return empty array for invalid edit', () => { |
| 98 | + expect(convertEdit(invalidEdit)).to.be.an('array').that.is.empty; |
| 99 | + }); |
| 100 | +}); |
0 commit comments