Files
supabase/apps/studio/components/interfaces/QuerySources/LogsTimeRangeSubMenu.test.tsx
T
Saxon Fletcher 39c3960215 fix(studio): harden Explorer query persistence (#49039)
<img width="1690" height="1034" alt="image"
src="https://github.com/user-attachments/assets/2dc5b10b-8e64-413b-861e-8b0b62e2d880"
/>

## Summary

- debounce draft persistence and flush pending edits when the page exits
- validate untrusted local storage with Zod, recover from malformed
entries, and retain the 50 most recently updated drafts
- harden route restoration, recent-item routing, close confirmation, and
local cleanup
- disable execution while project or replica data is resolving and fail
closed for missing replicas
- use an HTTP-safe UUID generator for self-hosted Studio
- adopt the upstream Explorer toolbar title API
- expand component and state coverage for persistence and execution
behavior

## To test

1. Open Explorer, select **Run SQL**, then enter SQL and rename the
query.
2. Reload the page and confirm the draft is restored; close its tab and
confirm it is discarded after the prompt.

## Why

This layer makes local-only Explorer drafts resilient to rapid edits,
reloads, stale browser data, and tab lifecycle edge cases.

## Impact

Queries remain local-only, and closing their tabs discards them after
confirmation. Save-as-notebook functionality remains intentionally out
of scope.

## Validation

- fresh non-incremental Studio TypeScript check
- 68 Vitest tests pass across Explorer, query sources, tabs layout, and
query/tab state
- Studio ESLint ratchet and Prettier check both clean


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Explorer query drafts now save automatically and restore reliably
across navigation, tab closures, page exits, and visibility changes.
- Recent query items now open directly to their associated Explorer
query.
- Logs time-range selections are handled consistently, including custom
ranges and preset matching.

- **Bug Fixes**
  - Prevented stale query loading states when switching between queries.
  - Improved handling of invalid or outdated saved drafts.
  - Limited saved drafts to the 50 most recently updated queries.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-14 13:16:54 +07:00

74 lines
2.6 KiB
TypeScript

import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from 'ui'
import { describe, expect, it, vi, type Mock } from 'vitest'
import { LogsTimeRangeSubMenu } from './LogsTimeRangeSubMenu'
import type { LogTimeRange } from '@/data/query-sources/query-source-registry'
import { customRender } from '@/tests/lib/custom-render'
mockAnimationsApi()
vi.mock('@/hooks/misc/useCheckEntitlements', () => ({
useCheckEntitlements: () => ({ getEntitlementNumericValue: () => 1 }),
}))
const renderSubMenu = ({
onRangeChange = vi.fn<(range: LogTimeRange) => void>(),
onOpenCustomRange = vi.fn<() => void>(),
onShowUpgrade = vi.fn<() => void>(),
range = { type: 'relative', amount: 1, unit: 'hour' } as LogTimeRange,
}: {
onRangeChange?: Mock<(range: LogTimeRange) => void>
onOpenCustomRange?: Mock<() => void>
onShowUpgrade?: Mock<() => void>
range?: LogTimeRange
} = {}) => {
customRender(
<DropdownMenu defaultOpen>
<DropdownMenuTrigger>Open</DropdownMenuTrigger>
<DropdownMenuContent>
<LogsTimeRangeSubMenu
range={range}
onRangeChange={onRangeChange}
onOpenCustomRange={onOpenCustomRange}
onShowUpgrade={onShowUpgrade}
/>
</DropdownMenuContent>
</DropdownMenu>
)
return { onRangeChange, onOpenCustomRange, onShowUpgrade }
}
describe('LogsTimeRangeSubMenu', () => {
it('opens the upgrade prompt instead of applying a range beyond retention', async () => {
const { onRangeChange, onShowUpgrade } = renderSubMenu()
await userEvent.hover(await screen.findByText('Time range'))
await userEvent.click(await screen.findByText('Last 7 days'))
expect(onShowUpgrade).toHaveBeenCalledOnce()
expect(onRangeChange).not.toHaveBeenCalled()
})
it('exposes the custom-range action', async () => {
const { onOpenCustomRange } = renderSubMenu()
await userEvent.hover(await screen.findByText('Time range'))
await userEvent.click(await screen.findByText('Custom range…'))
expect(onOpenCustomRange).toHaveBeenCalledOnce()
})
it('marks the structurally matching preset as selected', async () => {
renderSubMenu({ range: { type: 'relative', amount: 3, unit: 'hour' } })
await userEvent.hover(await screen.findByText('Time range'))
await waitFor(() => expect(screen.getAllByText('Last 3 hours')).toHaveLength(2))
expect(document.querySelector('.lucide-check')).toBeInTheDocument()
})
})