|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @jest-environment jsdom |
| 8 | + */ |
| 9 | + |
| 10 | +// Jest doesn't allow pragma below other comments. https://github.com/facebook/jest/issues/12573 |
| 11 | +// eslint-disable-next-line header/header |
| 12 | +import React, {type ReactNode} from 'react'; |
| 13 | +import {render} from '@testing-library/react'; |
| 14 | +import '@testing-library/jest-dom'; |
| 15 | +import DocSidebarItemCategory from '@theme/DocSidebarItem/Category'; |
| 16 | +import DocSidebarItemLink from '@theme/DocSidebarItem/Link'; |
| 17 | +import type {Props as CategoryProps} from '@theme/DocSidebarItem/Category'; |
| 18 | +import type {Props as LinkProps} from '@theme/DocSidebarItem/Link'; |
| 19 | + |
| 20 | +jest.mock('@docusaurus/Link', () => { |
| 21 | + const ReactActual = jest.requireActual<typeof import('react')>('react'); |
| 22 | + |
| 23 | + return { |
| 24 | + __esModule: true, |
| 25 | + default({ |
| 26 | + to, |
| 27 | + href, |
| 28 | + children, |
| 29 | + autoAddBaseUrl: _autoAddBaseUrl, |
| 30 | + ...props |
| 31 | + }: { |
| 32 | + to?: string; |
| 33 | + href?: string; |
| 34 | + children?: ReactNode; |
| 35 | + autoAddBaseUrl?: boolean; |
| 36 | + [key: string]: unknown; |
| 37 | + }) { |
| 38 | + return ReactActual.createElement( |
| 39 | + 'a', |
| 40 | + {href: href ?? to, ...props}, |
| 41 | + children, |
| 42 | + ); |
| 43 | + }, |
| 44 | + }; |
| 45 | +}); |
| 46 | + |
| 47 | +jest.mock('@docusaurus/plugin-content-docs/client', () => ({ |
| 48 | + isActiveSidebarItem: jest.fn(() => false), |
| 49 | + findFirstSidebarItemLink: jest.fn(() => undefined), |
| 50 | + useDocSidebarItemsExpandedState: jest.fn(() => ({ |
| 51 | + expandedItem: null, |
| 52 | + setExpandedItem: jest.fn(), |
| 53 | + })), |
| 54 | + useVisibleSidebarItems: jest.fn((items) => items), |
| 55 | +})); |
| 56 | + |
| 57 | +jest.mock( |
| 58 | + '@docusaurus/theme-common', |
| 59 | + () => { |
| 60 | + const ReactActual = jest.requireActual<typeof import('react')>('react'); |
| 61 | + |
| 62 | + return { |
| 63 | + ThemeClassNames: { |
| 64 | + docs: { |
| 65 | + docSidebarItemCategory: 'docSidebarItemCategory', |
| 66 | + docSidebarItemCategoryLevel: (level: number) => |
| 67 | + `docSidebarItemCategoryLevel${level}`, |
| 68 | + docSidebarItemLink: 'docSidebarItemLink', |
| 69 | + docSidebarItemLinkLevel: (level: number) => |
| 70 | + `docSidebarItemLinkLevel${level}`, |
| 71 | + }, |
| 72 | + }, |
| 73 | + useThemeConfig: jest.fn(() => ({ |
| 74 | + docs: {sidebar: {autoCollapseCategories: false}}, |
| 75 | + })), |
| 76 | + usePrevious: jest.fn(() => undefined), |
| 77 | + Collapsible({children}: {children?: ReactNode}) { |
| 78 | + return ReactActual.createElement(ReactActual.Fragment, null, children); |
| 79 | + }, |
| 80 | + useCollapsible({initialState}: {initialState: () => boolean}) { |
| 81 | + const [collapsed, setCollapsed] = ReactActual.useState(initialState); |
| 82 | + return {collapsed, setCollapsed}; |
| 83 | + }, |
| 84 | + }; |
| 85 | + }, |
| 86 | + {virtual: true}, |
| 87 | +); |
| 88 | + |
| 89 | +jest.mock( |
| 90 | + '@docusaurus/theme-common/internal', |
| 91 | + () => ({ |
| 92 | + isSamePath: jest.fn(() => false), |
| 93 | + }), |
| 94 | + {virtual: true}, |
| 95 | +); |
| 96 | + |
| 97 | +jest.mock('@docusaurus/Translate', () => ({ |
| 98 | + translate: ({message}: {message: string}) => message, |
| 99 | +})); |
| 100 | + |
| 101 | +jest.mock('@docusaurus/useIsBrowser', () => ({ |
| 102 | + __esModule: true, |
| 103 | + default: jest.fn(() => true), |
| 104 | +})); |
| 105 | + |
| 106 | +jest.mock('@theme/DocSidebarItems', () => ({ |
| 107 | + __esModule: true, |
| 108 | + default: jest.fn(() => null), |
| 109 | +})); |
| 110 | + |
| 111 | +jest.mock('@theme/Icon/ExternalLink', () => ({ |
| 112 | + __esModule: true, |
| 113 | + default: jest.fn(() => <span aria-hidden="true" />), |
| 114 | +})); |
| 115 | + |
| 116 | +describe('DocSidebarItem labels', () => { |
| 117 | + it('does not render title attributes for link labels', () => { |
| 118 | + const item: LinkProps['item'] = { |
| 119 | + type: 'link', |
| 120 | + label: 'Installation', |
| 121 | + href: '/docs/installation', |
| 122 | + }; |
| 123 | + |
| 124 | + const {getByText} = render( |
| 125 | + <DocSidebarItemLink item={item} activePath="" level={1} index={0} />, |
| 126 | + ); |
| 127 | + |
| 128 | + expect(getByText('Installation')).not.toHaveAttribute('title'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('does not render title attributes for category labels', () => { |
| 132 | + const item: CategoryProps['item'] = { |
| 133 | + type: 'category', |
| 134 | + label: 'Guides', |
| 135 | + collapsible: false, |
| 136 | + collapsed: false, |
| 137 | + items: [ |
| 138 | + { |
| 139 | + type: 'link', |
| 140 | + label: 'Child', |
| 141 | + href: '/docs/child', |
| 142 | + }, |
| 143 | + ], |
| 144 | + }; |
| 145 | + |
| 146 | + const {getByText} = render( |
| 147 | + <DocSidebarItemCategory item={item} activePath="" level={1} index={0} />, |
| 148 | + ); |
| 149 | + |
| 150 | + expect(getByText('Guides')).not.toHaveAttribute('title'); |
| 151 | + }); |
| 152 | +}); |
0 commit comments