Repository navigation
Expand file tree
/
Copy pathDocsSearch.test.tsx
More file actions
68 lines (53 loc) · 2.38 KB
/
Copy pathDocsSearch.test.tsx
File metadata and controls
68 lines (53 loc) · 2.38 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
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { beforeAll, describe, expect, it, vi } from 'vitest';
import { DocsSearch } from '@ValenceDocs/components/DocsSearch/DocsSearch';
import { renderInDocsRouter } from '@ValenceDocs/testing/renderInDocsRouter';
import type { DocPage } from '@ValenceDocs/content/DocPage.types';
const page = (path: string, title: string): DocPage => ({
path,
section: 'install',
sectionTitle: 'Install',
title,
description: `About ${title}.`,
order: 1,
load: () => Promise.reject(new Error('not loaded in a test')),
});
describe('DocsSearch', () => {
beforeAll(() => {
Element.prototype.scrollIntoView = () => undefined;
});
it('opens a palette listing every page from the button', async () => {
const user = userEvent.setup({ delay: null });
await renderInDocsRouter(() => (
<DocsSearch pages={[page('/install/a', 'Alpha'), page('/install/b', 'Beta')]} sources={{}} />
));
expect(screen.queryByText('Alpha')).not.toBeInTheDocument();
await user.click(await screen.findByRole('button', { name: 'Search the docs' }));
expect(await screen.findByText('Alpha')).toBeInTheDocument();
expect(screen.getByText('Beta')).toBeInTheDocument();
});
it('opens from Ctrl and K', async () => {
const user = userEvent.setup({ delay: null });
await renderInDocsRouter(() => (
<DocsSearch pages={[page('/install/a', 'Alpha')]} sources={{}} />
));
await user.keyboard('{Control>}k{/Control}');
expect(await screen.findByRole('combobox')).toBeInTheDocument();
});
it('reads the pages on first use and finds a match in their text', async () => {
const read = vi.fn(() => Promise.resolve('---\ntitle: X\n---\nPut nginx in front of it.'));
const user = userEvent.setup({ delay: null });
await renderInDocsRouter(() => (
<DocsSearch
pages={[page('/install/reverse-proxy', 'Reverse proxy'), page('/install/b', 'Other')]}
sources={{ './install/reverse-proxy.mdx': read }}
/>
));
expect(read).not.toHaveBeenCalled();
await user.click(await screen.findByRole('button', { name: 'Search the docs' }));
await user.type(await screen.findByRole('combobox'), 'nginx');
expect(await screen.findByText('Reverse proxy')).toBeInTheDocument();
expect(screen.queryByText('Other')).not.toBeInTheDocument();
});
});