Skip to content

Commit 0a57acf

Browse files
authored
fix: e2e test to get the list of urls to check from sitemap to avoid infinite looping through tests (#362)
1 parent f4e853c commit 0a57acf

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

cypress/e2e/links-and-images.cy.ts

+31-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
describe("links-and-images", () => {
22
it("should return 200 for all links and images", () => {
3-
const testedPages = new Set<string>();
4-
const pagesToTest = ["/"];
3+
const pagesToTest: string[] = [];
4+
5+
// First get all URLs from sitemap
6+
cy.request("https://www.davidhu.io/sitemap-0.xml").then((response) => {
7+
const parser = new DOMParser();
8+
const xmlDoc = parser.parseFromString(response.body, "text/xml");
9+
const urls = xmlDoc.getElementsByTagName("url");
10+
11+
for (let i = 0; i < urls.length; i++) {
12+
const loc = urls[i].getElementsByTagName("loc")[0];
13+
if (loc) {
14+
const url = loc.textContent;
15+
if (url) {
16+
pagesToTest.push(url);
17+
}
18+
}
19+
}
20+
21+
visitUrlsOnPage();
22+
});
523

624
function visitUrlsOnPage() {
725
const url = pagesToTest.pop();
@@ -10,12 +28,6 @@ describe("links-and-images", () => {
1028
return;
1129
}
1230

13-
if (testedPages.has(url)) {
14-
visitUrlsOnPage();
15-
return;
16-
}
17-
18-
testedPages.add(url);
1931
cy.visit(url);
2032
cy.log(">>>> Checking images for " + url);
2133

@@ -35,42 +47,33 @@ describe("links-and-images", () => {
3547
cy.get("a")
3648
.should(Cypress._.noop)
3749
.each((link) => {
38-
const url = (link.prop("href") as string) || "";
50+
const urlString = (link.prop("href") as string) || "";
3951

40-
// these two links do not allow bot visits
41-
if (url.includes("angel.co") || url.includes("linkedin.com")) {
52+
const url = new URL(urlString);
53+
54+
// do not allow bot visits
55+
if (url.host === "www.linkedin.com") {
4256
return;
4357
}
4458

45-
if (url.includes("mailto:")) {
59+
if (url.protocol === "mailto:") {
4660
return;
4761
}
4862

4963
// don't download any files
5064
if (
51-
url.endsWith(".msi") ||
52-
url.endsWith(".dmg") ||
53-
url.endsWith(".AppImage") ||
54-
url.endsWith(".deb")
65+
url.href.endsWith(".msi") ||
66+
url.href.endsWith(".dmg") ||
67+
url.href.endsWith(".AppImage") ||
68+
url.href.endsWith(".deb")
5569
) {
5670
return;
5771
}
5872

59-
cy.request(url);
60-
61-
if (url.includes(".xml")) {
62-
return;
63-
}
64-
65-
cy.log('>>>> Adding "' + url + '" to pages to test');
66-
if (url.includes("davidhu.io/") && !testedPages.has(url)) {
67-
pagesToTest.push(url);
68-
}
73+
cy.request(url.href);
6974
});
7075
})
7176
.then(visitUrlsOnPage);
7277
}
73-
74-
visitUrlsOnPage();
7578
});
7679
});

src/data/socialLinks.ts

-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@ export const socialLinks = [
1414
url: "https://twitter.com/notdavidhu",
1515
icon: "mdi:twitter",
1616
},
17-
{
18-
label: "AngelList",
19-
url: "https://angel.co/davidhu2000",
20-
icon: "fa-brands:angellist",
21-
},
2217
];

0 commit comments

Comments
 (0)