Skip to content

Commit 1b70844

Browse files
committed
refactor: adopt oscd-test-utils
1 parent c8013ad commit 1b70844

File tree

8 files changed

+893
-1027
lines changed

8 files changed

+893
-1027
lines changed

XMLEditor.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { expect } from "@open-wc/testing";
44
import { assert, property } from "fast-check";
55

66
import {
7-
sclDocString,
87
testDocs,
98
UndoRedoTestCase,
109
undoRedoTestCases,
11-
} from "./testHelpers.js";
10+
} from "@omicronenergy/oscd-test-utils/arbitraries.js";
11+
12+
import { sclDocString } from "@omicronenergy/oscd-test-utils/scl-sample-docs.js";
13+
14+
import { Commit, EditV2, Transactor } from "@omicronenergy/oscd-api";
1215

1316
import {
14-
Commit,
15-
EditV2,
16-
isSetAttributes,
1717
isSetTextContent,
18-
Transactor,
19-
} from "@omicronenergy/oscd-api";
18+
isSetAttributes,
19+
} from "@omicronenergy/oscd-api/utils.js";
2020

2121
import { XMLEditor } from "./XMLEditor.js";
2222

handleEdit.spec.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,28 @@ import {
55
insert,
66
isValidInsert,
77
remove,
8-
sclDocString,
98
setAttributes,
109
setTextContent,
1110
testDocs,
1211
UndoRedoTestCase,
1312
undoRedoTestCases,
1413
xmlAttributeName,
15-
} from "./testHelpers.js";
14+
} from "@omicronenergy/oscd-test-utils/arbitraries.js";
15+
16+
import { sclDocString } from "@omicronenergy/oscd-test-utils/scl-sample-docs.js";
1617

1718
import { EditV2, Insert } from "@omicronenergy/oscd-api";
19+
import { isEditV2 } from "@omicronenergy/oscd-api/utils.js";
20+
1821
import { handleEdit } from "./handleEdit.js";
1922

2023
import { assert, property } from "fast-check";
2124

