|
| 1 | +import type {i18n} from 'i18next'; |
| 2 | +import { |
| 3 | + afterEach, |
| 4 | + beforeAll, |
| 5 | + beforeEach, |
| 6 | + describe, |
| 7 | + expect, |
| 8 | + it, |
| 9 | + vi, |
| 10 | +} from 'vitest'; |
| 11 | +import {createTestI18n} from '@/vitest-utils/testing-helpers/i18n-utils'; |
| 12 | +import {renderStencilVNode} from '@/vitest-utils/testing-helpers/stencil-vnode-renderer'; |
| 13 | +import type {HighlightKeywords} from '../atomic-quickview-modal/atomic-quickview-modal'; |
| 14 | +import {QuickviewWordHighlight} from '../quickview-word-highlight/quickview-word-highlight'; |
| 15 | +import {QuickviewSidebar} from './stencil-atomic-quickview-sidebar'; |
| 16 | +import {identifierKeywordsSection} from './stencil-keywords'; |
| 17 | + |
| 18 | +describe('QuickviewSidebar (Stencil)', () => { |
| 19 | + let container: HTMLElement; |
| 20 | + let i18n: i18n; |
| 21 | + |
| 22 | + beforeAll(async () => { |
| 23 | + i18n = await createTestI18n(); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + container = document.createElement('div'); |
| 28 | + document.body.appendChild(container); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + container.remove(); |
| 33 | + }); |
| 34 | + |
| 35 | + const createWord = (text: string) => { |
| 36 | + const element = document.createElement('span'); |
| 37 | + element.scrollIntoView = vi.fn(); |
| 38 | + return new QuickviewWordHighlight(`${text}-id`, text, '#123456', element); |
| 39 | + }; |
| 40 | + |
| 41 | + const baseHighlightKeywords: HighlightKeywords = { |
| 42 | + highlightNone: false, |
| 43 | + keywords: {}, |
| 44 | + }; |
| 45 | + |
| 46 | + /** |
| 47 | + * Helper to render the Stencil functional component into the DOM. |
| 48 | + * |
| 49 | + * For Lit migration: replace this with a helper that renders the Lit |
| 50 | + * component directly (e.g., renderFunctionFixture). |
| 51 | + */ |
| 52 | + const renderComponent = async (props?: { |
| 53 | + minimized?: boolean; |
| 54 | + highlightKeywords?: HighlightKeywords; |
| 55 | + words?: Record<string, QuickviewWordHighlight>; |
| 56 | + onHighlightKeywords?: (highlight: HighlightKeywords) => void; |
| 57 | + onMinimize?: (minimize: boolean) => void; |
| 58 | + }) => { |
| 59 | + const words = props?.words ?? { |
| 60 | + alpha: createWord('alpha'), |
| 61 | + beta: createWord('beta'), |
| 62 | + }; |
| 63 | + |
| 64 | + const vnode = QuickviewSidebar( |
| 65 | + { |
| 66 | + i18n, |
| 67 | + words, |
| 68 | + minimized: props?.minimized ?? false, |
| 69 | + highlightKeywords: props?.highlightKeywords ?? baseHighlightKeywords, |
| 70 | + onHighlightKeywords: props?.onHighlightKeywords ?? vi.fn(), |
| 71 | + onMinimize: props?.onMinimize ?? vi.fn(), |
| 72 | + }, |
| 73 | + [], |
| 74 | + // biome-ignore lint/suspicious/noExplicitAny: Stencil FunctionalComponent requires utils parameter but it's not used |
| 75 | + {} as any |
| 76 | + ); |
| 77 | + |
| 78 | + const element = await renderStencilVNode(vnode!, container); |
| 79 | + return {element}; |
| 80 | + }; |
| 81 | + |
| 82 | + it('renders keywords and controls when expanded', async () => { |
| 83 | + await renderComponent({minimized: false}); |
| 84 | + |
| 85 | + const keywordsSection = container.querySelector( |
| 86 | + `#${identifierKeywordsSection}` |
| 87 | + ); |
| 88 | + const minimizeButton = container.querySelector( |
| 89 | + '[part="sidebar-minimize-button"]' |
| 90 | + ); |
| 91 | + const highlightLabel = container.querySelector('label'); |
| 92 | + |
| 93 | + expect(keywordsSection).toBeTruthy(); |
| 94 | + expect(minimizeButton).toBeTruthy(); |
| 95 | + expect(highlightLabel?.textContent).toBe(i18n.t('keywords-highlight')); |
| 96 | + }); |
| 97 | + |
| 98 | + it('hides keywords when minimized', async () => { |
| 99 | + await renderComponent({minimized: true}); |
| 100 | + |
| 101 | + const keywordsSection = container.querySelector( |
| 102 | + `#${identifierKeywordsSection}` |
| 103 | + ); |
| 104 | + const highlightLabel = container.querySelector('label'); |
| 105 | + |
| 106 | + expect(keywordsSection).toBeNull(); |
| 107 | + expect(highlightLabel).toBeNull(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('toggles minimize state when clicking the button', async () => { |
| 111 | + const onMinimize = vi.fn(); |
| 112 | + await renderComponent({minimized: false, onMinimize}); |
| 113 | + |
| 114 | + const minimizeButton = container.querySelector( |
| 115 | + '[part="sidebar-minimize-button"]' |
| 116 | + ) as HTMLButtonElement | null; |
| 117 | + |
| 118 | + expect(minimizeButton).toBeTruthy(); |
| 119 | + minimizeButton?.click(); |
| 120 | + |
| 121 | + expect(onMinimize).toHaveBeenCalledWith(true); |
| 122 | + }); |
| 123 | + |
| 124 | + it('propagates highlight checkbox changes', async () => { |
| 125 | + const onHighlightKeywords = vi.fn(); |
| 126 | + await renderComponent({ |
| 127 | + onHighlightKeywords, |
| 128 | + highlightKeywords: {...baseHighlightKeywords, highlightNone: false}, |
| 129 | + }); |
| 130 | + |
| 131 | + const checkbox = container.querySelector( |
| 132 | + '#atomic-quickview-sidebar-highlight-keywords' |
| 133 | + ) as HTMLButtonElement | null; |
| 134 | + |
| 135 | + expect(checkbox).toBeTruthy(); |
| 136 | + checkbox?.click(); |
| 137 | + |
| 138 | + expect(onHighlightKeywords).toHaveBeenCalledWith({ |
| 139 | + highlightNone: true, |
| 140 | + keywords: {}, |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments