|
| 1 | +open Cy |
| 2 | + |
| 3 | +// Wait for the app to fully hydrate before interacting with links. |
| 4 | +// The production build is pre-rendered so React must attach event |
| 5 | +// handlers before client-side navigation works. |
| 6 | +let waitForHydration = () => { |
| 7 | + getByTestId("navbar-primary")->shouldBeVisible->ignore |
| 8 | + cyWindow()->its("document.readyState")->shouldWithValue("eq", "complete")->ignore |
| 9 | + wait(2000) |
| 10 | +} |
| 11 | + |
| 12 | +// Use short, re-queryable selectors to avoid detached DOM issues. |
| 13 | +// When React re-renders during navigation, long chains can hold |
| 14 | +// references to stale elements. Separate cy.get() calls let Cypress |
| 15 | +// re-query from the DOM root on each retry. |
| 16 | + |
| 17 | +let clickNavLink = (~testId, ~text) => { |
| 18 | + get(`[data-testid="${testId}"] a:visible`) |
| 19 | + ->containsChainable(text) |
| 20 | + ->click |
| 21 | + ->ignore |
| 22 | +} |
| 23 | + |
| 24 | +let clickMobileNavLink = text => { |
| 25 | + get(`[data-testid="mobile-nav"] a:visible`) |
| 26 | + ->containsChainable(text) |
| 27 | + ->click |
| 28 | + ->ignore |
| 29 | +} |
| 30 | + |
| 31 | +let openMobileMenu = () => { |
| 32 | + get(`[data-testid="toggle-mobile-overlay"]`)->should("be.visible")->click->ignore |
| 33 | + get("#mobile-overlay")->should("be.visible")->ignore |
| 34 | +} |
| 35 | + |
| 36 | +// -- Desktop (1280x720) ------------------------------------------------------- |
| 37 | + |
| 38 | +describe("Desktop Navigation", () => { |
| 39 | + beforeEach(() => { |
| 40 | + viewport(1280, 720) |
| 41 | + visit("/") |
| 42 | + waitForHydration() |
| 43 | + }) |
| 44 | + |
| 45 | + describe("Primary navbar", () => { |
| 46 | + it( |
| 47 | + "should navigate to Docs via navbar link", |
| 48 | + () => { |
| 49 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Docs") |
| 50 | + url()->shouldInclude("/docs/manual/introduction")->ignore |
| 51 | + get("h1")->shouldBeVisible->ignore |
| 52 | + }, |
| 53 | + ) |
| 54 | + |
| 55 | + it( |
| 56 | + "should navigate to Playground via navbar link", |
| 57 | + () => { |
| 58 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Playground") |
| 59 | + url()->shouldInclude("/try")->ignore |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + it( |
| 64 | + "should navigate to Blog via navbar link", |
| 65 | + () => { |
| 66 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Blog") |
| 67 | + url()->shouldInclude("/blog")->ignore |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + it( |
| 72 | + "should navigate to Community via navbar link", |
| 73 | + () => { |
| 74 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Community") |
| 75 | + url()->shouldInclude("/community")->ignore |
| 76 | + get("h1")->shouldBeVisible->ignore |
| 77 | + }, |
| 78 | + ) |
| 79 | + |
| 80 | + it( |
| 81 | + "should navigate home via logo after clicking away", |
| 82 | + () => { |
| 83 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Blog") |
| 84 | + url()->shouldInclude("/blog")->ignore |
| 85 | + |
| 86 | + get("a[aria-label='homepage']")->should("be.visible")->first->click->ignore |
| 87 | + cyLocation("pathname")->shouldWithValue("eq", "/")->ignore |
| 88 | + }, |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + describe("Secondary navbar", () => { |
| 93 | + it( |
| 94 | + "should navigate through all secondary nav links from Docs", |
| 95 | + () => { |
| 96 | + // Click Docs in primary nav to reveal the secondary nav |
| 97 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Docs") |
| 98 | + url()->shouldInclude("/docs/manual/introduction")->ignore |
| 99 | + |
| 100 | + // Language Manual |
| 101 | + clickNavLink(~testId="navbar-secondary", ~text="Language Manual") |
| 102 | + url()->shouldInclude("/docs/manual/introduction")->ignore |
| 103 | + |
| 104 | + // API |
| 105 | + clickNavLink(~testId="navbar-secondary", ~text="API") |
| 106 | + url()->shouldInclude("/docs/manual/api")->ignore |
| 107 | + |
| 108 | + // Syntax Lookup |
| 109 | + clickNavLink(~testId="navbar-secondary", ~text="Syntax Lookup") |
| 110 | + url()->shouldInclude("/syntax-lookup")->ignore |
| 111 | + |
| 112 | + // React |
| 113 | + clickNavLink(~testId="navbar-secondary", ~text="React") |
| 114 | + url()->shouldInclude("/docs/react/introduction")->ignore |
| 115 | + }, |
| 116 | + ) |
| 117 | + }) |
| 118 | +}) |
| 119 | + |
| 120 | +// -- Mobile (375x667) --------------------------------------------------------- |
| 121 | + |
| 122 | +describe("Mobile Navigation", () => { |
| 123 | + beforeEach(() => { |
| 124 | + viewport(375, 667) |
| 125 | + visit("/") |
| 126 | + waitForHydration() |
| 127 | + }) |
| 128 | + |
| 129 | + describe("Primary navbar", () => { |
| 130 | + it( |
| 131 | + "should navigate to Docs via navbar link", |
| 132 | + () => { |
| 133 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Docs") |
| 134 | + url()->shouldInclude("/docs/manual/introduction")->ignore |
| 135 | + get("h1")->shouldBeVisible->ignore |
| 136 | + }, |
| 137 | + ) |
| 138 | + |
| 139 | + it( |
| 140 | + "should navigate home via logo after clicking away", |
| 141 | + () => { |
| 142 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Docs") |
| 143 | + url()->shouldInclude("/docs/manual/introduction")->ignore |
| 144 | + |
| 145 | + get("a[aria-label='homepage']")->should("be.visible")->first->click->ignore |
| 146 | + cyLocation("pathname")->shouldWithValue("eq", "/")->ignore |
| 147 | + }, |
| 148 | + ) |
| 149 | + }) |
| 150 | + |
| 151 | + describe("Mobile overlay navigation", () => { |
| 152 | + it( |
| 153 | + "should navigate to Playground via mobile menu", |
| 154 | + () => { |
| 155 | + openMobileMenu() |
| 156 | + clickMobileNavLink("Playground") |
| 157 | + url()->shouldInclude("/try")->ignore |
| 158 | + }, |
| 159 | + ) |
| 160 | + |
| 161 | + it( |
| 162 | + "should navigate to Blog via mobile menu", |
| 163 | + () => { |
| 164 | + openMobileMenu() |
| 165 | + clickMobileNavLink("Blog") |
| 166 | + url()->shouldInclude("/blog")->ignore |
| 167 | + }, |
| 168 | + ) |
| 169 | + |
| 170 | + it( |
| 171 | + "should navigate to Community via mobile menu", |
| 172 | + () => { |
| 173 | + openMobileMenu() |
| 174 | + clickMobileNavLink("Community") |
| 175 | + url()->shouldInclude("/community")->ignore |
| 176 | + get("h1")->shouldBeVisible->ignore |
| 177 | + }, |
| 178 | + ) |
| 179 | + }) |
| 180 | + |
| 181 | + describe("Secondary navbar", () => { |
| 182 | + it( |
| 183 | + "should navigate through all secondary nav links from Docs", |
| 184 | + () => { |
| 185 | + // Click Docs in primary nav to reveal the secondary nav |
| 186 | + clickNavLink(~testId="navbar-primary-left-content", ~text="Docs") |
| 187 | + url()->shouldInclude("/docs/manual/introduction")->ignore |
| 188 | + |
| 189 | + // Scroll to top so the secondary nav is visible |
| 190 | + cyScrollTo("top") |
| 191 | + |
| 192 | + // Language Manual |
| 193 | + clickNavLink(~testId="navbar-secondary", ~text="Language Manual") |
| 194 | + url()->shouldInclude("/docs/manual/introduction")->ignore |
| 195 | + |
| 196 | + // API |
| 197 | + cyScrollTo("top") |
| 198 | + clickNavLink(~testId="navbar-secondary", ~text="API") |
| 199 | + url()->shouldInclude("/docs/manual/api")->ignore |
| 200 | + |
| 201 | + // Syntax Lookup |
| 202 | + cyScrollTo("top") |
| 203 | + clickNavLink(~testId="navbar-secondary", ~text="Syntax Lookup") |
| 204 | + url()->shouldInclude("/syntax-lookup")->ignore |
| 205 | + |
| 206 | + // React |
| 207 | + cyScrollTo("top") |
| 208 | + clickNavLink(~testId="navbar-secondary", ~text="React") |
| 209 | + url()->shouldInclude("/docs/react/introduction")->ignore |
| 210 | + }, |
| 211 | + ) |
| 212 | + }) |
| 213 | +}) |
0 commit comments