|
1 | 1 | import { expect } from "@esm-bundle/chai";
|
2 | 2 | import "./header.js";
|
| 3 | +import pages from "../../stories/mocks/graph.json" with { type: "json" }; |
| 4 | + |
| 5 | +const CURRENT_ROUTE = "/guides/"; |
| 6 | +const ICONS = [ |
| 7 | + { |
| 8 | + link: "https://github.com/ProjectEvergreen/greenwood", |
| 9 | + title: "GitHub", |
| 10 | + }, |
| 11 | + { |
| 12 | + link: "https://discord.gg/bsy9jvWh", |
| 13 | + title: "Discord", |
| 14 | + }, |
| 15 | + { |
| 16 | + link: "https://twitter.com/PrjEvergreen", |
| 17 | + title: "Twitter", |
| 18 | + }, |
| 19 | +]; |
| 20 | + |
| 21 | +window.fetch = function () { |
| 22 | + return new Promise((resolve) => { |
| 23 | + resolve(new Response(JSON.stringify(pages.filter((page) => page.data.collection === "nav")))); |
| 24 | + }); |
| 25 | +}; |
3 | 26 |
|
4 | 27 | describe("Components/Header", () => {
|
5 |
| - const NAV = [ |
6 |
| - { |
7 |
| - title: "Documentation", |
8 |
| - label: "Docs", |
9 |
| - }, |
10 |
| - { |
11 |
| - title: "Guides", |
12 |
| - label: "Guides", |
13 |
| - }, |
14 |
| - { |
15 |
| - title: "Blog", |
16 |
| - label: "Blog", |
17 |
| - }, |
18 |
| - ]; |
19 |
| - const ICONS = [ |
20 |
| - { |
21 |
| - link: "https://github.com/ProjectEvergreen/greenwood", |
22 |
| - title: "GitHub", |
23 |
| - }, |
24 |
| - { |
25 |
| - link: "https://discord.gg/bsy9jvWh", |
26 |
| - title: "Discord", |
27 |
| - }, |
28 |
| - { |
29 |
| - link: "https://twitter.com/PrjEvergreen", |
30 |
| - title: "Twitter", |
31 |
| - }, |
32 |
| - ]; |
33 |
| - |
34 | 28 | let header;
|
35 | 29 |
|
36 | 30 | before(async () => {
|
37 | 31 | header = document.createElement("app-header");
|
| 32 | + header.setAttribute("current-route", CURRENT_ROUTE); |
| 33 | + |
38 | 34 | document.body.appendChild(header);
|
39 | 35 |
|
40 | 36 | await header.updateComplete;
|
@@ -62,16 +58,23 @@ describe("Components/Header", () => {
|
62 | 58 |
|
63 | 59 | it("should have the expected desktop navigation links", () => {
|
64 | 60 | const links = header.querySelectorAll("nav[aria-label='Main'] ul li a");
|
| 61 | + let activeRoute = undefined; |
65 | 62 |
|
66 |
| - Array.from(links).forEach((link) => { |
67 |
| - const navItem = NAV.find( |
68 |
| - (nav) => `/${nav.label.toLowerCase()}/` === link.getAttribute("href"), |
69 |
| - ); |
| 63 | + Array.from(links).forEach((link, idx) => { |
| 64 | + const navItem = pages.find((nav) => nav.route === link.getAttribute("href")); |
70 | 65 |
|
71 | 66 | expect(navItem).to.not.equal(undefined);
|
| 67 | + expect(navItem.data.order).to.equal((idx += 1)); |
72 | 68 | expect(link.textContent).to.equal(navItem.label);
|
73 | 69 | expect(link.getAttribute("title")).to.equal(navItem.title);
|
| 70 | + |
| 71 | + // current route should display as active |
| 72 | + if (navItem.route === CURRENT_ROUTE && link.getAttribute("class").includes("active")) { |
| 73 | + activeRoute = navItem; |
| 74 | + } |
74 | 75 | });
|
| 76 | + |
| 77 | + expect(activeRoute.route).to.equal(CURRENT_ROUTE); |
75 | 78 | });
|
76 | 79 |
|
77 | 80 | it("should have the expected social link icons", () => {
|
@@ -121,16 +124,23 @@ describe("Components/Header", () => {
|
121 | 124 |
|
122 | 125 | it("should have the expected navigation links", () => {
|
123 | 126 | const links = header.querySelectorAll("nav[aria-label='Mobile'] ul li a");
|
| 127 | + let activeRoute = undefined; |
124 | 128 |
|
125 |
| - Array.from(links).forEach((link) => { |
126 |
| - const navItem = NAV.find( |
127 |
| - (nav) => `/${nav.label.toLowerCase()}/` === link.getAttribute("href"), |
128 |
| - ); |
| 129 | + Array.from(links).forEach((link, idx) => { |
| 130 | + const navItem = pages.find((nav) => nav.route === link.getAttribute("href")); |
129 | 131 |
|
130 | 132 | expect(navItem).to.not.equal(undefined);
|
| 133 | + expect(navItem.data.order).to.equal((idx += 1)); |
131 | 134 | expect(link.textContent).to.equal(navItem.label);
|
132 | 135 | expect(link.getAttribute("title")).to.equal(navItem.title);
|
| 136 | + |
| 137 | + // current route should display as active |
| 138 | + if (navItem.route === CURRENT_ROUTE && link.getAttribute("class").includes("active")) { |
| 139 | + activeRoute = navItem; |
| 140 | + } |
133 | 141 | });
|
| 142 | + |
| 143 | + expect(activeRoute.route).to.equal(CURRENT_ROUTE); |
134 | 144 | });
|
135 | 145 | });
|
136 | 146 |
|
|
0 commit comments