@@ -3,14 +3,18 @@ import userEvent from '@testing-library/user-event'
33import { ReactFlowProvider } from '@xyflow/react'
44import { NuqsTestingAdapter } from 'nuqs/adapters/testing'
55import type { FC , ReactNode } from 'react'
6- import { afterEach , describe , expect , it , vi } from 'vitest'
6+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
77import { UserEditingProvider } from '../../../../../../../stores'
88import * as UseTableSelection from '../../../../../hooks'
99import { CommandPaletteProvider } from '../../CommandPaletteProvider'
1010import * as UseCommandPalette from '../../CommandPaletteProvider/hooks'
1111import type { CommandPaletteSuggestion } from '../../types'
1212import { useTableOptionSelect } from './useTableOptionSelect'
1313
14+ beforeEach ( ( ) => {
15+ window . location . hash = ''
16+ } )
17+
1418afterEach ( ( ) => {
1519 vi . clearAllMocks ( )
1620} )
@@ -86,9 +90,57 @@ describe('keyboard interactions', () => {
8690 } )
8791 } )
8892
93+ describe ( 'when suggestion is a column' , ( ) => {
94+ it ( 'moves to suggested table column in ERD and closes the dialog on Enter' , async ( ) => {
95+ const user = userEvent . setup ( )
96+ renderHook (
97+ ( ) =>
98+ useTableOptionSelect ( {
99+ type : 'column' ,
100+ tableName : 'users' ,
101+ columnName : 'name' ,
102+ } ) ,
103+ { wrapper } ,
104+ )
105+
106+ await user . keyboard ( '{Enter}' )
107+
108+ expect ( mockSelectTable ) . toHaveBeenCalledWith ( {
109+ displayArea : 'main' ,
110+ tableId : 'users' ,
111+ } )
112+ expect ( mockSetCommandPaletteDialogOpen ) . toHaveBeenCalledWith ( false )
113+
114+ // other functions are not called
115+ expect ( mockWindowOpen ) . not . toHaveBeenCalled ( )
116+ } )
117+
118+ it ( 'opens suggested table in another tab on ⌘Enter' , async ( ) => {
119+ const user = userEvent . setup ( )
120+ renderHook (
121+ ( ) =>
122+ useTableOptionSelect ( {
123+ type : 'column' ,
124+ tableName : 'users' ,
125+ columnName : 'name' ,
126+ } ) ,
127+ { wrapper } ,
128+ )
129+
130+ await user . keyboard ( '{Meta>}{Enter}{/Meta}' )
131+
132+ expect ( mockWindowOpen ) . toHaveBeenCalledWith (
133+ '?active=users#users__columns__name' ,
134+ )
135+
136+ // other functions are not called
137+ expect ( mockSelectTable ) . not . toHaveBeenCalled ( )
138+ expect ( mockSetCommandPaletteDialogOpen ) . not . toHaveBeenCalled ( )
139+ } )
140+ } )
141+
89142 describe . each < CommandPaletteSuggestion | null > ( [
90143 { type : 'command' , name : 'copy link' } ,
91- { type : 'column' , tableName : 'users' , columnName : 'created_at' } ,
92144 null ,
93145 ] ) ( 'when suggestion is other than tables, suggestion = %o' , ( suggestion ) => {
94146 it ( 'does nothing when on Enter' , async ( ) => {
@@ -115,48 +167,99 @@ describe('keyboard interactions', () => {
115167 } )
116168} )
117169
118- describe ( 'tableOptionSelectHandler ' , ( ) => {
119- // a component for testing the "tableOptionSelectHandler " method of the hook
170+ describe ( 'optionSelectHandler ' , ( ) => {
171+ // a component for testing the "optionSelectHandler " method of the hook
120172 // in the test cases, we simulate the method clicking a link of a table option
121- const TableOptionLinkWithSelectHandler : FC < { tableName : string } > = ( {
122- tableName,
123- } ) => {
124- const { tableOptionSelectHandler } = useTableOptionSelect ( null )
173+ const TableOptionLinkWithSelectHandler : FC < {
174+ tableName : string
175+ columnName : string | undefined
176+ } > = ( { tableName, columnName } ) => {
177+ const { optionSelectHandler } = useTableOptionSelect ( null )
125178
126179 return (
127180 < a
128181 href = "/"
129182 // use the handler by passing the event object and table name
130- onClick = { ( event ) => tableOptionSelectHandler ( event , tableName ) }
183+ onClick = { ( event ) => optionSelectHandler ( event , tableName , columnName ) }
131184 >
132185 table option link
133186 </ a >
134187 )
135188 }
136189
137- it ( 'moves to clicked table in ERD and closes the dialog' , async ( ) => {
138- const user = userEvent . setup ( )
139- render ( < TableOptionLinkWithSelectHandler tableName = "follows" /> , {
140- wrapper,
190+ describe ( 'when passing only tableName' , ( ) => {
191+ it ( 'moves to clicked table in ERD and closes the dialog' , async ( ) => {
192+ const user = userEvent . setup ( )
193+ render (
194+ < TableOptionLinkWithSelectHandler
195+ tableName = "follows"
196+ columnName = { undefined }
197+ /> ,
198+ { wrapper } ,
199+ )
200+
201+ await user . click ( screen . getByRole ( 'link' , { name : 'table option link' } ) )
202+
203+ expect ( mockSelectTable ) . toHaveBeenCalled ( )
204+ expect ( mockSetCommandPaletteDialogOpen ) . toHaveBeenCalledWith ( false )
141205 } )
142206
143- await user . click ( screen . getByRole ( 'link' , { name : 'table option link' } ) )
207+ it ( 'does nothing with ⌘ + click (default browser action: open in new tab)' , async ( ) => {
208+ const user = userEvent . setup ( )
209+ render (
210+ < TableOptionLinkWithSelectHandler
211+ tableName = "follows"
212+ columnName = { undefined }
213+ /> ,
214+ { wrapper } ,
215+ )
216+
217+ await user . keyboard ( '{Meta>}' )
218+ await user . click ( screen . getByRole ( 'link' , { name : 'table option link' } ) )
219+ await user . keyboard ( '{/Meta}' )
144220
145- expect ( mockSelectTable ) . toHaveBeenCalled ( )
146- expect ( mockSetCommandPaletteDialogOpen ) . toHaveBeenCalledWith ( false )
221+ expect ( mockSelectTable ) . not . toHaveBeenCalled ( )
222+ expect ( mockSetCommandPaletteDialogOpen ) . not . toHaveBeenCalled ( )
223+ } )
147224 } )
148225
149- it ( 'does nothing with ⌘ + click (default browser action: open in new tab)' , async ( ) => {
150- const user = userEvent . setup ( )
151- render ( < TableOptionLinkWithSelectHandler tableName = "follows" /> , {
152- wrapper,
226+ describe ( 'when passing both tableName and columnName' , ( ) => {
227+ it ( 'moves to clicked table in ERD and closes the dialog' , async ( ) => {
228+ const user = userEvent . setup ( )
229+ render (
230+ < TableOptionLinkWithSelectHandler
231+ tableName = "follows"
232+ columnName = "user_id"
233+ /> ,
234+ { wrapper } ,
235+ )
236+
237+ await user . click ( screen . getByRole ( 'link' , { name : 'table option link' } ) )
238+
239+ expect ( mockSelectTable ) . toHaveBeenCalled ( )
240+ expect ( mockSetCommandPaletteDialogOpen ) . toHaveBeenCalledWith ( false )
241+ expect ( window . location . hash ) . toBe ( '#follows__columns__user_id' )
153242 } )
154243
155- await user . keyboard ( '{Meta>}' )
156- await user . click ( screen . getByRole ( 'link' , { name : 'table option link' } ) )
157- await user . keyboard ( '{/Meta}' )
244+ it ( 'does nothing with ⌘ + click (default browser action: open in new tab)' , async ( ) => {
245+ const user = userEvent . setup ( )
246+ render (
247+ < TableOptionLinkWithSelectHandler
248+ tableName = "follows"
249+ columnName = "user_id"
250+ /> ,
251+ {
252+ wrapper,
253+ } ,
254+ )
255+
256+ await user . keyboard ( '{Meta>}' )
257+ await user . click ( screen . getByRole ( 'link' , { name : 'table option link' } ) )
258+ await user . keyboard ( '{/Meta}' )
158259
159- expect ( mockSelectTable ) . not . toHaveBeenCalled ( )
160- expect ( mockSetCommandPaletteDialogOpen ) . not . toHaveBeenCalled ( )
260+ expect ( mockSelectTable ) . not . toHaveBeenCalled ( )
261+ expect ( mockSetCommandPaletteDialogOpen ) . not . toHaveBeenCalled ( )
262+ expect ( window . location . hash ) . toBe ( '' )
263+ } )
161264 } )
162265} )
0 commit comments