Automerge backed Markdown editor for the Atmosphere
Something went wrong. Try again.
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225// src/plugin/__tests__/workspace-sidebar-collapsed.test.tsimport { describe, it, expect, vi, beforeEach } from 'vitest'import { Workspace } from '../workspace'import { ItemView } from '../views/item-view'import { ReamsEvents } from '../reams/events'import type { App } from '../app'
class TestView extends ItemView { getViewType() { return 'test-view' } getDisplayText() { return 'Test View' }}
/** * Create a minimal App mock for testing Reams events */function createAppMock(ws: Workspace): App { const reamsEvents = new ReamsEvents() const app = { workspace: ws, reamsEvents, } as unknown as App ws.app = app return app}
describe('Workspace sidebar collapsed state ownership', () => { let workspace: Workspace let app: App
beforeEach(() => { workspace = new Workspace(document.createElement('div')) app = createAppMock(workspace) workspace.registerViewType('test-view', (leaf) => new TestView(leaf)) })
describe('initial state', () => { it('leftSidebarCollapsed defaults to true', () => { expect(workspace.leftSidebarCollapsed).toBe(true) })
it('rightSidebarCollapsed defaults to true', () => { expect(workspace.rightSidebarCollapsed).toBe(true) }) })
describe('setLeftSidebarCollapsed', () => { it('updates leftSidebarCollapsed to false', () => { workspace.setLeftSidebarCollapsed(false) expect(workspace.leftSidebarCollapsed).toBe(false) })
it('updates leftSidebarCollapsed to true', () => { workspace.setLeftSidebarCollapsed(false) workspace.setLeftSidebarCollapsed(true) expect(workspace.leftSidebarCollapsed).toBe(true) })
it('fires left-sidebar-collapsed-change event with new collapsed state', () => { const listener = vi.fn() app.reamsEvents.on('left-sidebar-collapsed-change', listener) workspace.setLeftSidebarCollapsed(false) expect(listener).toHaveBeenCalledWith(false) workspace.setLeftSidebarCollapsed(true) expect(listener).toHaveBeenCalledWith(true) })
it('does not fire event when state is unchanged', () => { const listener = vi.fn() workspace.setLeftSidebarCollapsed(false) app.reamsEvents.on('left-sidebar-collapsed-change', listener) workspace.setLeftSidebarCollapsed(false) // same state expect(listener).not.toHaveBeenCalled() }) })
describe('setRightSidebarCollapsed', () => { it('updates rightSidebarCollapsed to false', () => { workspace.setRightSidebarCollapsed(false) expect(workspace.rightSidebarCollapsed).toBe(false) })
it('updates rightSidebarCollapsed to true', () => { workspace.setRightSidebarCollapsed(false) workspace.setRightSidebarCollapsed(true) expect(workspace.rightSidebarCollapsed).toBe(true) })
it('fires right-sidebar-collapsed-change event with new collapsed state', () => { const listener = vi.fn() app.reamsEvents.on('right-sidebar-collapsed-change', listener) workspace.setRightSidebarCollapsed(false) expect(listener).toHaveBeenCalledWith(false) workspace.setRightSidebarCollapsed(true) expect(listener).toHaveBeenCalledWith(true) })
it('does not fire event when state is unchanged', () => { const listener = vi.fn() workspace.setRightSidebarCollapsed(false) app.reamsEvents.on('right-sidebar-collapsed-change', listener) workspace.setRightSidebarCollapsed(false) // same state expect(listener).not.toHaveBeenCalled() }) })
describe('revealLeaf sets collapsed state', () => { it('sets leftSidebarCollapsed to false when revealing left panel leaf', async () => { const leaf = workspace.getLeftLeaf(true) await leaf.openView('test-view') expect(workspace.leftSidebarCollapsed).toBe(true) workspace.revealLeaf(leaf) expect(workspace.leftSidebarCollapsed).toBe(false) })
it('sets rightSidebarCollapsed to false when revealing right panel leaf', async () => { const leaf = workspace.getRightLeaf(true) await leaf.openView('test-view') expect(workspace.rightSidebarCollapsed).toBe(true) workspace.revealLeaf(leaf) expect(workspace.rightSidebarCollapsed).toBe(false) })
it('fires collapsed-change event when revealing collapsed panel leaf', async () => { const leaf = workspace.getRightLeaf(true) await leaf.openView('test-view') const listener = vi.fn() app.reamsEvents.on('right-sidebar-collapsed-change', listener) workspace.revealLeaf(leaf) expect(listener).toHaveBeenCalledWith(false) })
it('does not fire collapsed-change event when panel is already expanded', async () => { workspace.setRightSidebarCollapsed(false) const leaf = workspace.getRightLeaf(true) await leaf.openView('test-view') const listener = vi.fn() app.reamsEvents.on('right-sidebar-collapsed-change', listener) workspace.revealLeaf(leaf) expect(listener).not.toHaveBeenCalled() })
it('still fires reveal-panel for backward compatibility', async () => { const leaf = workspace.getRightLeaf(true) await leaf.openView('test-view') const listener = vi.fn() app.reamsEvents.on('reveal-panel', listener) workspace.revealLeaf(leaf) expect(listener).toHaveBeenCalledWith('right') }) })
describe('typed event overloads', () => { it('left-sidebar-collapsed-change has correct type signature', () => { const listener = vi.fn((collapsed: boolean) => { // Type assertion to verify signature void (collapsed as boolean) expect(typeof collapsed).toBe('boolean') }) app.reamsEvents.on('left-sidebar-collapsed-change', listener) workspace.setLeftSidebarCollapsed(false) expect(listener).toHaveBeenCalled() })
it('right-sidebar-collapsed-change has correct type signature', () => { const listener = vi.fn((collapsed: boolean) => { // Type assertion to verify signature void (collapsed as boolean) expect(typeof collapsed).toBe('boolean') }) app.reamsEvents.on('right-sidebar-collapsed-change', listener) workspace.setRightSidebarCollapsed(false) expect(listener).toHaveBeenCalled() }) })
describe('ensureSideLeaf integration', () => { it('expands panel when ensureSideLeaf is called with reveal=true', async () => { // ensureSideLeaf calls revealLeaf internally, which should expand the panel expect(workspace.rightSidebarCollapsed).toBe(true) // ensureSideLeaf with active:true (default) should reveal the leaf const leaf = await workspace.ensureSideLeaf('test-view', 'right', { active: true }) // Wait for the async setViewState to complete and trigger revealLeaf await new Promise(resolve => setTimeout(resolve, 10)) expect(workspace.rightSidebarCollapsed).toBe(false) expect(leaf).toBeDefined() }) })
describe('programmatic state changes', () => { it('allows external code to collapse sidebar', () => { // Expand first workspace.setLeftSidebarCollapsed(false) expect(workspace.leftSidebarCollapsed).toBe(false) // External code can collapse workspace.setLeftSidebarCollapsed(true) expect(workspace.leftSidebarCollapsed).toBe(true) })
it('allows external code to collapse right panel', () => { // Expand first workspace.setRightSidebarCollapsed(false) expect(workspace.rightSidebarCollapsed).toBe(false) // External code can collapse workspace.setRightSidebarCollapsed(true) expect(workspace.rightSidebarCollapsed).toBe(true) }) })})