Skip to content

Commit

Permalink
Create method to get node by eid
Browse files Browse the repository at this point in the history
While writing some unit tests using xpaths, I stumbled upon some
limitations of jsdom (the dom implementation used in the tests): It e.g.
does not support upper case characters within either the xpath, which is
something we need for e.g. the eIds (see also:
jsdom/jsdom#2530). Sadly the support for
xpaths is even worse with happy-dom :/

I therefore decided to mock the implementation of the method for
evaluating xpaths in the unit tests using the
 [xpath](https://www.npmjs.com/package/xpath) library.

RISDEV-3932
  • Loading branch information
malte-laukoetter committed May 14, 2024
1 parent 56fe720 commit df29b20
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
12 changes: 11 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"unplugin-icons": "~0.19.0",
"vite": "~5.2.10",
"vitest": "~1.6.0",
"vue-tsc": "~2.0.14"
"vue-tsc": "~2.0.14",
"xpath": "^0.0.34"
}
}
40 changes: 40 additions & 0 deletions frontend/src/services/ldmldeService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { xmlStringToDocument } from "@/services/xmlService"
import { describe, expect, it, vi } from "vitest"
import { getNodeByEid } from "@/services/ldmldeService"
import { useNamespaces } from "xpath"

// The DOM implementation used by our unit tests (jsdom) does not have a good enough xpath support for our tests.
// Therefore, we need to use a different library to evaluate the xpath.
vi.mock("@/services/xmlService", async (importOriginal) => ({
...(await importOriginal<typeof import("@/services/xmlService")>()),
evaluateXPath: (xpath: string, node: Node) =>
useNamespaces({
akn: "http://Inhaltsdaten.LegalDocML.de/1.6/",
})(xpath, node, true),
}))

describe("ldmldeService", () => {
describe("getNodeByEid", () => {
it("should parse xml string", () => {
const document =
xmlStringToDocument(`<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../../../Grammatiken/legalDocML.de.sch" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<akn:akomaNtoso xmlns:akn="http://Inhaltsdaten.LegalDocML.de/1.6/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://Metadaten.LegalDocML.de/1.6/ ../../../Grammatiken/legalDocML.de-metadaten.xsd http://Inhaltsdaten.LegalDocML.de/1.6/ ../../../Grammatiken/legalDocML.de-regelungstextverkuendungsfassung.xsd">
<akn:act name="regelungstext" eId="hauptteil-1">
<akn:body GUID="0B4A8E1F-65EF-4B7C-9E22-E83BA6B73CD8" eId="hauptteil-1">
<akn:article GUID="cdbfc728-a070-42d9-ba2f-357945afef06" eId="hauptteil-1_art-1" period="#geltungszeitgr-1" refersTo="hauptaenderung">
<akn:num GUID="25a9acae-7463-4490-bc3f-8258b629d7e9" eId="hauptteil-1_art-1_bezeichnung-1">
<akn:marker GUID="81c9c481-9427-4f03-9f51-099aa9b2201e" eId="hauptteil-1_art-1_bezeichnung-1_zaehlbez-1" name="1"/>Artikel 1</akn:num>
<akn:heading GUID="92827aa8-8118-4207-9f93-589345f0bab6" eId="hauptteil-1_art-1_überschrift-1">Änderung des
Bundesverfassungsschutzgesetzes</akn:heading>
</akn:article>
</akn:body>
</akn:act>
</akn:akomaNtoso>
`)

const node = getNodeByEid(document, "hauptteil-1_art-1_bezeichnung-1")
expect(node?.textContent?.trim()).to.eq("Artikel 1")
})
})
})
5 changes: 5 additions & 0 deletions frontend/src/services/ldmldeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { evaluateXPath } from "@/services/xmlService"

export function getNodeByEid(xml: Document, eid: string) {
return evaluateXPath(`//*[@eId="${eid}"]`, xml)
}

0 comments on commit df29b20

Please sign in to comment.