From c18719e573a9382e9bc2919a70ce06b5d9c008e9 Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Thu, 25 Jun 2020 13:24:48 +0200 Subject: [PATCH] fix unit test --- .../src/remarkCommitLinksParser.test.ts | 19 +++++++++++++------ .../src/remarkCommitLinksParser.ts | 8 +++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/scm-ui/ui-components/src/remarkCommitLinksParser.test.ts b/scm-ui/ui-components/src/remarkCommitLinksParser.test.ts index 11ca3b7444..9d54e3539a 100644 --- a/scm-ui/ui-components/src/remarkCommitLinksParser.test.ts +++ b/scm-ui/ui-components/src/remarkCommitLinksParser.test.ts @@ -26,16 +26,20 @@ import { regExpPattern } from "./remarkCommitLinksParser"; describe("Remark Commit Links RegEx Tests", () => { it("should match simple names", () => { - expect("namespace/name@1a5s4w8a".match(regExpPattern)).toBeTruthy(); + const regExp = new RegExp(regExpPattern, "g"); + expect("namespace/name@1a5s4w8a".match(regExp)).toBeTruthy(); }); it("should match complex names", () => { - expect("hitchhiker/heart-of-gold@c7237cb60689046990dc9dc2a388a517adb3e2b2".match(regExpPattern)).toBeTruthy(); + const regExp = new RegExp(regExpPattern, "g"); + expect("hitchhiker/heart-of-gold@c7237cb60689046990dc9dc2a388a517adb3e2b2".match(regExp)).toBeTruthy(); }); it("should replace match", () => { - expect("Prefix namespace/name@42 suffix".replace(regExpPattern, "replaced")).toBe("Prefix replaced suffix"); + const regExp = new RegExp(regExpPattern, "g"); + expect("Prefix namespace/name@42 suffix".replace(regExp, "replaced")).toBe("Prefix replaced suffix"); }); it("should match groups", () => { - const match = regExpPattern.exec("namespace/name@42"); + const regExp = new RegExp(regExpPattern, "g"); + const match = regExp.exec("namespace/name@42"); expect(match).toBeTruthy(); if (match) { expect(match[1]).toBe("namespace"); @@ -44,15 +48,18 @@ describe("Remark Commit Links RegEx Tests", () => { } }); it("should match multiple links in text", () => { + const regExp = new RegExp(regExpPattern, "g"); const text = "Prefix hitchhiker/heart-of-gold@42 some text hitchhiker/heart-of-gold@21 suffix"; const matches = []; - let match = regExpPattern.exec(text); + let match = regExp.exec(text); while (match !== null) { matches.push(match[0]); - match = regExpPattern.exec(text); + match = regExp.exec(text); } + console.log(matches) + expect(matches[0]).toBe("hitchhiker/heart-of-gold@42"); expect(matches[1]).toBe("hitchhiker/heart-of-gold@21"); }); diff --git a/scm-ui/ui-components/src/remarkCommitLinksParser.ts b/scm-ui/ui-components/src/remarkCommitLinksParser.ts index 50b8cc8d3b..fa26c793db 100644 --- a/scm-ui/ui-components/src/remarkCommitLinksParser.ts +++ b/scm-ui/ui-components/src/remarkCommitLinksParser.ts @@ -31,19 +31,21 @@ import { TFunction } from "i18next"; const namePartRegex = nameRegex.source.substring(1, nameRegex.source.length - 1); // Visible for testing -export const regExpPattern = new RegExp(`(${namePartRegex})\\/(${namePartRegex})@([\\w\\d]+)`, "g"); +export const regExpPattern = `(${namePartRegex})\\/(${namePartRegex})@([\\w\\d]+)`; function match(value: string): RegExpMatchArray[] { + const regExp = new RegExp(regExpPattern, "g"); const matches = []; - let m = regExpPattern.exec(value); + let m = regExp.exec(value); while (m) { matches.push(m); - m = regExpPattern.exec(value); + m = regExp.exec(value); } return matches; } export const createTransformer = (t: TFunction): MdastPlugin => { + return (tree: MarkdownAbstractSyntaxTree) => { visit(tree, "text", (node: MarkdownAbstractSyntaxTree, index: number, parent: MarkdownAbstractSyntaxTree) => { if (parent.type === "link" || !node.value) {