Skip to content

Commit 3ecff48

Browse files
committed
Add stencil unit tests
1 parent 76d801d commit 3ecff48

File tree

4 files changed

+577
-0
lines changed

4 files changed

+577
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import type {VNode} from '@stencil/core';
2+
import type {i18n} from 'i18next';
3+
import {
4+
afterEach,
5+
beforeAll,
6+
beforeEach,
7+
describe,
8+
expect,
9+
it,
10+
vi,
11+
} from 'vitest';
12+
import {createTestI18n} from '@/vitest-utils/testing-helpers/i18n-utils';
13+
import {renderStencilVNode} from '@/vitest-utils/testing-helpers/stencil-vnode-renderer';
14+
import type {HighlightKeywords} from '../atomic-quickview-modal/atomic-quickview-modal';
15+
import {HighlightKeywordsCheckbox} from './stencil-highlight-keywords-checkbox';
16+
17+
describe('HighlightKeywordsCheckbox (Stencil)', () => {
18+
let container: HTMLElement;
19+
let i18n: i18n;
20+
21+
beforeAll(async () => {
22+
i18n = await createTestI18n();
23+
});
24+
25+
beforeEach(() => {
26+
container = document.createElement('div');
27+
document.body.appendChild(container);
28+
});
29+
30+
afterEach(() => {
31+
container.remove();
32+
});
33+
34+
const baseHighlightKeywords: HighlightKeywords = {
35+
highlightNone: false,
36+
keywords: {},
37+
};
38+
39+
/**
40+
* Helper to render the Stencil functional component into the DOM.
41+
*
42+
* For Lit migration: replace this with a helper that renders the Lit
43+
* component directly (e.g., renderFunctionFixture).
44+
*/
45+
const renderComponent = async (props?: {
46+
highlightKeywords?: HighlightKeywords;
47+
onHighlightKeywords?: (highlight: HighlightKeywords) => void;
48+
minimized?: boolean;
49+
}) => {
50+
const onHighlightKeywords = props?.onHighlightKeywords ?? vi.fn();
51+
const highlightKeywords = props?.highlightKeywords ?? baseHighlightKeywords;
52+
53+
const vnode = HighlightKeywordsCheckbox(
54+
{
55+
i18n,
56+
highlightKeywords,
57+
onHighlightKeywords,
58+
minimized: props?.minimized ?? false,
59+
},
60+
[],
61+
// biome-ignore lint/suspicious/noExplicitAny: Stencil FunctionalComponent requires utils parameter but it's not used
62+
{} as any
63+
) as VNode;
64+
65+
await renderStencilVNode(vnode, container);
66+
67+
return {onHighlightKeywords};
68+
};
69+
70+
it('renders the checkbox with translated label', async () => {
71+
await renderComponent({minimized: false});
72+
73+
const checkbox = container.querySelector(
74+
'#atomic-quickview-sidebar-highlight-keywords'
75+
) as HTMLButtonElement | null;
76+
const label = container.querySelector('label');
77+
78+
const translation = i18n.t('keywords-highlight');
79+
80+
expect(checkbox).toBeTruthy();
81+
expect(label?.textContent).toBe(translation);
82+
expect(label?.getAttribute('for')).toBe(
83+
'atomic-quickview-sidebar-highlight-keywords'
84+
);
85+
expect(checkbox?.getAttribute('aria-checked')).toBe('true');
86+
});
87+
88+
it('hides the label when minimized', async () => {
89+
await renderComponent({minimized: true});
90+
91+
const label = container.querySelector('label');
92+
expect(label).toBeNull();
93+
});
94+
95+
it('calls onHighlightKeywords with highlightNone toggled when checkbox changes', async () => {
96+
const onHighlightKeywords = vi.fn();
97+
await renderComponent({
98+
onHighlightKeywords,
99+
highlightKeywords: {...baseHighlightKeywords, highlightNone: true},
100+
});
101+
102+
const checkbox = container.querySelector(
103+
'#atomic-quickview-sidebar-highlight-keywords'
104+
) as HTMLButtonElement | null;
105+
106+
expect(checkbox?.getAttribute('aria-checked')).toBe('false');
107+
108+
checkbox?.click();
109+
110+
expect(onHighlightKeywords).toHaveBeenCalledWith({
111+
highlightNone: false,
112+
keywords: {},
113+
});
114+
});
115+
});

0 commit comments

Comments
 (0)