feat(app): editor sidebar hidden below 1 280 px, menu one tap away
AC9: on the draft editor path AppLayout uses the xl class set (sidebar
from 1 280 px, menu button and brand below); other pages keep lg.
AppLayout split: Sidebar and TopBar in ShellChrome.tsx. MobileDrawer
takes closesFrom ('64rem' | '80rem') and also closes at setup when
its query already matches.
Rewritten assertions (R-24):
- shell.AppLayout "opens the mobile drawer" and shell.navigation
"carries the same entries into the mobile drawer": stub a narrow
matchMedia, the drawer now closes at setup at the happy-dom width.
New: unit tests (class sets, 64rem/80rem close, setup close, leave
guard on a drawer link) and editor-shell.spec F journeys (sidebar at
1024/1180/1279/1280, one-tap menu, drawer open at 1180, guard).
This commit is contained in:
1 parent
89da8d3f55
commit
580ff28214
6 files changed
+317
-74
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { cleanup, render, screen, within } from '@testing-library/react'
|
||||
import { act, cleanup, render, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
||||
import { createFakeSupabase, fakeProfile, fakeUser, type FakeSupabase } from '../helpers/fakeSupabase'
|
||||
@@ -13,6 +13,7 @@ vi.mock('@/lib/supabase', () => ({
|
||||
import AppLayout from '@/components/shell/AppLayout'
|
||||
import { monogram } from '@/components/shell/UserBadge'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { fr } from '@/i18n/fr'
|
||||
|
||||
@@ -31,7 +32,28 @@ beforeEach(() => {
|
||||
avatarUrl: null,
|
||||
})
|
||||
})
|
||||
afterEach(cleanup)
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.unstubAllGlobals()
|
||||
useLeaveGuardStore.getState().arm(false)
|
||||
})
|
||||
|
||||
/** A matchMedia whose queries match from `widthRem`; `fire` flips it to a wider screen. */
|
||||
function stubScreen(widthRem: number) {
|
||||
const listeners = new Map<string, Set<(e: { matches: boolean }) => void>>()
|
||||
const min = (query: string) => Number(/min-width: (\d+)rem/.exec(query)![1])
|
||||
vi.stubGlobal('matchMedia', (query: string) => ({
|
||||
matches: widthRem >= min(query),
|
||||
addEventListener: (_: string, fn: (e: { matches: boolean }) => void) => {
|
||||
listeners.set(query, (listeners.get(query) ?? new Set()).add(fn))
|
||||
},
|
||||
removeEventListener: (_: string, fn: (e: { matches: boolean }) => void) => {
|
||||
listeners.get(query)?.delete(fn)
|
||||
},
|
||||
}))
|
||||
return (query: string) =>
|
||||
act(() => listeners.get(query)?.forEach((fn) => fn({ matches: true })))
|
||||
}
|
||||
|
||||
function mount(path = '/app') {
|
||||
return render(
|
||||
@@ -105,6 +127,7 @@ describe('AppLayout', () => {
|
||||
|
||||
it('opens the mobile drawer from the menu button and closes it on navigation', async () => {
|
||||
const user = userEvent.setup()
|
||||
stubScreen(40)
|
||||
mount()
|
||||
await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] }))
|
||||
const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
|
||||
@@ -124,6 +147,70 @@ describe('AppLayout', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('AppLayout, editor sidebar from 1 280 px', () => {
|
||||
const sidebar = () => document.querySelector('aside') as HTMLElement
|
||||
const topBarMenu = () => screen.getByRole('button', { name: fr['shell.open_menu'], hidden: true })
|
||||
|
||||
it('uses the xl class set on the editor path, the lg set elsewhere', () => {
|
||||
mount('/app/admin/sheets/s-1/draft')
|
||||
expect(sidebar()).toHaveClass('xl:flex')
|
||||
expect(sidebar()).not.toHaveClass('lg:flex')
|
||||
expect(topBarMenu()).toHaveClass('xl:hidden')
|
||||
expect(screen.getByRole('heading', { level: 1, hidden: true })).toHaveClass('xl:block')
|
||||
cleanup()
|
||||
mount('/app/admin/sheets/s-1')
|
||||
expect(sidebar()).toHaveClass('lg:flex')
|
||||
expect(sidebar()).not.toHaveClass('xl:flex')
|
||||
expect(topBarMenu()).toHaveClass('lg:hidden')
|
||||
})
|
||||
|
||||
it('keeps the drawer open at 1 180 px in the editor, closes it from 1 280 px', async () => {
|
||||
const user = userEvent.setup()
|
||||
const fire = stubScreen(73.75)
|
||||
mount('/app/admin/sheets/s-1/draft')
|
||||
await user.click(topBarMenu())
|
||||
expect(await screen.findByRole('dialog', { name: fr['shell.nav_label'] })).toBeInTheDocument()
|
||||
fire('(min-width: 64rem)')
|
||||
expect(screen.getByRole('dialog', { name: fr['shell.nav_label'] })).toBeInTheDocument()
|
||||
fire('(min-width: 80rem)')
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
||||
})
|
||||
|
||||
it('closes the drawer on the 64rem query on other pages', async () => {
|
||||
const user = userEvent.setup()
|
||||
const fire = stubScreen(50)
|
||||
mount('/app')
|
||||
await user.click(topBarMenu())
|
||||
await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
|
||||
fire('(min-width: 64rem)')
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
||||
})
|
||||
|
||||
it('closes at setup when its query already matches', async () => {
|
||||
const user = userEvent.setup()
|
||||
stubScreen(73.75)
|
||||
mount('/app')
|
||||
await user.click(topBarMenu())
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
||||
cleanup()
|
||||
mount('/app/admin/sheets/s-1/draft')
|
||||
await user.click(topBarMenu())
|
||||
expect(await screen.findByRole('dialog', { name: fr['shell.nav_label'] })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('asks before leaving from a drawer link while changes are unsaved', async () => {
|
||||
const user = userEvent.setup()
|
||||
stubScreen(73.75)
|
||||
mount('/app/admin/sheets/s-1/draft')
|
||||
useLeaveGuardStore.getState().arm(true)
|
||||
await user.click(topBarMenu())
|
||||
const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
|
||||
await user.click(within(drawer).getByRole('link', { name: new RegExp(fr['shell.nav.account']) }))
|
||||
expect(screen.queryByText('account page')).toBeNull()
|
||||
expect(await screen.findByText(fr['leave.title'])).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe('monogram', () => {
|
||||
it('takes initials, else two letters, else ?', () => {
|
||||
expect(monogram('Alice Local', undefined)).toBe('AL')
|
||||
|
||||
@@ -80,7 +80,10 @@ beforeEach(() => {
|
||||
avatarUrl: null,
|
||||
})
|
||||
})
|
||||
afterEach(cleanup)
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe('SidebarNav entries', () => {
|
||||
it('hides both V1-2 entries from an account without a right', () => {
|
||||
@@ -126,6 +129,12 @@ describe('SidebarNav entries', () => {
|
||||
|
||||
it('carries the same entries into the mobile drawer', async () => {
|
||||
const user = userEvent.setup()
|
||||
// A narrow screen: the drawer closes by itself once the sidebar breakpoint matches.
|
||||
vi.stubGlobal('matchMedia', () => ({
|
||||
matches: false,
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
}))
|
||||
mount([grant('sheet_editor')])
|
||||
await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] }))
|
||||
const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
|
||||
|
||||
Reference in new issue
Block a user