25+
it("fails at distinguishing EditV2", () => {
26+
expect(isEditV2([{ node: "notanode", parent: "notanode", reference: 42 }])).to
27+
.be.false;
28+
});
29+
2230
describe("handleEdit", () => {
2331
let sclDoc: XMLDocument;
2432

@@ -190,29 +198,37 @@ describe("handleEdit", () => {
190198
testDocs.chain(([{ nodes }]) => setAttributes(nodes)),
191199
(edit) => {
192200
handleEdit(edit);
193-
const attributesHandledCorrectly = Object.entries(edit.attributes)
194-
.filter(([name]) => xmlAttributeName.test(name))
195-
.map((entry) => entry as [string, string | null])
196-
.every(
197-
([name, value]) => edit.element.getAttribute(name) === value,
198-
);
199-
const attributesNSHandledCorrectly = Object.entries(
200-
edit.attributesNS,
201-
)
202-
.map((entry) => entry as [string, Record<string, string | null>])
203-
.every(([ns, attributes]) => {
204-
const unprefixedAttributes = Object.fromEntries(
205-
Object.entries(attributes)
206-
.filter(([name]) => xmlAttributeName.test(name))
207-
.map((entry) => entry as [string, string | null])
208-
.map(([name, value]) => [name.split(":", 2).pop(), value])
209-
.filter(([name]) => name),
210-
);
211-
return Object.entries(unprefixedAttributes).every(
212-
([name, value]) =>
213-
edit.element.getAttributeNS(ns, name!) === value,
214-
);
215-
});
201+
const attributesHandledCorrectly = edit.attributes
202+
? Object.entries(edit.attributes)
203+
.filter(([name]) => xmlAttributeName.test(name))
204+
.map((entry) => entry as [string, string | null])
205+
.every(
206+
([name, value]) =>
207+
edit.element.getAttribute(name) === value,
208+
)
209+
: true;
210+
const attributesNSHandledCorrectly = edit.attributesNS
211+
? Object.entries(edit.attributesNS)
212+
.map(
213+
(entry) => entry as [string, Record<string, string | null>],
214+
)
215+
.every(([ns, attributes]) => {
216+
const unprefixedAttributes = Object.fromEntries(
217+
Object.entries(attributes)
218+
.filter(([name]) => xmlAttributeName.test(name))
219+
.map((entry) => entry as [string, string | null])
220+
.map(([name, value]) => [
221+
name.split(":", 2).pop(),
222+
value,
223+
])
224+
.filter(([name]) => name),
225+
);
226+
return Object.entries(unprefixedAttributes).every(
227+
([name, value]) =>
228+
edit.element.getAttributeNS(ns, name!) === value,
229+
);
230+
})
231+
: true;
216232
return attributesHandledCorrectly && attributesNSHandledCorrectly;
217233
},
218234
),
@@ -242,7 +258,7 @@ describe("handleEdit", () => {
242258
const ed = handleEdit(a);
243259
undoEdits.unshift(ed);
244260
});
245-
if (edits.length) handleEdit(undoEdits);
261+
handleEdit(undoEdits);
246262
expect(doc1).to.satisfy((doc: XMLDocument) =>
247263
doc.isEqualNode(oldDoc1),
248264
);

handleEdit.ts

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import {
22
EditV2,
33
Insert,
4-
isComplex,
5-
isInsert,
6-
isRemove,
7-
isSetAttributes,
8-
isSetTextContent,
94
Remove,
105
SetAttributes,
116
SetTextContent,
127
} from "@omicronenergy/oscd-api";
138

9+
import {
10+
isComplexEditV2,
11+
isInsert,
12+
isRemove,
13+
isSetAttributes,
14+
isSetTextContent,
15+
} from "@omicronenergy/oscd-api/utils.js";
16+
1417
function handleSetTextContent({
1518
element,
1619
textContent,
@@ -32,65 +35,66 @@ function handleSetTextContent({
3235

3336
function handleSetAttributes({
3437
element,
35-
attributes,
36-
attributesNS,
38+
attributes = {},
39+
attributesNS = {},
3740
}: SetAttributes): SetAttributes {
3841
const oldAttributes = { ...attributes };
3942
const oldAttributesNS = { ...attributesNS };
4043

4144
// save element's non-prefixed attributes for undo
42-
Object.keys(attributes)
43-
.reverse()
44-
.forEach((name) => {
45-
oldAttributes[name] = element.getAttribute(name);
46-
});
45+
if (attributes)
46+
Object.keys(attributes)
47+
.reverse()
48+
.forEach((name) => {
49+
oldAttributes[name] = element.getAttribute(name);
50+
});
4751

4852
// change element's non-prefixed attributes
49-
for (const entry of Object.entries(attributes)) {
50-
try {
51-
const [name, value] = entry as [string, string | null];
52-
if (value === null) element.removeAttribute(name);
53-
else element.setAttribute(name, value);
54-
} catch (_e) {
55-
// undo nothing if update didn't work on this attribute
56-
delete oldAttributes[entry[0]];
53+
if (attributes)
54+
for (const entry of Object.entries(attributes)) {
55+
try {
56+
const [name, value] = entry as [string, string | null];
57+
if (value === null) element.removeAttribute(name);
58+
else element.setAttribute(name, value);
59+
} catch (_e) {
60+
// undo nothing if update didn't work on this attribute
61+
delete oldAttributes[entry[0]];
62+
}
5763
}
58-
}
5964

6065
// save element's namespaced attributes for undo
61-
Object.entries(attributesNS).forEach(([ns, attrs]) => {
62-
Object.keys(attrs!)
63-
.reverse()
64-
.forEach((name) => {
65-
oldAttributesNS[ns] = {
66-
...oldAttributesNS[ns],
67-
[name]: element.getAttributeNS(ns, name.split(":").pop()!),
68-
};
69-
});
70-
Object.keys(attrs!).forEach((name) => {
71-
delete oldAttributesNS[ns]![name];
66+
if (attributesNS)
67+
Object.entries(attributesNS).forEach(([ns, attrs]) => {
68+
Object.keys(attrs!)
69+
.reverse()
70+
.forEach((name) => {
71+
oldAttributesNS[ns] = {
72+
...oldAttributesNS[ns],
73+
[name]: element.getAttributeNS(ns, name.split(":").pop()!),
74+
};
75+
});
7276
});
73-
});
7477

7578
// change element's namespaced attributes
76-
for (const nsEntry of Object.entries(attributesNS)) {
77-
const [ns, attrs] = nsEntry as [
78-
string,
79-
Partial<Record<string, string | null>>,
80-
];
81-
for (const entry of Object.entries(attrs)) {
82-
try {
83-
const [name, value] = entry as [string, string | null];
84-
if (value === null) {
85-
element.removeAttributeNS(ns, name.split(":").pop()!);
86-
} else {
87-
element.setAttributeNS(ns, name, value);
79+
if (attributesNS)
80+
for (const nsEntry of Object.entries(attributesNS)) {
81+
const [ns, attrs] = nsEntry as [
82+
string,
83+
Partial<Record<string, string | null>>,
84+
];
85+
for (const entry of Object.entries(attrs)) {
86+
try {
87+
const [name, value] = entry as [string, string | null];
88+
if (value === null) {
89+
element.removeAttributeNS(ns, name.split(":").pop()!);
90+
} else {
91+
element.setAttributeNS(ns, name, value);
92+
}
93+
} catch (_e) {
94+
delete oldAttributesNS[ns]![entry[0]];
8895
}
89-
} catch (_e) {
90-
delete oldAttributesNS[ns]![entry[0]];
9196
}
9297
}
93-
}
9498

9599
return {
96100
element,
@@ -141,13 +145,11 @@ export function handleEdit(edit: EditV2): EditV2 {
141145
if (isRemove(edit)) return handleRemove(edit);
142146
if (isSetAttributes(edit)) return handleSetAttributes(edit);
143147
if (isSetTextContent(edit)) return handleSetTextContent(edit);
144-
if (isComplex(edit))
148+
if (isComplexEditV2(edit))
145149
return edit
146150
.map((edit) => handleEdit(edit))
147151
.reverse()
148152
.flat(Infinity as 1);
149153

150-
console.error(`Invalid edit provided: ${edit}`);
151-
152154
return [];
153155
}

oscd-editor.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
export {
2+
Commit,
3+
CommitOptions,
24
EditV2,
35
Insert,
46
Remove,
57
SetAttributes,
68
SetTextContent,
7-
isComplex,
8-
isEditV2,
9-
isInsert,
10-
isRemove,
11-
isSetAttributes,
12-
isSetTextContent,
9+
TransactedCallback,
10+
Transactor,
1311
} from "@omicronenergy/oscd-api";
1412

15-
export { Edit, Update, isEdit } from "@omicronenergy/oscd-api";
16-
1713
export { handleEdit } from "./handleEdit.js";
1814

19-
export {
20-
complexEdit,
21-
edit,
22-
remove,
23-
setAttributes,
24-
setTextContent,
25-
simpleEdit,
26-
} from "./testHelpers.js";
27-
2815
export { XMLEditor } from "./XMLEditor.js";
29-
30-
export type {
31-
Commit,
32-
CommitOptions,
33-
Transactor,
34-
TransactedCallback,
35-
} from "@omicronenergy/oscd-api";

0 commit comments

Comments
 (0)