cc-haha/desktop/src/components/browser/BrowserSurface.test.tsx
程序员阿江(Relakkes) 5d02f4c4e7 feat(desktop): subscribe preview bridge events into browser store
Routes preview://event (navigated/ready) from Rust into the browser store via a new subscribePreviewEvents subscriber; adds title field and setNavigated/setReady reducers to BrowserSessionState; BrowserSurface subscribes on mount to replace M1 optimistic nav-state.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 17:40:45 +08:00

47 lines
2.0 KiB
TypeScript

import '@testing-library/jest-dom'
import { fireEvent, render, screen } from '@testing-library/react'
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
beforeAll(() => {
globalThis.ResizeObserver = class { observe() {} unobserve() {} disconnect() {} } as unknown as typeof ResizeObserver
})
const { bridge } = vi.hoisted(() => ({
bridge: { open: vi.fn(), navigate: vi.fn(), setBounds: vi.fn(), setVisible: vi.fn(), close: vi.fn() },
}))
vi.mock('../../lib/previewBridge', () => ({ previewBridge: bridge }))
vi.mock('@tauri-apps/api/event', () => ({ listen: () => Promise.resolve(() => {}) }))
import { BrowserSurface } from './BrowserSurface'
import { useBrowserPanelStore } from '../../stores/browserPanelStore'
afterEach(() => {
Object.values(bridge).forEach((f) => f.mockReset())
useBrowserPanelStore.setState(useBrowserPanelStore.getInitialState(), true)
})
describe('BrowserSurface', () => {
it('opens the preview at the session url on mount when surface is open', () => {
useBrowserPanelStore.getState().open('s1', 'http://localhost:5173/')
render(<BrowserSurface sessionId="s1" />)
expect(bridge.open).toHaveBeenCalledWith('http://localhost:5173/', expect.objectContaining({ width: expect.any(Number) }))
})
it('navigating via address bar calls store + bridge', () => {
useBrowserPanelStore.getState().open('s1', 'http://localhost:5173/')
render(<BrowserSurface sessionId="s1" />)
const input = screen.getByRole('textbox')
fireEvent.change(input, { target: { value: 'http://localhost:3000/' } })
fireEvent.submit(input.closest('form')!)
expect(bridge.navigate).toHaveBeenCalledWith('http://localhost:3000/')
expect(useBrowserPanelStore.getState().bySession['s1']!.url).toBe('http://localhost:3000/')
})
it('hides the native webview on unmount', () => {
useBrowserPanelStore.getState().open('s1', 'http://localhost:5173/')
const { unmount } = render(<BrowserSurface sessionId="s1" />)
unmount()
expect(bridge.setVisible).toHaveBeenLastCalledWith(false)
})
})