diff --git a/containers/ecr-viewer/src/app/services/formatService.tsx b/containers/ecr-viewer/src/app/services/formatService.tsx index dabe0ddfab..fc7c6c9f24 100644 --- a/containers/ecr-viewer/src/app/services/formatService.tsx +++ b/containers/ecr-viewer/src/app/services/formatService.tsx @@ -415,25 +415,18 @@ export function getFirstNonCommentChild( } /** - * Extracts the `data-id` value from the element or an HTML comment within the child nodes. - * The `data-id` is either expected to be an attribute (`data-id="some-id"`) or in the format: ``. + * Extracts the `data-id` attribute from the element or its ID if there is no `data-id` attribute. * @param elem - The element to search for the `data-id`. * @returns - The extracted `data-id` value if found, otherwise `null`. */ export function getDataId(elem: HTMLLIElement | HTMLTableElement | Element) { - if (elem.getAttribute("data-id")) return elem.getAttribute("data-id"); - for (let i = 0; i < elem.childNodes.length; i++) { - const node = elem.childNodes[i]; - if (node.nodeType === Node.COMMENT_NODE) { - // Extract the comment content and parse out the data-id - const commentText = node.nodeValue?.trim() || ""; - const match = commentText.match(/data-id:\s*([^\s]+)/); - if (match) { - return match[1]; - } - } + if (elem.getAttribute("data-id")) { + return elem.getAttribute("data-id"); + } else if (elem.id) { + return elem.id; + } else { + return null; // Return null if no match is found } - return null; // Return null if no match is found } /** diff --git a/containers/ecr-viewer/src/app/tests/assets/BundleLab.json b/containers/ecr-viewer/src/app/tests/assets/BundleLab.json index b455b7c962..b18eb122c0 100644 --- a/containers/ecr-viewer/src/app/tests/assets/BundleLab.json +++ b/containers/ecr-viewer/src/app/tests/assets/BundleLab.json @@ -47,7 +47,7 @@ "title": "Results", "text": { "status": "generated", - "div": " documented in this encounter" + "div": " documented in this encounter" }, "code": { "coding": [ diff --git a/containers/ecr-viewer/src/app/tests/services/formatService.test.tsx b/containers/ecr-viewer/src/app/tests/services/formatService.test.tsx index d9121b91ee..013c15196a 100644 --- a/containers/ecr-viewer/src/app/tests/services/formatService.test.tsx +++ b/containers/ecr-viewer/src/app/tests/services/formatService.test.tsx @@ -158,9 +158,9 @@ describe("Format Date", () => { describe("formatTablesToJSON", () => { describe("return JSON object given an HTML string", () => { - it("
  • ", () => { + it("
  • ", () => { const htmlString = - "
  • Lab Test
  • ComponentAnalysis Time
    Campylobacter, NAAT01/01/2024 1:00 PM PDT
    Salmonella, NAAT01/01/2024 1:00 PM PDT
    Specimen (Source)Collection TimeReceived Time
    Stool01/01/2024 12:00 PM PDT01/01/2024 12:00 PM PDT
  • "; + "
  • Lab Test
    ComponentAnalysis Time
    Campylobacter, NAAT01/01/2024 1:00 PM PDT
    Salmonella, NAAT01/01/2024 1:00 PM PDT
    Specimen (Source)Collection TimeReceived Time
    Stool01/01/2024 12:00 PM PDT01/01/2024 12:00 PM PDT
  • "; const expectedResult = [ { resultId: "Result.12345", @@ -219,9 +219,70 @@ describe("formatTablesToJSON", () => { expect(result).toEqual(expectedResult); }); - it("", () => { + it("
  • ", () => { const htmlString = - "Lab Test
    ComponentAnalysis Time
    Campylobacter, NAAT01/01/2024 1:00 PM PDT
    Salmonella, NAAT01/01/2024 1:00 PM PDT
    Specimen (Source)Collection TimeReceived Time
    Stool01/01/2024 12:00 PM PDT01/01/2024 12:00 PM PDT
    "; + "
  • Lab Test
    ComponentAnalysis Time
    Campylobacter, NAAT01/01/2024 1:00 PM PDT
    Salmonella, NAAT01/01/2024 1:00 PM PDT
    Specimen (Source)Collection TimeReceived Time
    Stool01/01/2024 12:00 PM PDT01/01/2024 12:00 PM PDT
  • "; + const expectedResult = [ + { + resultId: "Result.12345", + resultName: "Lab Test", + tables: [ + [ + { + Component: { + value: "Campylobacter, NAAT", + metadata: { + id: "Result.12345.Comp1Name", + }, + }, + "Analysis Time": { + value: "01/01/2024 1:00 PM PDT", + metadata: {}, + }, + }, + { + Component: { + value: "Salmonella, NAAT", + metadata: { + id: "Result.12345.Comp2Name", + }, + }, + "Analysis Time": { + value: "01/01/2024 1:00 PM PDT", + metadata: {}, + }, + }, + ], + [ + { + "Specimen (Source)": { + value: "Stool", + metadata: { + id: "Result.12345.Specimen", + }, + }, + "Collection Time": { + value: "01/01/2024 12:00 PM PDT", + metadata: {}, + }, + "Received Time": { + value: "01/01/2024 12:00 PM PDT", + metadata: {}, + }, + }, + ], + ], + }, + ]; + + const result = formatTablesToJSON(htmlString); + + expect(result).toEqual(expectedResult); + }); + + it("", () => { + const htmlString = + "Lab Test
    ComponentAnalysis Time
    Campylobacter, NAAT01/01/2024 1:00 PM PDT
    Salmonella, NAAT01/01/2024 1:00 PM PDT
    Specimen (Source)Collection TimeReceived Time
    Stool01/01/2024 12:00 PM PDT01/01/2024 12:00 PM PDT
    "; const expectedResult = [ { resultId: "Result.12345", @@ -280,6 +341,67 @@ describe("formatTablesToJSON", () => { expect(result).toEqual(expectedResult); }); + it("", () => { + const htmlString = + "Lab Test
    ComponentAnalysis Time
    Campylobacter, NAAT01/01/2024 1:00 PM PDT
    Salmonella, NAAT01/01/2024 1:00 PM PDT
    Specimen (Source)Collection TimeReceived Time
    Stool01/01/2024 12:00 PM PDT01/01/2024 12:00 PM PDT
    "; + const expectedResult = [ + { + resultId: "Result.12345", + resultName: "Lab Test", + tables: [ + [ + { + Component: { + value: "Campylobacter, NAAT", + metadata: { + id: "Result.12345.Comp1Name", + }, + }, + "Analysis Time": { + value: "01/01/2024 1:00 PM PDT", + metadata: {}, + }, + }, + { + Component: { + value: "Salmonella, NAAT", + metadata: { + id: "Result.12345.Comp2Name", + }, + }, + "Analysis Time": { + value: "01/01/2024 1:00 PM PDT", + metadata: {}, + }, + }, + ], + [ + { + "Specimen (Source)": { + value: "Stool", + metadata: { + id: "Result.12345.Specimen", + }, + }, + "Collection Time": { + value: "01/01/2024 12:00 PM PDT", + metadata: {}, + }, + "Received Time": { + value: "01/01/2024 12:00 PM PDT", + metadata: {}, + }, + }, + ], + ], + }, + ]; + + const result = formatTablesToJSON(htmlString); + + expect(result).toEqual(expectedResult); + }); + it("
    ", () => { const tableString = "
    Pending Results
    Name
    test1
    Scheduled Orders
    Name
    test2
    documented as of this encounter\n"; @@ -567,40 +689,22 @@ describe("getDataId", () => { expect(result).toEqual("attribute123"); }); - it("should return the correct data-id from a comment node", () => { - const li = document.createElement("li"); - li.appendChild(document.createComment("data-id: abc123-abc123")); - - const result = getDataId(li); - expect(result).toEqual("abc123-abc123"); - }); - - it("should return null if there is no data-id attribute or comment node", () => { + it("should return the id if there is one", () => { const li = document.createElement("li"); - li.textContent = "No comment or data-id attribute here"; + li.setAttribute("ID", "id123"); const result = getDataId(li); - expect(result).toBeNull(); + expect(result).toEqual("id123"); }); - it("should return null if the comment does not contain a valid data-id", () => { + it("should return null if there is no id or data-id attribute", () => { const li = document.createElement("li"); - li.appendChild(document.createComment("some other comment")); + li.textContent = "No id or data-id attribute here"; const result = getDataId(li); expect(result).toBeNull(); }); - it("should handle multiple child nodes and find the data-id in a comment", () => { - const li = document.createElement("li"); - li.appendChild(document.createTextNode("Some text")); - li.appendChild(document.createComment("data-id: abc123-abc123")); - li.appendChild(document.createElement("span")); - - const result = getDataId(li); - expect(result).toEqual("abc123-abc123"); - }); - it("should return the correct data-id from a table element", () => { const table = document.createElement("table"); table.setAttribute("data-id", "table123"); @@ -608,15 +712,6 @@ describe("getDataId", () => { const result = getDataId(table); expect(result).toEqual("table123"); }); - - it("should prioritize the data-id attribute over a comment node", () => { - const li = document.createElement("li"); - li.setAttribute("data-id", "attribute123"); - li.appendChild(document.createComment("data-id: abc123-abc123")); - - const result = getDataId(li); - expect(result).toEqual("attribute123"); // Should return the attribute value - }); }); describe("getFirstNonCommentChild", () => {