pages-dev-util.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { isRoutingRuleMatch } from "../pages-dev-util";
  2. describe("isRoutingRuleMatch", () => {
  3. it("should match rules referencing root level correctly", () => {
  4. const routingRule = "/";
  5. expect(isRoutingRuleMatch("/", routingRule)).toBeTruthy();
  6. expect(isRoutingRuleMatch("/foo", routingRule)).toBeFalsy();
  7. expect(isRoutingRuleMatch("/foo/", routingRule)).toBeFalsy();
  8. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeFalsy();
  9. });
  10. it("should match include-all rules correctly", () => {
  11. const routingRule = "/*";
  12. expect(isRoutingRuleMatch("/", routingRule)).toBeTruthy();
  13. expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
  14. expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
  15. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
  16. expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
  17. expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
  18. expect(isRoutingRuleMatch("/foo/bar/baz/", routingRule)).toBeTruthy();
  19. });
  20. it("should match `/*` suffix-ed rules correctly", () => {
  21. let routingRule = "/foo/*";
  22. expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
  23. expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
  24. expect(isRoutingRuleMatch("/foobar", routingRule)).toBeFalsy();
  25. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
  26. expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
  27. expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
  28. expect(isRoutingRuleMatch("/bar/foo/baz", routingRule)).toBeFalsy();
  29. routingRule = "/foo/bar/*";
  30. expect(isRoutingRuleMatch("/foo", routingRule)).toBeFalsy();
  31. expect(isRoutingRuleMatch("/foo/", routingRule)).toBeFalsy();
  32. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
  33. expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
  34. expect(isRoutingRuleMatch("/foo/barfoo", routingRule)).toBeFalsy();
  35. expect(isRoutingRuleMatch("baz/foo/bar", routingRule)).toBeFalsy();
  36. expect(isRoutingRuleMatch("baz/foo/bar/", routingRule)).toBeFalsy();
  37. });
  38. it("should match `/` suffix-ed rules correctly", () => {
  39. let routingRule = "/foo/";
  40. expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
  41. expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
  42. routingRule = "/foo/bar/";
  43. expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
  44. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
  45. });
  46. it("should match `*` suffix-ed rules correctly", () => {
  47. let routingRule = "/foo*";
  48. expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
  49. expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
  50. expect(isRoutingRuleMatch("/foobar", routingRule)).toBeTruthy();
  51. expect(isRoutingRuleMatch("/barfoo", routingRule)).toBeFalsy();
  52. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
  53. expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
  54. expect(isRoutingRuleMatch("/bar/foobar", routingRule)).toBeFalsy();
  55. expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
  56. expect(isRoutingRuleMatch("/bar/foo/baz", routingRule)).toBeFalsy();
  57. routingRule = "/foo/bar*";
  58. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
  59. expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
  60. expect(isRoutingRuleMatch("/foo/barfoo", routingRule)).toBeTruthy();
  61. expect(isRoutingRuleMatch("/bar/foo/barfoo", routingRule)).toBeFalsy();
  62. expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
  63. expect(isRoutingRuleMatch("/bar/foo/bar/baz", routingRule)).toBeFalsy();
  64. });
  65. it("should match rules without wildcards correctly", () => {
  66. let routingRule = "/foo";
  67. expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
  68. expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
  69. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeFalsy();
  70. expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
  71. routingRule = "/foo/bar";
  72. expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
  73. expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
  74. expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeFalsy();
  75. expect(isRoutingRuleMatch("/baz/foo/bar", routingRule)).toBeFalsy();
  76. });
  77. it("should throw an error if pathname or routing rule params are missing", () => {
  78. // MISSING PATHNAME
  79. expect(() =>
  80. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  81. // @ts-ignore: sanity check
  82. isRoutingRuleMatch(undefined, "/*")
  83. ).toThrow("Pathname is undefined.");
  84. expect(() =>
  85. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  86. // @ts-ignore: sanity check
  87. isRoutingRuleMatch(null, "/*")
  88. ).toThrow("Pathname is undefined.");
  89. expect(() => isRoutingRuleMatch("", "/*")).toThrow(
  90. "Pathname is undefined."
  91. );
  92. // MISSING ROUTING RULE
  93. expect(() =>
  94. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  95. // @ts-ignore: sanity check
  96. isRoutingRuleMatch("/foo", undefined)
  97. ).toThrow("Routing rule is undefined.");
  98. expect(() =>
  99. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  100. // @ts-ignore: sanity check
  101. isRoutingRuleMatch("/foo", null)
  102. ).toThrow("Routing rule is undefined.");
  103. expect(() => isRoutingRuleMatch("/foo", "")).toThrow(
  104. "Routing rule is undefined."
  105. );
  106. });
  107. });