util.test.ts 4.9 KB
Newer Older
J
Joe Previte 已提交
1
import { JSDOM } from "jsdom"
2 3
import * as util from "../../../src/common/util"
import { createLoggerMock } from "../../utils/helpers"
J
Joe Previte 已提交
4

J
Joe Previte 已提交
5 6 7
const dom = new JSDOM()
global.document = dom.window.document

8
export type LocationLike = Pick<Location, "pathname" | "origin">
A
Asher 已提交
9 10 11 12

describe("util", () => {
  describe("normalize", () => {
    it("should remove multiple slashes", () => {
A
Asher 已提交
13
      expect(util.normalize("//foo//bar//baz///mumble")).toBe("/foo/bar/baz/mumble")
A
Asher 已提交
14 15 16
    })

    it("should remove trailing slashes", () => {
A
Asher 已提交
17
      expect(util.normalize("qux///")).toBe("qux")
A
Asher 已提交
18
    })
A
Asher 已提交
19 20

    it("should preserve trailing slash if it exists", () => {
A
Asher 已提交
21 22
      expect(util.normalize("qux///", true)).toBe("qux/")
      expect(util.normalize("qux", true)).toBe("qux")
A
Asher 已提交
23
    })
A
Asher 已提交
24
  })
J
Joe Previte 已提交
25 26 27

  describe("split", () => {
    it("should split at a comma", () => {
A
Asher 已提交
28
      expect(util.split("Hello,world", ",")).toStrictEqual(["Hello", "world"])
J
Joe Previte 已提交
29 30 31
    })

    it("shouldn't split if the delimiter doesn't exist", () => {
A
Asher 已提交
32
      expect(util.split("Hello world", ",")).toStrictEqual(["Hello world", ""])
J
Joe Previte 已提交
33 34 35 36 37
    })
  })

  describe("plural", () => {
    it("should add an s if count is greater than 1", () => {
A
Asher 已提交
38
      expect(util.plural(2, "dog")).toBe("dogs")
J
Joe Previte 已提交
39 40
    })
    it("should NOT add an s if the count is 1", () => {
A
Asher 已提交
41
      expect(util.plural(1, "dog")).toBe("dog")
J
Joe Previte 已提交
42 43 44
    })
  })

J
Joe Previte 已提交
45 46
  describe("generateUuid", () => {
    it("should generate a unique uuid", () => {
A
Asher 已提交
47 48
      const uuid = util.generateUuid()
      const uuid2 = util.generateUuid()
J
Joe Previte 已提交
49 50 51 52 53
      expect(uuid).toHaveLength(24)
      expect(typeof uuid).toBe("string")
      expect(uuid).not.toBe(uuid2)
    })
    it("should generate a uuid of a specific length", () => {
A
Asher 已提交
54
      const uuid = util.generateUuid(10)
J
Joe Previte 已提交
55 56 57 58
      expect(uuid).toHaveLength(10)
    })
  })

J
Joe Previte 已提交
59 60
  describe("trimSlashes", () => {
    it("should remove leading slashes", () => {
A
Asher 已提交
61
      expect(util.trimSlashes("/hello-world")).toBe("hello-world")
J
Joe Previte 已提交
62 63 64
    })

    it("should remove trailing slashes", () => {
A
Asher 已提交
65
      expect(util.trimSlashes("hello-world/")).toBe("hello-world")
J
Joe Previte 已提交
66 67 68
    })

    it("should remove both leading and trailing slashes", () => {
A
Asher 已提交
69
      expect(util.trimSlashes("/hello-world/")).toBe("hello-world")
J
Joe Previte 已提交
70 71 72
    })

    it("should remove multiple leading and trailing slashes", () => {
A
Asher 已提交
73
      expect(util.trimSlashes("///hello-world////")).toBe("hello-world")
J
Joe Previte 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    })
  })

  describe("resolveBase", () => {
    beforeEach(() => {
      const location: LocationLike = {
        pathname: "/healthz",
        origin: "http://localhost:8080",
      }

      // Because resolveBase is not a pure function
      // and relies on the global location to be set
      // we set it before all the tests
      // and tell TS that our location should be looked at
      // as Location (even though it's missing some properties)
      global.location = location as Location
    })

    it("should resolve a base", () => {
A
Asher 已提交
93
      expect(util.resolveBase("localhost:8080")).toBe("/localhost:8080")
J
Joe Previte 已提交
94 95 96
    })

    it("should resolve a base with a forward slash at the beginning", () => {
A
Asher 已提交
97
      expect(util.resolveBase("/localhost:8080")).toBe("/localhost:8080")
J
Joe Previte 已提交
98 99 100
    })

    it("should resolve a base with query params", () => {
A
Asher 已提交
101
      expect(util.resolveBase("localhost:8080?folder=hello-world")).toBe("/localhost:8080")
J
Joe Previte 已提交
102 103 104
    })

    it("should resolve a base with a path", () => {
A
Asher 已提交
105
      expect(util.resolveBase("localhost:8080/hello/world")).toBe("/localhost:8080/hello/world")
J
Joe Previte 已提交
106 107 108
    })

    it("should resolve a base to an empty string when not provided", () => {
A
Asher 已提交
109
      expect(util.resolveBase()).toBe("")
J
Joe Previte 已提交
110 111 112 113 114
    })
  })

  describe("arrayify", () => {
    it("should return value it's already an array", () => {
A
Asher 已提交
115
      expect(util.arrayify(["hello", "world"])).toStrictEqual(["hello", "world"])
J
Joe Previte 已提交
116
    })
J
Joe Previte 已提交
117

J
Joe Previte 已提交
118 119
    it("should wrap the value in an array if not an array", () => {
      expect(
A
Asher 已提交
120
        util.arrayify({
J
Joe Previte 已提交
121 122 123 124 125
          name: "Coder",
          version: "3.8",
        }),
      ).toStrictEqual([{ name: "Coder", version: "3.8" }])
    })
J
Joe Previte 已提交
126

J
Joe Previte 已提交
127
    it("should return an empty array if the value is undefined", () => {
A
Asher 已提交
128
      expect(util.arrayify(undefined)).toStrictEqual([])
J
Joe Previte 已提交
129 130 131
    })
  })

J
Joe Previte 已提交
132 133 134 135 136 137 138 139 140
  describe("logError", () => {
    afterEach(() => {
      jest.clearAllMocks()
    })

    afterAll(() => {
      jest.restoreAllMocks()
    })

A
Asher 已提交
141 142
    const loggerModule = createLoggerMock()

J
Joe Previte 已提交
143 144 145 146
    it("should log an error with the message and stack trace", () => {
      const message = "You don't have access to that folder."
      const error = new Error(message)

A
Asher 已提交
147
      util.logError(loggerModule.logger, "ui", error)
J
Joe Previte 已提交
148

J
Joe Previte 已提交
149 150
      expect(loggerModule.logger.error).toHaveBeenCalled()
      expect(loggerModule.logger.error).toHaveBeenCalledWith(`ui: ${error.message} ${error.stack}`)
J
Joe Previte 已提交
151 152 153
    })

    it("should log an error, even if not an instance of error", () => {
A
Asher 已提交
154
      util.logError(loggerModule.logger, "api", "oh no")
J
Joe Previte 已提交
155

J
Joe Previte 已提交
156 157
      expect(loggerModule.logger.error).toHaveBeenCalled()
      expect(loggerModule.logger.error).toHaveBeenCalledWith("api: oh no")
J
Joe Previte 已提交
158 159
    })
  })
A
Asher 已提交
160
})