File size: 4,000 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * @jest-environment jsdom
 */
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { act, fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import CommandPalette from '../src/index';
import { useCommandPalette } from '../src/use-command-palette';

const commands = [
	{
		name: 'getHelp',
		label: 'Get help',
		callback: ( { close }: { close: () => void } ) => {
			close();
		},
	},
	{
		name: 'sendFeedback',
		label: 'Send feedback',
		searchLabel: 'find help',
		callback: ( { close }: { close: () => void } ) => {
			close();
		},
	},
	{
		name: 'clearCache',
		label: 'Clear cache',
		callback: ( { close }: { close: () => void } ) => {
			close();
		},
	},
	{
		name: 'enableEdgeCache',
		label: 'Enable edge cache',
		callback: ( { close }: { close: () => void } ) => {
			close();
		},
	},
];
const queryClient = new QueryClient();

jest.mock( '../src/use-command-palette' );

window.ResizeObserver = jest.fn( () => ( {
	observe: jest.fn(),
	unobserve: jest.fn(),
	disconnect: jest.fn(),
} ) );

describe( 'CommandPalette', () => {
	const renderCommandPalette = () => {
		( useCommandPalette as jest.Mock ).mockReturnValue( {
			commands: commands,
			filterNotice: 'Mock Filter Notice',
			emptyListNotice: 'No results found',
			currentSiteId: null,
		} );

		render(
			<QueryClientProvider client={ queryClient }>
				<CommandPalette
					currentRoute="/sites"
					currentSiteId={ null }
					navigate={ () => {} }
					useCommands={ () => commands }
					useSites={ () => [] }
				/>
			</QueryClientProvider>
		);

		act( () => {
			fireEvent.keyDown( document, { key: 'k', metaKey: true } );
		} );
	};

	it( 'should confirm that the command palette opens with the commands from the commands array', () => {
		renderCommandPalette();

		expect( screen.getByPlaceholderText( 'Search for commands' ) ).toBeInTheDocument();
		expect( screen.getByText( 'Get help' ) ).toBeInTheDocument();
	} );

	it( 'should return only "Get help" command as it matches label and "Send feedback" as it matches searchLabel; other commands are hidden', () => {
		renderCommandPalette();

		expect( screen.getByPlaceholderText( 'Search for commands' ) ).toBeInTheDocument();
		fireEvent.change( screen.getByPlaceholderText( 'Search for commands' ), {
			target: { value: 'help' },
		} );

		expect( screen.getByText( 'Get' ) ).toBeInTheDocument();
		expect( screen.getByText( 'help' ) ).toBeInTheDocument();
		expect( screen.getByText( 'Send feedback' ) ).toBeInTheDocument();
		expect( screen.queryByText( 'Clear cache' ) ).toBeNull();
		expect( screen.queryByText( 'Enable edge cache' ) ).toBeNull();
	} );

	it( 'should return "No results found" when there is no match for search', () => {
		renderCommandPalette();

		expect( screen.getByPlaceholderText( 'Search for commands' ) ).toBeInTheDocument();
		fireEvent.change( screen.getByPlaceholderText( 'Search for commands' ), {
			target: { value: 'blue' },
		} );

		expect( screen.getByText( 'No results found' ) ).toBeInTheDocument();
		expect( screen.queryByText( 'Send feedback' ) ).toBeNull();
		expect( screen.queryByText( 'Clear cache' ) ).toBeNull();
		expect( screen.queryByText( 'Enable edge cache' ) ).toBeNull();
	} );

	it( 'should close the palette when you select a specific command with no nested commands', () => {
		renderCommandPalette();

		expect( screen.getByPlaceholderText( 'Search for commands' ) ).toBeInTheDocument();
		const getHelpCommand = screen.getByText( 'Get help' );
		fireEvent.click( getHelpCommand );

		expect( screen.queryByPlaceholderText( 'Search for commands' ) ).toBeNull();
	} );

	it( 'should close the command palette when cmd + k is pressed', () => {
		renderCommandPalette();

		const searchInput = screen.queryByPlaceholderText( 'Search for commands' );
		expect( searchInput ).toBeInTheDocument();

		fireEvent.keyDown( document, { key: 'k', metaKey: true } );

		expect( screen.queryByText( 'Get help' ) ).toBeNull();
	} );
} );