|
| 1 | +open Vitest |
| 2 | + |
| 3 | +@module("vitest/browser") @scope("userEvent") |
| 4 | +external keyboard: string => promise<unit> = "keyboard" |
| 5 | + |
| 6 | +@send external toHaveFocus: element => promise<unit> = "toHaveFocus" |
| 7 | + |
| 8 | +let categories: array<SidebarNav.Category.t> = [ |
| 9 | + { |
| 10 | + name: "Resources", |
| 11 | + items: [ |
| 12 | + {name: "Overview", href: "/community/overview"}, |
| 13 | + {name: "Code of Conduct", href: "/community/code-of-conduct"}, |
| 14 | + ], |
| 15 | + }, |
| 16 | +] |
| 17 | + |
| 18 | +let entries: array<TableOfContents.entry> = [ |
| 19 | + {header: "Official Channels", href: "#official-channels"}, |
| 20 | +] |
| 21 | + |
| 22 | +module CurrentPage = { |
| 23 | + @react.component |
| 24 | + let make = () => { |
| 25 | + let {pathname} = ReactRouter.useLocation() |
| 26 | + <div dataTestId="current-page"> {React.string((pathname :> string))} </div> |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +module Redirect = { |
| 31 | + @module("react-router") @react.component |
| 32 | + external make: (~to: string) => React.element = "Navigate" |
| 33 | +} |
| 34 | + |
| 35 | +let community = (~redirect=false, ()) => |
| 36 | + <React.StrictMode> |
| 37 | + <ReactRouter.MemoryRouter initialEntries=["/community/overview"]> |
| 38 | + {redirect ? <Redirect to="/community/code-of-conduct" /> : React.null} |
| 39 | + <CommunityLayout categories entries> |
| 40 | + <div> |
| 41 | + <CurrentPage /> |
| 42 | + <h2 id="official-channels"> {React.string("Official channels content")} </h2> |
| 43 | + </div> |
| 44 | + </CommunityLayout> |
| 45 | + </ReactRouter.MemoryRouter> |
| 46 | + </React.StrictMode> |
| 47 | + |
| 48 | +let renderCommunity = () => render(community()) |
| 49 | + |
| 50 | +test("mobile community navigation opens and closes", async () => { |
| 51 | + await viewport(390, 844) |
| 52 | + let screen = await renderCommunity() |
| 53 | + let trigger = await screen->getByText("Community navigation") |
| 54 | + let dialog = await screen->getByTestId("community-mobile-sidebar") |
| 55 | + |
| 56 | + await element(trigger)->toBeVisible |
| 57 | + await element(trigger)->toHaveAttribute("aria-expanded", "false") |
| 58 | + await element(dialog)->notToBeInTheDocument |
| 59 | + await trigger->click |
| 60 | + |
| 61 | + await element(trigger)->toHaveAttribute("aria-expanded", "true") |
| 62 | + await element(dialog)->toBeVisible |
| 63 | + let resources = await dialog->getByText("Resources") |
| 64 | + await element(resources)->toBeVisible |
| 65 | + let close = await dialog->getByLabelText("Close community navigation") |
| 66 | + await close->click |
| 67 | + await element(dialog)->notToBeInTheDocument |
| 68 | + await element(trigger)->toHaveAttribute("aria-expanded", "false") |
| 69 | + await element(trigger)->toHaveFocus |
| 70 | +}) |
| 71 | + |
| 72 | +test("Escape closes community navigation and returns focus to the opener", async () => { |
| 73 | + await viewport(390, 844) |
| 74 | + let screen = await renderCommunity() |
| 75 | + let trigger = await screen->getByText("Community navigation") |
| 76 | + let dialog = await screen->getByTestId("community-mobile-sidebar") |
| 77 | + await trigger->click |
| 78 | + await element(dialog)->toBeVisible |
| 79 | + |
| 80 | + await keyboard("{Escape}") |
| 81 | + await element(dialog)->notToBeInTheDocument |
| 82 | + await element(trigger)->toHaveAttribute("aria-expanded", "false") |
| 83 | + await element(trigger)->toHaveFocus |
| 84 | +}) |
| 85 | + |
| 86 | +test("community page links close the drawer, including the current page link", async () => { |
| 87 | + await viewport(390, 844) |
| 88 | + let screen = await renderCommunity() |
| 89 | + let trigger = await screen->getByText("Community navigation") |
| 90 | + let dialog = await screen->getByTestId("community-mobile-sidebar") |
| 91 | + await trigger->click |
| 92 | + let link = await dialog->getByText("Code of Conduct") |
| 93 | + await link->click |
| 94 | + |
| 95 | + let currentPage = await screen->getByTestId("current-page") |
| 96 | + await element(currentPage)->toHaveTextContent("/community/code-of-conduct") |
| 97 | + await element(dialog)->notToBeInTheDocument |
| 98 | + |
| 99 | + await trigger->click |
| 100 | + let currentPageLink = await dialog->getByText("Code of Conduct") |
| 101 | + await currentPageLink->click |
| 102 | + await element(dialog)->notToBeInTheDocument |
| 103 | +}) |
| 104 | + |
| 105 | +test("community table of contents links close the mobile drawer", async () => { |
| 106 | + await viewport(390, 844) |
| 107 | + let screen = await renderCommunity() |
| 108 | + let trigger = await screen->getByText("Community navigation") |
| 109 | + let dialog = await screen->getByTestId("community-mobile-sidebar") |
| 110 | + await trigger->click |
| 111 | + let tocLink = await dialog->getByText("Official Channels") |
| 112 | + await tocLink->click |
| 113 | + await element(dialog)->notToBeInTheDocument |
| 114 | +}) |
| 115 | + |
| 116 | +test( |
| 117 | + "desktop community navigation stays visible and resizing dismisses an open drawer", |
| 118 | + async () => { |
| 119 | + await viewport(390, 844) |
| 120 | + let screen = await renderCommunity() |
| 121 | + let trigger = await screen->getByText("Community navigation") |
| 122 | + let dialog = await screen->getByTestId("community-mobile-sidebar") |
| 123 | + await trigger->click |
| 124 | + await element(dialog)->toBeVisible |
| 125 | + |
| 126 | + await viewport(900, 900) |
| 127 | + await element(dialog)->notToBeInTheDocument |
| 128 | + await element(trigger)->notToBeVisible |
| 129 | + let resources = await screen->getByText("Resources") |
| 130 | + await element(resources)->toBeVisible |
| 131 | + |
| 132 | + await viewport(390, 844) |
| 133 | + await element(trigger)->toBeVisible |
| 134 | + await element(trigger)->toHaveAttribute("aria-expanded", "false") |
| 135 | + }, |
| 136 | +) |
| 137 | + |
| 138 | +test("rerendering community layout preserves the open dialog and keyboard focus", async () => { |
| 139 | + await viewport(390, 844) |
| 140 | + let screen = await renderCommunity() |
| 141 | + let trigger = await screen->getByText("Community navigation") |
| 142 | + let dialog = await screen->getByTestId("community-mobile-sidebar") |
| 143 | + await trigger->click |
| 144 | + await element(dialog)->toBeVisible |
| 145 | + await keyboard("{Tab}") |
| 146 | + let overview = await dialog->getByText("Overview") |
| 147 | + await element(overview)->toHaveFocus |
| 148 | + |
| 149 | + await screen->rerender(community()) |
| 150 | + await element(dialog)->toBeVisible |
| 151 | + await element(overview)->toHaveFocus |
| 152 | +}) |
| 153 | + |
| 154 | +test("route changes outside the drawer reset mobile navigation", async () => { |
| 155 | + await viewport(390, 844) |
| 156 | + let screen = await renderCommunity() |
| 157 | + let trigger = await screen->getByText("Community navigation") |
| 158 | + let dialog = await screen->getByTestId("community-mobile-sidebar") |
| 159 | + await trigger->click |
| 160 | + await element(dialog)->toBeVisible |
| 161 | + |
| 162 | + await screen->rerender(community(~redirect=true, ())) |
| 163 | + let currentPage = await screen->getByTestId("current-page") |
| 164 | + await element(currentPage)->toHaveTextContent("/community/code-of-conduct") |
| 165 | + await element(dialog)->notToBeInTheDocument |
| 166 | + await element(trigger)->toHaveAttribute("aria-expanded", "false") |
| 167 | + await trigger->click |
| 168 | + await element(dialog)->toBeVisible |
| 169 | +}) |
0 commit comments