Skip to content

Commit

Permalink
test: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clepski committed Jan 10, 2025
1 parent 28e32af commit 3b40cbe
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 83 deletions.
148 changes: 148 additions & 0 deletions packages/openscd/test/unit/edit-action-to-v1-converter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { html, fixture, expect } from '@open-wc/testing';

import {
Create,
Delete,
EditorAction,
isCreate,
isDelete,
isMove,
isReplace,
isSimple,
isUpdate,
Move,
Replace,
SimpleAction,
Update,
createUpdateAction
} from '@openscd/core/foundation/deprecated/editor.js';
import { Edit, Insert, Remove, Update as UpdateV2 } from '@openscd/core';

import { convertEditActiontoV1 } from '../../src/addons/editor/edit-action-to-v1-converter.js';


describe('edit-action-to-v1-converter', () => {
const doc = new DOMParser().parseFromString(
`
<Substation name="sub">
<Bay name="bay" desc="desc"></Bay>
</Substation>
<Substation name="sub2">
</Substation>
`,
'application/xml'
);
const substation = doc.querySelector('Substation')!;
const substation2 = doc.querySelector('Substation[name="sub2"]')!;
const bay = doc.querySelector('Bay')!;

it('should convert delete to remove', () => {
const deleteAction: Delete = {
old: {
parent: substation,
element: bay
}
};

const remove = convertEditActiontoV1(deleteAction);

const expectedRemove: Remove = {
node: bay
};

expect(remove).to.deep.equal(expectedRemove);
});

it('should convert create to insert', () => {
const newBay = doc.createElement('Bay');
newBay.setAttribute('name', 'bay2');

const createAction: Create = {
new: {
parent: substation,
element: newBay
}
};

const insert = convertEditActiontoV1(createAction);

const expectedInsert: Insert = {
parent: substation,
node: newBay,
reference: null
};

expect(insert).to.deep.equal(expectedInsert);
});

it('should convert update to updateV2', () => {
const newAttributes = {
name: 'newBayName',
};
const updateAction = createUpdateAction(bay, newAttributes);

const updateV2 = convertEditActiontoV1(updateAction);

const expectedUpdateV2: UpdateV2 = {
element: bay,
attributes: {
...newAttributes,
desc: null
}
};

expect(updateV2).to.deep.equal(expectedUpdateV2);
});

it('should convert move to insert', () => {
const moveAction: Move = {
old: {
parent: substation,
element: bay,
reference: null
},
new: {
parent: substation2,
reference: null
}
};

const insert = convertEditActiontoV1(moveAction);

const expectedInsert: Insert = {
parent: substation2,
node: bay,
reference: null
};

expect(insert).to.deep.equal(expectedInsert);
});

it('should convert replace to complex action with remove and insert', () => {
const ied = doc.createElement('IED');
ied.setAttribute('name', 'ied');

const replace: Replace = {
old: {
element: bay
},
new: {
element: ied
}
};

const [ remove, insert ] = convertEditActiontoV1(replace) as Edit[];

const expectedRemove: Remove = {
node: bay
};
const expectedInsert: Insert = {
parent: substation,
node: ied,
reference: bay.nextSibling
};

expect(remove).to.deep.equal(expectedRemove);
expect(insert).to.deep.equal(expectedInsert);
});
});
144 changes: 61 additions & 83 deletions packages/openscd/test/unit/edit-v1-to-v2-converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ import {
Update,
createUpdateAction
} from '@openscd/core/foundation/deprecated/editor.js';
import { Edit, Insert, Remove, Update as UpdateV2 } from '@openscd/core';
import { Edit, Insert, InsertV2, Remove, Update as UpdateV1, RemoveV2, SetAttributesV2 } from '@openscd/core';

import { convertEditActiontoV1 } from '../../src/addons/editor/edit-action-to-v1-converter.js';
import { convertEditV1toV2 } from '../../src/addons/editor/edit-v1-to-v2-converter';


