eigent/test/unit/components/ContentHeaderFrame.test.tsx

190 lines
6.8 KiB
TypeScript

// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
import ContentHeader, {
ContentHeaderFrame,
useFocusContentHeading,
} from '@/components/Layout/ContentHeader';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { AnimatePresence, motion } from 'framer-motion';
import { lazy, Suspense } from 'react';
import { describe, expect, it, vi } from 'vitest';
describe('ContentHeaderFrame', () => {
it('does not focus a heading when its pane becomes outgoing', () => {
function Heading() {
const ref = useFocusContentHeading('detail');
return (
<h1 tabIndex={-1} ref={ref}>
Detail heading
</h1>
);
}
const page = (present: boolean) => (
<>
<button>Next section</button>
<AnimatePresence initial={false}>
{present ? (
<motion.div key="detail" exit={{ opacity: 0 }}>
<Heading />
</motion.div>
) : null}
</AnimatePresence>
</>
);
const view = render(page(true));
expect(
screen.getByRole('heading', { name: 'Detail heading' })
).toHaveFocus();
screen.getByRole('button', { name: 'Next section' }).focus();
view.rerender(page(false));
expect(screen.getByRole('button', { name: 'Next section' })).toHaveFocus();
});
it('keeps the divider outside fading content and preserves header interaction', () => {
const onAdd = vi.fn();
const page = (title: string) => (
<ContentHeaderFrame>
<div data-testid="animated-content" style={{ opacity: 0 }}>
<ContentHeader
persistent
title={title}
actions={<button onClick={onAdd}>Add</button>}
/>
Page body
</div>
</ContentHeaderFrame>
);
const view = render(page('Skills'));
const frame = document.querySelector('[data-content-header-frame]');
const divider = frame?.querySelector('[data-content-header-divider]');
expect(divider).toHaveClass(
'border-b',
'border-ds-hairline-subtle-default'
);
expect(frame).not.toHaveClass('border-b');
expect(screen.getByTestId('animated-content')).not.toContainElement(frame);
expect(frame).toContainElement(screen.getByText('Skills'));
fireEvent.click(screen.getByRole('button', { name: 'Add' }));
expect(onAdd).toHaveBeenCalledOnce();
view.rerender(page('Connectors'));
expect(document.querySelector('[data-content-header-frame]')).toBe(frame);
expect(frame?.querySelector('[data-content-header-divider]')).toBe(divider);
expect(frame).toContainElement(screen.getByText('Connectors'));
expect(screen.queryByText('Skills')).not.toBeInTheDocument();
expect(document.querySelectorAll('header')).toHaveLength(1);
});
it('rejects a late header from an outgoing nested presence tree', async () => {
let resolveHeader!: (module: { default: () => React.JSX.Element }) => void;
const LateHeader = lazy(
() =>
new Promise<{ default: () => React.JSX.Element }>((resolve) => {
resolveHeader = resolve;
})
);
const page = (current: 'skills' | 'connectors') => (
<ContentHeaderFrame>
<AnimatePresence initial={false}>
{current === 'skills' ? (
<motion.div key="skills" exit={{ opacity: 0 }}>
<AnimatePresence propagate>
<motion.div key="skills-section" exit={{ opacity: 0 }}>
<Suspense fallback={<span>Loading skills</span>}>
<LateHeader />
</Suspense>
</motion.div>
</AnimatePresence>
</motion.div>
) : (
<motion.div key="connectors">
<ContentHeader persistent title="Connectors" />
</motion.div>
)}
</AnimatePresence>
</ContentHeaderFrame>
);
const view = render(page('skills'));
const frame = document.querySelector('[data-content-header-frame]');
view.rerender(page('connectors'));
await act(async () =>
resolveHeader({
default: () => (
<ContentHeader persistent title="Stale skills controls" />
),
})
);
expect(document.querySelector('[data-content-header-frame]')).toBe(frame);
expect(frame).toContainElement(screen.getByText('Connectors'));
expect(screen.queryByText('Stale skills controls')).not.toBeInTheDocument();
});
it('does not remount or refocus an outgoing persistent heading', () => {
const focusOutgoing = vi.fn();
const page = (current: 'detail' | 'home') => (
<ContentHeaderFrame>
<AnimatePresence initial={false}>
{current === 'detail' ? (
<motion.div key="detail" exit={{ opacity: 0 }}>
<ContentHeader
persistent
titleAsChild
title={
<h1
tabIndex={-1}
ref={(node) => {
if (node) focusOutgoing();
}}
>
Detail
</h1>
}
/>
</motion.div>
) : (
<motion.div key="home">
<ContentHeader persistent title="Home" />
</motion.div>
)}
</AnimatePresence>
</ContentHeaderFrame>
);
const view = render(page('detail'));
expect(focusOutgoing).toHaveBeenCalledOnce();
view.rerender(page('home'));
expect(focusOutgoing).toHaveBeenCalledOnce();
expect(screen.getByText('Home')).toBeInTheDocument();
expect(screen.queryByText('Detail')).not.toBeInTheDocument();
});
it('honors a borderless persistent header', () => {
render(
<ContentHeaderFrame>
<ContentHeader persistent border={false} title="Borderless" />
</ContentHeaderFrame>
);
expect(
document.querySelector('[data-content-header-divider]')
).not.toBeInTheDocument();
});
it('keeps standalone headers inline when no frame is supplied', () => {
render(<ContentHeader persistent title="Standalone" />);
expect(screen.getByText('Standalone').closest('header')).toHaveClass(
'border-b'
);
});
});