EditorBaseLayout.test.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { render, screen } from '@testing-library/react'
  2. import type { ReactNode } from 'react'
  3. import { beforeEach, describe, expect, it, vi } from 'vitest'
  4. import { EditorBaseLayout } from './EditorBaseLayout'
  5. const { mockTabsState, mockEditorType } = vi.hoisted(() => ({
  6. mockTabsState: {
  7. activeTab: 'r-1',
  8. openTabs: ['r-1'],
  9. tabsMap: {
  10. 'r-1': {
  11. id: 'r-1',
  12. type: 'r',
  13. label: 'routines',
  14. metadata: {
  15. name: 'tasks',
  16. schema: 'public',
  17. tableId: 1,
  18. },
  19. },
  20. },
  21. } as any,
  22. mockEditorType: vi.fn(),
  23. }))
  24. vi.mock('common', () => ({
  25. useParams: () => ({ ref: 'default' }),
  26. }))
  27. vi.mock('next/navigation', () => ({
  28. usePathname: () => '/project/default/editor/1',
  29. }))
  30. vi.mock('@/state/tabs', () => ({
  31. useTabsStateSnapshot: () => mockTabsState,
  32. }))
  33. vi.mock('ui', () => ({
  34. cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),
  35. }))
  36. vi.mock('../ProjectLayout', () => ({
  37. ProjectLayoutWithAuth: ({
  38. browserTitle,
  39. children,
  40. }: {
  41. browserTitle?: Record<string, string>
  42. children: ReactNode
  43. }) => (
  44. <div>
  45. <div data-testid="browser-title">{JSON.stringify(browserTitle ?? {})}</div>
  46. {children}
  47. </div>
  48. ),
  49. }))
  50. vi.mock('../Tabs/CollapseButton', () => ({
  51. CollapseButton: () => null,
  52. }))
  53. vi.mock('../Tabs/Tabs', () => ({
  54. EditorTabs: () => null,
  55. }))
  56. vi.mock('./EditorsLayout.hooks', () => ({
  57. useEditorType: () => mockEditorType(),
  58. }))
  59. describe('EditorBaseLayout browser title', () => {
  60. beforeEach(() => {
  61. mockEditorType.mockReturnValue('table')
  62. mockTabsState.activeTab = 'r-1'
  63. mockTabsState.openTabs = ['r-1']
  64. mockTabsState.tabsMap = {
  65. 'r-1': {
  66. id: 'r-1',
  67. type: 'r',
  68. label: 'routines',
  69. metadata: {
  70. name: 'tasks',
  71. schema: 'public',
  72. tableId: 1,
  73. },
  74. },
  75. }
  76. })
  77. it('prefers the live tab label over stale metadata when composing the title entity', () => {
  78. render(
  79. <EditorBaseLayout title="Tables" product="Table Editor">
  80. <div>Page content</div>
  81. </EditorBaseLayout>
  82. )
  83. expect(JSON.parse(screen.getByTestId('browser-title').textContent ?? '{}')).toEqual({
  84. section: 'Tables',
  85. entity: 'routines',
  86. })
  87. })
  88. it('falls back to metadata when a tab does not have a label yet', () => {
  89. mockTabsState.tabsMap['r-1'].label = undefined
  90. render(
  91. <EditorBaseLayout title="Tables" product="Table Editor">
  92. <div>Page content</div>
  93. </EditorBaseLayout>
  94. )
  95. expect(JSON.parse(screen.getByTestId('browser-title').textContent ?? '{}')).toEqual({
  96. section: 'Tables',
  97. entity: 'tasks',
  98. })
  99. })
  100. })