describe('edit-v1-to-v2-converter', () => {
const nsXsi = 'urn:example.com';

const doc = new DOMParser().parseFromString(
`
<Substation name="sub">
<Substation name="sub" xmlns:xsi="${nsXsi}">
<Bay name="bay" desc="desc"></Bay>
</Substation>
<Substation name="sub2">
Expand All @@ -35,114 +37,90 @@ describe('edit-v1-to-v2-converter', () => {
const substation = doc.querySelector('Substation')!;
const substation2 = doc.querySelector('Substation[name="sub2"]')!;
const bay = doc.querySelector('Bay')!;

it('should convert delete to remove', () => {
const deleteAction: Delete = {
old: {
parent: substation,
element: bay
}

it('should keep remove as is', () => {
const remove: Remove = {
node: bay
};

const remove = convertEditActiontoV1(deleteAction);

const expectedRemove: Remove = {
const removeV2 = convertEditV1toV2(remove);
const expectedRemoveV2: RemoveV2 = {
node: bay
};

expect(remove).to.deep.equal(expectedRemove);
expect(removeV2).to.deep.equal(expectedRemoveV2);
});

it('should convert create to insert', () => {
it('should keep insert as is', () => {
const newBay = doc.createElement('Bay');
newBay.setAttribute('name', 'bay2');

const createAction: Create = {
new: {
parent: substation,
element: newBay
}
};

const insert = convertEditActiontoV1(createAction);

const expectedInsert: Insert = {
const insert: Insert = {
node: newBay,
parent: substation,
reference: null
};

const insertV2 = convertEditV1toV2(insert);

const expectedInsertV2: InsertV2 = {
node: newBay,
parent: substation,
reference: null
};

expect(insert).to.deep.equal(expectedInsert);
expect(insertV2).to.deep.equal(expectedInsertV2);
});

it('should convert update to updateV2', () => {
it('should convert update to set attributes', () => {
const newAttributes = {
name: 'newBayName',
};
const updateAction = createUpdateAction(bay, newAttributes);

const updateV2 = convertEditActiontoV1(updateAction);

const expectedUpdateV2: UpdateV2 = {
const update: UpdateV1 = {
element: bay,
attributes: {
...newAttributes,
desc: null
}
attributes: newAttributes
}

const setAttributesV2 = convertEditV1toV2(update);

const expectedSetAttributesV2: SetAttributesV2 = {
element: bay,
attributes: newAttributes,
attributesNS: {}
};

expect(updateV2).to.deep.equal(expectedUpdateV2);
expect(setAttributesV2).to.deep.equal(expectedSetAttributesV2);
});

it('should convert move to insert', () => {
const moveAction: Move = {
old: {
parent: substation,
element: bay,
reference: null
},
new: {
parent: substation2,
reference: null
it('shoudl convert update with namespaced attributes', () => {
const newAttributes = {
name: 'newBayName',
type: {
value: 'new value',
namespaceURI: nsXsi
}
};

const insert = convertEditActiontoV1(moveAction);

const expectedInsert: Insert = {
parent: substation2,
node: bay,
reference: null
};

expect(insert).to.deep.equal(expectedInsert);
});
const update: UpdateV1 = {
element: bay,
attributes: newAttributes
}

it('should convert replace to complex action with remove and insert', () => {
const ied = doc.createElement('IED');
ied.setAttribute('name', 'ied');
const setAttributesV2 = convertEditV1toV2(update);

const replace: Replace = {
old: {
element: bay
const expectedSetAttributesV2: SetAttributesV2 = {
element: bay,
attributes: {
name: 'newBayName'
},
new: {
element: ied
attributesNS: {
[nsXsi]: {
type: 'new value'
}
}
};

const [ remove, insert ] = convertEditActiontoV1(replace) as Edit[];

const expectedRemove: Remove = {
node: bay
};
const expectedInsert: Insert = {
parent: substation,
node: ied,
reference: bay.nextSibling
};

expect(remove).to.deep.equal(expectedRemove);
expect(insert).to.deep.equal(expectedInsert);
expect(setAttributesV2).to.deep.equal(expectedSetAttributesV2);
});
});

0 comments on commit 3b40cbe

Please sign in to comment.