diff --git a/src/templates/assets/javascripts/integrations/version/correspondingPage.test.ts b/src/templates/assets/javascripts/integrations/version/correspondingPage.test.ts index 432783ed547..7feee9de3e5 100644 --- a/src/templates/assets/javascripts/integrations/version/correspondingPage.test.ts +++ b/src/templates/assets/javascripts/integrations/version/correspondingPage.test.ts @@ -4,32 +4,116 @@ import { Sitemap } from "../sitemap" import { selectedVersionCorrespondingURL } from "./correspondingPage" -const sitemapURLsV1 = [ - "https://test.github.io/project/0.1/", - "https://test.github.io/project/0.1/bar/", - "https://test.github.io/project/0.1/foo/" -] - describe("Version switcher tests", () => { - it("selectedVersionCorrespondingURL test", () => { - assert.equal( - selectedVersionCorrespondingURL({ - selectedVersionSitemap: sitemapFromURLList(sitemapURLsV1), - selectedVersionBaseURL: new URL("https://test.github.io/project/0.1/"), - currentLocation: new URL("https://test.github.io/project/latest/bar/#heading?param=some"), - currentBaseURL: "https://test.github.io/project/latest/" - })?.href, - "https://test.github.io/project/0.1/bar/#heading?param=some", - ) - assert.equal( - selectedVersionCorrespondingURL({ - selectedVersionSitemap: sitemapFromURLList(sitemapURLsV1), - selectedVersionBaseURL: new URL("https://test.github.io/project/0.1/"), - currentLocation: new URL("https://test.github.io/project/latest/notinv1/"), - currentBaseURL: "https://test.github.io/project/latest/" - }), - undefined, - ) + // These examples are obtained by pausing the JS debugger in various situation and + // observing the local variables. + describe("Normal deployed to GitHub situation", () => { + const sitemapURLsV1 = [ + "https://test.github.io/project/0.1/", + "https://test.github.io/project/0.1/bar/", + "https://test.github.io/project/0.1/foo/" + ] + + it("returns a URL when the selected version has the current page", () => { + assert.equal( + selectedVersionCorrespondingURL({ + selectedVersionSitemap: sitemapFromURLList(sitemapURLsV1), + selectedVersionBaseURL: new URL( + "https://test.github.io/project/0.1/", + ), + currentLocation: new URL("https://test.github.io/project/0.2/bar/#heading?param=some"), + currentBaseURL: "https://test.github.io/project/0.2/" + })?.href, + "https://test.github.io/project/0.1/bar/#heading?param=some", + ) + }) + it("returns nothing when the selected version does not have the current page", () => { + assert.equal( + selectedVersionCorrespondingURL({ + selectedVersionSitemap: sitemapFromURLList(sitemapURLsV1), + selectedVersionBaseURL: new URL( + "https://test.github.io/project/0.1/", + ), + currentLocation: new URL( + "https://test.github.io/project/0.2/notinv1/#heading?param=some", + ), + currentBaseURL: "https://test.github.io/project/0.2/" + }), + undefined, + ) + }) + }) + + describe("Deployed to GitHub with canonical_version='latest'", () => { + // https://github.com/squidfunk/mkdocs-material/issues/7226 If the target + // version has `canonical_version='latest'`, then the sitemap looks as though + // it was for the "latest" version + const sitemapURLsLatestVersion = [ + "https://test.github.io/project/latest/", + "https://test.github.io/project/latest/bar/", + "https://test.github.io/project/latest/foo/" + ] + + it("does not (yet, TODO #7226) return a URL when the selected version has the current page", () => { + assert.equal( + selectedVersionCorrespondingURL({ + selectedVersionSitemap: sitemapFromURLList(sitemapURLsLatestVersion), + selectedVersionBaseURL: new URL( + "https://test.github.io/project/0.1/", + ), + currentLocation: new URL("https://test.github.io/project/0.2/bar/#heading?param=some"), + currentBaseURL: "https://test.github.io/project/0.2/" + })?.href, + undefined, + ) + }) + it("returns nothing when the selected version does not have the current page", () => { + assert.equal( + selectedVersionCorrespondingURL({ + selectedVersionSitemap: sitemapFromURLList(sitemapURLsLatestVersion), + selectedVersionBaseURL: new URL( + "https://test.github.io/project/0.1/", + ), + currentLocation: new URL( + "https://test.github.io/project/0.2/notinv1/#heading?param=some", + ), + currentBaseURL: "https://test.github.io/project/0.2/" + }), + undefined, + ) + }) + }) + + describe("Served from localhost with `mike serve`", () => { + // TODO: test how fetchSitemap converts the actual sitemap into this + const sitemapURLsLocalhost = [ + "https://localhost/project/0.1/", + "https://localhost/project/0.1/bar/", + "https://localhost/project/0.1/foo/" + ] + + it("does not (yet, TODO) return a URL when the selected version has the current page", () => { + assert.equal( + selectedVersionCorrespondingURL({ + selectedVersionSitemap: sitemapFromURLList(sitemapURLsLocalhost), + selectedVersionBaseURL: new URL("https://localhost:8000/0.1/"), + currentLocation: new URL("https://localhost:8000/0.2/bar/#heading?param=some"), + currentBaseURL: "https://localhost:8000/0.2/" + })?.href, + undefined, + ) + }) + it("returns nothing when the selected version does not have the current page", () => { + assert.equal( + selectedVersionCorrespondingURL({ + selectedVersionSitemap: sitemapFromURLList(sitemapURLsLocalhost), + selectedVersionBaseURL: new URL("https://localhost:8000/0.1/"), + currentLocation: new URL("https://localhost:8000/0.2/notinv1/#heading?param=some"), + currentBaseURL: "https://localhost:8000/0.2/" + }), + undefined, + ) + }) }) })