-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBookSidebar.test.tsx
More file actions
75 lines (66 loc) · 2.45 KB
/
Copy pathBookSidebar.test.tsx
File metadata and controls
75 lines (66 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { describe, it, expect, afterEach } from 'vitest';
import { render, screen, cleanup, within } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { BookSidebar } from './BookSidebar';
import type { ResolvedBook } from './book-nav';
afterEach(cleanup);
const book: ResolvedBook = {
name: 'crm_manual',
label: 'CRM Manual',
groups: [
{
key: 'start',
label: 'Getting started',
entries: [
{ doc: 'crm_intro', label: 'Intro' },
{ separator: true },
{ href: 'https://example.com', label: 'External' },
],
},
{ key: 'extra', label: 'Extra', synthetic: true, entries: [{ doc: 'misc_note', label: 'Note' }] },
],
};
function renderSidebar(activeDoc?: string) {
return render(
<MemoryRouter>
<BookSidebar book={book} activeDoc={activeDoc} docHref={(n) => `/docs/${n}`} />
</MemoryRouter>,
);
}
describe('BookSidebar', () => {
it('renders the book label and group headings', () => {
renderSidebar();
expect(screen.getByText('CRM Manual')).toBeInTheDocument();
expect(screen.getByText('Getting started')).toBeInTheDocument();
expect(screen.getByText('Extra')).toBeInTheDocument();
});
it('renders doc links to the resolved href', () => {
renderSidebar();
const link = screen.getByRole('link', { name: 'Intro' });
expect(link).toHaveAttribute('href', '/docs/crm_intro');
});
it('marks the active doc with aria-current', () => {
renderSidebar('crm_intro');
expect(screen.getByRole('link', { name: 'Intro' })).toHaveAttribute('aria-current', 'page');
expect(screen.getByRole('link', { name: 'Note' })).not.toHaveAttribute('aria-current');
});
it('renders external links with target=_blank', () => {
renderSidebar();
const ext = screen.getByRole('link', { name: /External/ });
expect(ext).toHaveAttribute('href', 'https://example.com');
expect(ext).toHaveAttribute('target', '_blank');
});
it('renders a separator as a non-interactive divider', () => {
renderSidebar();
// The "Getting started" group has 2 links (Intro + External) and 1 separator.
const startNav = screen.getByLabelText('CRM Manual');
expect(within(startNav).getAllByRole('link').length).toBe(3); // Intro, External, Note
});
});