mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-16 13:03:31 +08:00
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>
47 lines
2.0 KiB
TypeScript
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)
|
|
})
|
|
})
|