one
Version:
One is a new React Framework that makes Vite serve both native and web.
199 lines (198 loc) • 7.4 kB
JavaScript
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { generateSitemap } from "./generateSitemap.native.js";
describe("generateSitemap", function () {
var originalEnv;
beforeEach(function () {
originalEnv = process.env.ONE_SERVER_URL, delete process.env.ONE_SERVER_URL;
}), afterEach(function () {
originalEnv !== void 0 ? process.env.ONE_SERVER_URL = originalEnv : delete process.env.ONE_SERVER_URL;
}), it("generates basic sitemap XML", function () {
var routes = [{
path: "/"
}, {
path: "/about"
}, {
path: "/blog"
}],
options = {},
result = generateSitemap(routes, options);
expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>'), expect(result).toContain('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'), expect(result).toContain("<loc>/</loc>"), expect(result).toContain("<loc>/about</loc>"), expect(result).toContain("<loc>/blog</loc>"), expect(result).toContain("</urlset>");
}), it("uses baseUrl when provided", function () {
var routes = [{
path: "/"
}, {
path: "/about"
}],
options = {
baseUrl: "https://example.com"
},
result = generateSitemap(routes, options);
expect(result).toContain("<loc>https://example.com/</loc>"), expect(result).toContain("<loc>https://example.com/about</loc>");
}), it("strips trailing slash from baseUrl", function () {
var routes = [{
path: "/about"
}],
options = {
baseUrl: "https://example.com/"
},
result = generateSitemap(routes, options);
expect(result).toContain("<loc>https://example.com/about</loc>"), expect(result).not.toContain("https://example.com//about");
}), it("uses ONE_SERVER_URL env var when baseUrl not provided", function () {
process.env.ONE_SERVER_URL = "https://env-url.com";
var routes = [{
path: "/test"
}],
options = {},
result = generateSitemap(routes, options);
expect(result).toContain("<loc>https://env-url.com/test</loc>");
}), it("applies default priority to all routes", function () {
var routes = [{
path: "/"
}, {
path: "/about"
}],
options = {
priority: 0.8
},
result = generateSitemap(routes, options);
expect(result).toMatch(RegExp("<url>\\s*<loc>\\/about<\\/loc>\\s*<priority>0\\.8<\\/priority>\\s*<\\/url>", "s"));
}), it("applies default changefreq to all routes", function () {
var routes = [{
path: "/"
}, {
path: "/about"
}],
options = {
changefreq: "weekly"
},
result = generateSitemap(routes, options);
expect(result).toContain("<changefreq>weekly</changefreq>");
}), it("respects route-level sitemap exports", function () {
var routes = [{
path: "/",
routeExport: {
priority: 1,
changefreq: "daily"
}
}, {
path: "/about",
routeExport: {
priority: 0.5,
changefreq: "monthly"
}
}],
options = {
priority: 0.7,
changefreq: "weekly"
},
result = generateSitemap(routes, options);
expect(result).toMatch(RegExp("<url>\\s*<loc>\\/<\\/loc>\\s*<changefreq>daily<\\/changefreq>\\s*<priority>1\\.0<\\/priority>\\s*<\\/url>", "s")), expect(result).toMatch(RegExp("<url>\\s*<loc>\\/about<\\/loc>\\s*<changefreq>monthly<\\/changefreq>\\s*<priority>0\\.5<\\/priority>\\s*<\\/url>", "s"));
}), it("excludes routes with routeExport.exclude = true", function () {
var routes = [{
path: "/"
}, {
path: "/admin",
routeExport: {
exclude: !0
}
}, {
path: "/about"
}],
options = {},
result = generateSitemap(routes, options);
expect(result).toContain("<loc>/</loc>"), expect(result).toContain("<loc>/about</loc>"), expect(result).not.toContain("/admin");
}), it("excludes routes matching exclude glob patterns", function () {
var routes = [{
path: "/"
}, {
path: "/admin/dashboard"
}, {
path: "/admin/users"
}, {
path: "/about"
}, {
path: "/api/health"
}],
options = {
exclude: ["/admin/*", "/api/*"]
},
result = generateSitemap(routes, options);
expect(result).toContain("<loc>/</loc>"), expect(result).toContain("<loc>/about</loc>"), expect(result).not.toContain("/admin"), expect(result).not.toContain("/api");
}), it("includes lastmod when provided in route export", function () {
var routes = [{
path: "/",
routeExport: {
lastmod: "2024-01-15"
}
}, {
path: "/about",
routeExport: {
lastmod: /* @__PURE__ */new Date("2024-06-20")
}
}],
options = {},
result = generateSitemap(routes, options);
expect(result).toContain("<lastmod>2024-01-15</lastmod>"), expect(result).toContain("<lastmod>2024-06-20</lastmod>");
}), it("escapes XML special characters in URLs", function () {
var routes = [{
path: "/search?q=foo&bar=baz"
}],
options = {
baseUrl: "https://example.com"
},
result = generateSitemap(routes, options);
expect(result).toContain("&"), expect(result).not.toContain("&bar");
}), it("generates empty sitemap when no routes", function () {
var routes = [],
options = {},
result = generateSitemap(routes, options);
expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>'), expect(result).toContain('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'), expect(result).toContain("</urlset>"), expect(result).not.toContain("<url>");
}), it("handles all valid changefreq values", function () {
var changefreqs = ["always", "hourly", "daily", "weekly", "monthly", "yearly", "never"],
_iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = changefreqs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var changefreq = _step.value,
routes = [{
path: "/",
routeExport: {
changefreq
}
}],
result = generateSitemap(routes, {});
expect(result).toContain(`<changefreq>${changefreq}</changefreq>`);
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
}), it("formats priority with one decimal place", function () {
var routes = [{
path: "/",
routeExport: {
priority: 1
}
}, {
path: "/about",
routeExport: {
priority: 0.5
}
}, {
path: "/blog",
routeExport: {
priority: 0.75
}
}],
options = {},
result = generateSitemap(routes, options);
expect(result).toContain("<priority>1.0</priority>"), expect(result).toContain("<priority>0.5</priority>"), expect(result).toContain("<priority>0.8</priority>");
});
});
//# sourceMappingURL=generateSitemap.test.native.js.map