|
| 1 | +const fs = require("fs").promises; |
| 2 | +const { getParent, getName, graphTD, Mermaid } = require("./mermaid"); |
| 3 | + |
| 4 | +function createChildSpan(name, pid, parent) { |
| 5 | + return { |
| 6 | + name, |
| 7 | + parent, |
| 8 | + context: { |
| 9 | + pid |
| 10 | + } |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +function createSampleSpans() { |
| 15 | + const aa = createChildSpan("aa", 1); |
| 16 | + aa.root = true; |
| 17 | + const bb = createChildSpan("bb", 2, aa); |
| 18 | + const cc = createChildSpan("cc", 3, aa); |
| 19 | + const dd = createChildSpan("dd", 4, bb); |
| 20 | + |
| 21 | + const spans = [aa, bb, cc, dd]; |
| 22 | + |
| 23 | + return spans; |
| 24 | +} |
| 25 | + |
| 26 | +const graphResult = `graph TD; |
| 27 | + root-->aa1 |
| 28 | + aa1-->bb2 |
| 29 | + aa1-->cc3 |
| 30 | + bb2-->dd4`; |
| 31 | + |
| 32 | +describe("getParent", () => { |
| 33 | + it("should return `root` if root flag is true", () => { |
| 34 | + expect(getParent({ root: true })).toEqual("root"); |
| 35 | + }); |
| 36 | + it("should return parent if root flag not true", () => { |
| 37 | + expect(getParent({ parent: "parent" })).toEqual("parent"); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("getName", () => { |
| 42 | + it("should return 'root' if span is root", () => { |
| 43 | + expect(getName("root")).toEqual("root"); |
| 44 | + }); |
| 45 | + it("should return constructed name if span is not root", () => { |
| 46 | + const span = createChildSpan("name", 1); |
| 47 | + expect(getName(span)).toEqual("name1"); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("graphTD", () => { |
| 52 | + it("should create graph tree", () => { |
| 53 | + const result = graphTD(createSampleSpans()); |
| 54 | + expect(result).toEqual(graphResult); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("Mermaid", () => { |
| 59 | + describe("constructor", () => { |
| 60 | + it("should create spans array", () => { |
| 61 | + const result = new Mermaid(); |
| 62 | + expect(result).toHaveProperty("spans", []); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("create", () => { |
| 67 | + it("should have static method to create new instance", () => { |
| 68 | + const result = Mermaid.create(); |
| 69 | + expect(result).toBeInstanceOf(Mermaid); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("start", () => { |
| 74 | + it("should track spans", () => { |
| 75 | + const result = new Mermaid(); |
| 76 | + result.start("span"); |
| 77 | + expect(result.spans).toEqual(["span"]); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("report", () => { |
| 82 | + it("should create and return mermaid graph", async () => { |
| 83 | + const result = new Mermaid(); |
| 84 | + result.spans = createSampleSpans(); |
| 85 | + expect(await result.report()).toEqual(graphResult); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should save to file", async () => { |
| 89 | + const result = new Mermaid(); |
| 90 | + result.spans = createSampleSpans(); |
| 91 | + |
| 92 | + const spyWriteFile = jest.spyOn(fs, "writeFile").mockResolvedValue(true); |
| 93 | + |
| 94 | + await result.report("/test.mer"); |
| 95 | + expect(spyWriteFile).toBeCalledWith("/test.mer", graphResult); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments