test(editor): cover the three no-prompt switch branches

This commit is contained in:
unsc9 committed 2026-10-29 09:10:44 +08:00
1 parent 50ecb43c54
commit ae71cd05e6
1 file changed
+105 -16
+105 -16
View File
@@ -338,6 +338,25 @@ function typeAndCommit(input: HTMLInputElement, value: string): void {
})
}
/** Register a viewer that reports an unsaved draft on mount; its report
* feeds the editor-dirty registry the switch/close guards read. */
function registerDirtyViewer(service: ReturnType<typeof createBetterSidebarService>): void {
const DirtyViewer = (viewerProps: FileViewerProps): ReactNode => {
useEffect(() => {
viewerProps.onToolbarState?.({ modes: true, mode: 'edit', dirty: true, editable: true, saveState: 'idle' })
// Mount-only: this instance's draft is the report above.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return null
}
service.registerFileViewer({
id: 'test:dirty',
exts: ['fake'],
fetchStrategy: 'none',
component: DirtyViewer,
})
}
describe('EditorHost (files window)', () => {
it('a path-less tab renders the empty-state hint with the tree panel open', () => {
const { store, ctx, homeTab } = setup()
@@ -411,22 +430,7 @@ describe('EditorHost (files window)', () => {
it('in-place mode: switching files confirms before dropping an unsaved draft', () => {
const { store, ctx } = setup()
const service = ctx.betterSidebar
// A viewer holding a draft: its toolbar report feeds the host's dirty
// registry, which is what the switch guard reads.
const DirtyViewer = (viewerProps: FileViewerProps): ReactNode => {
useEffect(() => {
viewerProps.onToolbarState?.({ modes: true, mode: 'edit', dirty: true, editable: true, saveState: 'idle' })
// Mount-only: this instance's draft is the report above.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return null
}
service.registerFileViewer({
id: 'test:dirty',
exts: ['fake'],
fetchStrategy: 'none',
component: DirtyViewer,
})
registerDirtyViewer(service)
const tabId = 'editor:/tmp/a.fake'
service.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: tabId })
const fileTab = (): SidebarTab =>
@@ -453,6 +457,91 @@ describe('EditorHost (files window)', () => {
}
})
it('in-place mode: re-opening the same path never asks to discard the draft', () => {
const { store, ctx } = setup()
registerDirtyViewer(ctx.betterSidebar)
const tabId = 'editor:/tmp/a.fake'
ctx.betterSidebar.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: tabId })
const fileTab = (): SidebarTab =>
allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
.find(tab => tab.id === tabId)!
store.setPrefs({ ...store.getPrefs(), editorExplorer: true })
const { container, unmount } = mountHost(ctx, store, fileTab)
const confirm = vi.spyOn(window, 'confirm')
try {
typeAndCommit(container.querySelector('input')!, '/tmp/a.fake')
// The same path is not a switch: no prompt, same tab, same file.
expect(confirm).not.toHaveBeenCalled()
expect(fileTab().id).toBe(tabId)
expect(fileTab().path).toBe('/tmp/a.fake')
} finally {
confirm.mockRestore()
unmount()
}
})
it('in-place mode: a file another native type claims never asks about the draft', () => {
const { store, ctx } = setup()
const service = ctx.betterSidebar
registerDirtyViewer(service)
const resources: string[] = []
const claimedCtx = {
betterSidebar: service,
get: (name: string) => {
if (name === 'betterSidebar') return service
if (name === 'sidebarRightTabs') {
return { candidates: (address: string) => address.endsWith('.drawio') ? [{ kind: 'drawio' }] : [{ kind: 'editor' }] }
}
if (name === 'sidebarRight') {
return { openResource: (address: string) => { resources.push(address) } }
}
return undefined
},
sessions: ctx.sessions,
} as unknown as Context
service.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: 'editor:/tmp/a.fake', meta: { treeOpen: false } })
const fileTab = (): SidebarTab =>
allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
.find(tab => tab.path === '/tmp/a.fake')!
store.setPrefs({ ...store.getPrefs(), editorExplorer: true })
const { container, unmount } = mountHost(claimedCtx, store, fileTab)
const confirm = vi.spyOn(window, 'confirm')
try {
typeAndCommit(container.querySelector('input[placeholder^="File path"]')!, '/tmp/board.drawio')
// The claiming type opens the file elsewhere; this tab keeps its draft,
// so there is nothing to confirm.
expect(confirm).not.toHaveBeenCalled()
expect(resources).toHaveLength(1)
expect(fileTab().path).toBe('/tmp/a.fake')
} finally {
confirm.mockRestore()
unmount()
}
})
it('split mode: opening another file never asks about the source draft', () => {
const { store, ctx } = setup()
registerDirtyViewer(ctx.betterSidebar)
ctx.betterSidebar.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: 'editor:/tmp/a.fake' })
const fileTab = (): SidebarTab =>
allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
.find(tab => tab.path === '/tmp/a.fake')!
store.setPrefs({ ...store.getPrefs(), editorExplorer: false })
const { container, unmount } = mountHost(ctx, store, fileTab)
const confirm = vi.spyOn(window, 'confirm')
try {
typeAndCommit(container.querySelector('input[placeholder^="File path"]')!, '/tmp/b.fake')
const tabs = allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
// Split mode opens a per-path tab; the source tab and its draft stay put.
expect(confirm).not.toHaveBeenCalled()
expect(fileTab().path).toBe('/tmp/a.fake')
expect(tabs.some(tab => tab.path === '/tmp/b.fake')).toBe(true)
} finally {
confirm.mockRestore()
unmount()
}
})
it('in-place mode: a file another native type claims opens THAT type\'s tab, not the editor (#695)', () => {
const { store, ctx } = setup()
const service = ctx.betterSidebar