|
| 1 | +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { createRenderer, isJsdom, screen } from '@mui/internal-test-utils'; |
| 4 | +import { ThemeProvider } from '@mui/material/styles'; |
| 5 | +import PageContext from '../../PageContext'; |
| 6 | +import type { MuiPageContext } from '../../PageContext'; |
| 7 | +import { brandingLightTheme } from '../../branding'; |
| 8 | +import { UserLanguageProvider } from '../../i18n'; |
| 9 | +import { AppNavDrawer } from './AppNavDrawer'; |
| 10 | + |
| 11 | +vi.hoisted(() => vi.resetModules()); |
| 12 | +vi.mock('next/router', () => ({ |
| 13 | + useRouter: () => ({ pathname: '/example/', asPath: '/example/' }), |
| 14 | +})); |
| 15 | + |
| 16 | +const noop = () => {}; |
| 17 | +const MOBILE_WIDTH = 412; |
| 18 | +const DESKTOP_WIDTH = 1280; |
| 19 | +const SAVED_SCROLL_TOP = 64; |
| 20 | +const desktopQuery = brandingLightTheme.breakpoints.up('lg').replace('@media ', ''); |
| 21 | +const mobileQuery = brandingLightTheme.breakpoints.down('lg').replace('@media ', ''); |
| 22 | +const activePage = { pathname: '/example/', title: 'Example' }; |
| 23 | +const pageContext: MuiPageContext = { |
| 24 | + activePage, |
| 25 | + activePageParents: [activePage], |
| 26 | + pages: [activePage], |
| 27 | + productId: 'material-ui', |
| 28 | + productCategoryId: 'core', |
| 29 | + productIdentifier: { name: 'Material UI', metadata: '', versions: [] }, |
| 30 | +}; |
| 31 | +const scrollDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'scrollIntoView'); |
| 32 | +const measure = vi.fn(); |
| 33 | +const scroll = vi.fn(); |
| 34 | +let viewportWidth = MOBILE_WIDTH; |
| 35 | + |
| 36 | +function LayoutProbe({ onLayout }: { onLayout?: () => void }) { |
| 37 | + React.useLayoutEffect(() => { |
| 38 | + onLayout?.(); |
| 39 | + }, [onLayout]); |
| 40 | + return null; |
| 41 | +} |
| 42 | + |
| 43 | +function drawer(open: 'closed' | 'open' = 'closed', onLayout?: () => void) { |
| 44 | + return ( |
| 45 | + <UserLanguageProvider defaultUserLanguage="en"> |
| 46 | + <ThemeProvider theme={brandingLightTheme}> |
| 47 | + <PageContext.Provider value={pageContext}> |
| 48 | + <AppNavDrawer |
| 49 | + disablePermanent={false} |
| 50 | + mobileOpen={open === 'open'} |
| 51 | + onClose={noop} |
| 52 | + onOpen={noop} |
| 53 | + /> |
| 54 | + <LayoutProbe onLayout={onLayout} /> |
| 55 | + </PageContext.Provider> |
| 56 | + </ThemeProvider> |
| 57 | + </UserLanguageProvider> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +beforeEach(() => { |
| 62 | + const queries = new Map<string, MediaQueryList>(); |
| 63 | + vi.stubGlobal('matchMedia', (query: string) => { |
| 64 | + if (!queries.has(query)) { |
| 65 | + queries.set(query, { |
| 66 | + matches: |
| 67 | + query === desktopQuery |
| 68 | + ? viewportWidth >= brandingLightTheme.breakpoints.values.lg |
| 69 | + : query === mobileQuery && viewportWidth < brandingLightTheme.breakpoints.values.lg, |
| 70 | + media: query, |
| 71 | + onchange: null, |
| 72 | + addEventListener: noop, |
| 73 | + removeEventListener: noop, |
| 74 | + addListener: noop, |
| 75 | + removeListener: noop, |
| 76 | + dispatchEvent: () => true, |
| 77 | + }); |
| 78 | + } |
| 79 | + return queries.get(query); |
| 80 | + }); |
| 81 | + const getRect = Element.prototype.getBoundingClientRect; |
| 82 | + vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function getRectMock( |
| 83 | + this: Element, |
| 84 | + ) { |
| 85 | + if (this.classList.contains('app-drawer-active')) { |
| 86 | + measure(); |
| 87 | + return { |
| 88 | + x: 0, |
| 89 | + y: -20, |
| 90 | + top: -20, |
| 91 | + bottom: 0, |
| 92 | + left: 0, |
| 93 | + right: 20, |
| 94 | + width: 20, |
| 95 | + height: 20, |
| 96 | + toJSON: noop, |
| 97 | + }; |
| 98 | + } |
| 99 | + return getRect.call(this); |
| 100 | + }); |
| 101 | + Object.defineProperty(Element.prototype, 'scrollIntoView', { |
| 102 | + configurable: true, |
| 103 | + value(this: Element) { |
| 104 | + if (this.classList.contains('app-drawer-active')) { |
| 105 | + scroll(); |
| 106 | + } |
| 107 | + }, |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +afterEach(() => { |
| 112 | + vi.restoreAllMocks(); |
| 113 | + vi.unstubAllGlobals(); |
| 114 | + measure.mockClear(); |
| 115 | + scroll.mockClear(); |
| 116 | + if (scrollDescriptor) { |
| 117 | + Object.defineProperty(Element.prototype, 'scrollIntoView', scrollDescriptor); |
| 118 | + } else { |
| 119 | + Reflect.deleteProperty(Element.prototype, 'scrollIntoView'); |
| 120 | + } |
| 121 | +}); |
| 122 | +afterAll(() => vi.resetModules()); |
| 123 | + |
| 124 | +describe.skipIf(!isJsdom())('AppNavDrawer scroll restoration', () => { |
| 125 | + const { render, renderToString } = createRenderer(); |
| 126 | + |
| 127 | + it('waits until a closed mobile drawer opens before measuring the active link', () => { |
| 128 | + viewportWidth = MOBILE_WIDTH; |
| 129 | + const { hydrate } = renderToString(drawer()); |
| 130 | + const { rerender } = hydrate(); |
| 131 | + |
| 132 | + expect(measure).not.toHaveBeenCalled(); |
| 133 | + expect(scroll).not.toHaveBeenCalled(); |
| 134 | + |
| 135 | + rerender(drawer('open')); |
| 136 | + |
| 137 | + expect(measure).toHaveBeenCalled(); |
| 138 | + expect(scroll).toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('restores desktop scrolling and keeps the active link visible', () => { |
| 142 | + viewportWidth = DESKTOP_WIDTH; |
| 143 | + const layoutReads = vi.fn(); |
| 144 | + const matchMedia = window.matchMedia; |
| 145 | + vi.stubGlobal('matchMedia', undefined); |
| 146 | + const { hydrate } = renderToString( |
| 147 | + drawer('closed', () => layoutReads(measure.mock.calls.length)), |
| 148 | + ); |
| 149 | + vi.stubGlobal('matchMedia', matchMedia); |
| 150 | + const { unmount } = hydrate(); |
| 151 | + |
| 152 | + expect(layoutReads.mock.calls[0][0]).toBeGreaterThan(0); |
| 153 | + |
| 154 | + const link = screen.getByRole('link', { name: 'Example', hidden: true }); |
| 155 | + const scroller = link.closest('ul')!.parentElement!.parentElement!; |
| 156 | + scroller.scrollTop = SAVED_SCROLL_TOP; |
| 157 | + unmount(); |
| 158 | + measure.mockClear(); |
| 159 | + scroll.mockClear(); |
| 160 | + |
| 161 | + render(drawer()); |
| 162 | + |
| 163 | + const restored = screen.getByRole('link', { name: 'Example', hidden: true }).closest('ul')! |
| 164 | + .parentElement!.parentElement!; |
| 165 | + expect(restored.scrollTop).toBe(SAVED_SCROLL_TOP); |
| 166 | + expect(measure).toHaveBeenCalled(); |
| 167 | + expect(scroll).toHaveBeenCalled(); |
| 168 | + }); |
| 169 | +}); |
0 commit comments