ProductMenuShortcuts.test.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { render } from '@testing-library/react'
  2. import { beforeEach, describe, expect, it, vi } from 'vitest'
  3. import type { ProductMenuGroup } from './ProductMenu.types'
  4. import { ProductMenuShortcuts } from './ProductMenuShortcuts'
  5. import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
  6. const { mockPush, mockUseShortcut } = vi.hoisted(() => ({
  7. mockPush: vi.fn(),
  8. mockUseShortcut: vi.fn(),
  9. }))
  10. vi.mock('next/router', () => ({
  11. useRouter: () => ({
  12. push: mockPush,
  13. }),
  14. }))
  15. vi.mock('@/state/shortcuts/useShortcut', () => ({
  16. useShortcut: mockUseShortcut,
  17. }))
  18. const menu: ProductMenuGroup[] = [
  19. {
  20. title: 'Visible',
  21. items: [
  22. {
  23. name: 'Tables',
  24. key: 'tables',
  25. url: '/project/ref/database/tables',
  26. shortcutId: SHORTCUT_IDS.NAV_DATABASE_TABLES,
  27. },
  28. {
  29. name: 'Functions',
  30. key: 'functions',
  31. url: '/project/ref/database/functions',
  32. shortcutId: SHORTCUT_IDS.NAV_DATABASE_FUNCTIONS,
  33. disabled: true,
  34. },
  35. {
  36. name: 'External',
  37. key: 'external',
  38. url: 'https://example.com',
  39. shortcutId: SHORTCUT_IDS.NAV_DATABASE_EXTENSIONS,
  40. isExternal: true,
  41. },
  42. {
  43. name: 'No shortcut',
  44. key: 'no-shortcut',
  45. url: '/project/ref/database/triggers',
  46. },
  47. ],
  48. },
  49. ]
  50. describe('ProductMenuShortcuts', () => {
  51. beforeEach(() => {
  52. vi.clearAllMocks()
  53. })
  54. it('registers visible enabled internal shortcut items', () => {
  55. render(<ProductMenuShortcuts menu={menu} />)
  56. expect(mockUseShortcut).toHaveBeenCalledTimes(1)
  57. expect(mockUseShortcut).toHaveBeenCalledWith(
  58. SHORTCUT_IDS.NAV_DATABASE_TABLES,
  59. expect.any(Function)
  60. )
  61. })
  62. it('navigates to the item url when the shortcut fires', () => {
  63. render(<ProductMenuShortcuts menu={menu} />)
  64. const callback = mockUseShortcut.mock.calls[0][1]
  65. callback()
  66. expect(mockPush).toHaveBeenCalledWith('/project/ref/database/tables')
  67. })
  68. it('registers shortcut items nested under childItems', () => {
  69. render(
  70. <ProductMenuShortcuts
  71. menu={[
  72. {
  73. title: 'Nested',
  74. items: [
  75. {
  76. name: 'Parent',
  77. key: 'parent',
  78. url: '/project/ref/database',
  79. childItems: [
  80. {
  81. name: 'Indexes',
  82. key: 'indexes',
  83. url: '/project/ref/database/indexes',
  84. shortcutId: SHORTCUT_IDS.NAV_DATABASE_INDEXES,
  85. },
  86. ],
  87. },
  88. ],
  89. },
  90. ]}
  91. />
  92. )
  93. expect(mockUseShortcut).toHaveBeenCalledWith(
  94. SHORTCUT_IDS.NAV_DATABASE_INDEXES,
  95. expect.any(Function)
  96. )
  97. })
  98. })