File size: 4,036 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
/**
* @jest-environment jsdom
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react';
import React from 'react';
import { CommandPaletteContext, CommandPaletteContextProvider } from '../src/context';
import { useCommandPalette } from '../src/use-command-palette';
const callback = () => {};
const defaultCommands = [
{
callback,
name: 'getHelp',
label: 'Get help',
},
{
callback,
name: 'clearCache',
label: 'Clear cache',
},
{
callback,
name: 'enableEdgeCache',
label: 'Enable edge cache',
},
];
const commandsWithViewMySite = [
{
callback,
name: 'getHelp',
label: 'Get help',
},
{
callback,
name: 'clearCache',
label: 'Clear cache',
},
{
callback,
name: 'viewMySites',
label: 'View my sites',
},
{
callback,
name: 'enableEdgeCache',
label: 'Enable edge cache',
},
];
const commandsWithViewMySiteResult = [
{
name: 'viewMySites',
label: 'View my sites',
},
{
name: 'getHelp',
label: 'Get help',
},
{
name: 'clearCache',
label: 'Clear cache',
},
{
name: 'enableEdgeCache',
label: 'Enable edge cache',
},
];
const commandsWithContext = [
{
callback,
name: 'getHelp',
label: 'Get help',
context: [ '/sites' ],
},
{
callback,
name: 'clearCache',
label: 'Clear cache',
context: [ '/sites' ],
},
{
callback,
name: 'enableEdgeCache',
label: 'Enable edge cache',
context: [ '/settings' ],
},
];
const commandsWithContextResult = [
{
name: 'enableEdgeCache',
label: 'Enable edge cache',
context: [ '/settings' ],
},
{
name: 'getHelp',
label: 'Get help',
context: [ '/sites' ],
},
{
name: 'clearCache',
label: 'Clear cache',
context: [ '/sites' ],
},
];
jest.mock( 'cmdk', () => ( {
useCommandState: jest.fn(),
} ) );
describe( 'useCommandPalette', () => {
const queryClient = new QueryClient();
const renderUseCommandPalette = ( {
currentRoute = null,
commands,
}: {
currentRoute?: CommandPaletteContext[ 'currentRoute' ];
commands: ReturnType< CommandPaletteContext[ 'useCommands' ] >;
} ) =>
renderHook( () => useCommandPalette(), {
wrapper: ( { children } ) => (
<QueryClientProvider client={ queryClient }>
<CommandPaletteContextProvider
currentRoute={ currentRoute }
currentSiteId={ 1 }
navigate={ () => {} }
useCommands={ () => commands }
userCapabilities={ {} }
useSites={ () => [] }
emptyListNotice=""
placeHolderOverride=""
search=""
selectedCommandName=""
setEmptyListNotice={ () => {} }
setFooterMessage={ () => {} }
setSelectedCommandName={ () => {} }
close={ () => {} }
setSearch={ () => {} }
setPlaceholderOverride={ () => {} }
>
{ children }
</CommandPaletteContextProvider>
</QueryClientProvider>
),
} );
it( 'should return the commands in the order that they are added to the commands array with no change', () => {
const { result } = renderUseCommandPalette( { commands: defaultCommands } );
expect(
result.current.commands.map( ( { name, label } ) => ( { callback, name, label } ) )
).toEqual( defaultCommands );
} );
it( 'should return the View My Sites command first before other commands from commandsWithViewMySite array when no context is specified', () => {
const { result } = renderUseCommandPalette( { commands: commandsWithViewMySite } );
expect( result.current.commands.map( ( { name, label } ) => ( { name, label } ) ) ).toEqual(
commandsWithViewMySiteResult
);
} );
it( 'should return Enable Edge Cache command first as it matches the context; all other commands should follow in the order they are added to commandsWithContext array', () => {
const { result } = renderUseCommandPalette( {
currentRoute: '/settings',
commands: commandsWithContext,
} );
expect(
result.current.commands.map( ( { name, label, context } ) => ( { name, label, context } ) )
).toEqual( commandsWithContextResult );
} );
} );
|