Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

2.5 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import {
  clampPosition,
  getNextPosition,
  getToolbarStyle,
  isMenuContent,
  shouldShowMenuButton,
} from './FloatingMobileToolbar.utils'

describe('isMenuContent', () => {
  it('returns false for null', () => {
    expect(isMenuContent(null)).toBe(false)
  })

  it('returns false for sidebar id string', () => {
    expect(isMenuContent('help-panel')).toBe(false)
  })

  it('returns true for non-string content', () => {
    expect(isMenuContent({})).toBe(true)
    expect(isMenuContent(0)).toBe(true)
  })
})

describe('shouldShowMenuButton', () => {
  it('returns true for project, org, account paths', () => {
    expect(shouldShowMenuButton('/project/ref')).toBe(true)
    expect(shouldShowMenuButton('/org/slug')).toBe(true)
    expect(shouldShowMenuButton('/account/me')).toBe(true)
  })

  it('returns false for other paths', () => {
    expect(shouldShowMenuButton('/organizations')).toBe(false)
    expect(shouldShowMenuButton('/')).toBe(false)
  })
})

describe('clampPosition', () => {
  const viewport = { width: 400, height: 800 }
  const navSize = { width: 100, height: 48 }

  it('clamps to viewport bounds', () => {
    const next = clampPosition({ x: 0, y: 0 }, { dx: 500, dy: 900 }, viewport, navSize)
    expect(next.x).toBe(300)
    expect(next.y).toBe(752)
  })

  it('does not go negative', () => {
    const next = clampPosition({ x: 10, y: 10 }, { dx: -20, dy: -20 }, viewport, navSize)
    expect(next.x).toBe(0)
    expect(next.y).toBe(0)
  })
})

describe('getNextPosition', () => {
  const viewport = { width: 400, height: 800 }
  const navSize = { width: 100, height: 48 }
  const dragStart = { x: 50, y: 100, startX: 60, startY: 110 }

  it('returns null when under threshold', () => {
    expect(getNextPosition(dragStart, 62, 111, viewport, navSize, 8)).toBeNull()
  })

  it('returns clamped position when over threshold', () => {
    const next = getNextPosition(dragStart, 80, 130, viewport, navSize, 5)
    expect(next).toEqual({ x: 70, y: 120 })
  })
})

describe('getToolbarStyle', () => {
  const viewport = { width: 390, height: 844 }
  const navSize = { width: 200, height: 48 }

  it('centers the toolbar by default and raises it above an open sheet', () => {
    const base = { position: null, navSize, viewport, isDragging: false }
    const closed = getToolbarStyle({ ...base, isSheetOpen: false })
    const open = getToolbarStyle({ ...base, isSheetOpen: true })

    expect(closed.left).toBe('50%')
    expect(Number(open.zIndex)).toBeGreaterThan(Number(closed.zIndex))
  })
})