Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/essence/mmgisAPI/__mocks__/mmgisAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
//
// PanelManager_ imports mmgisAPI, which transitively pulls in the entire Map_
// rendering stack. The panel specs only exercise panel logic, so this stub
// stands in with a no-op event bus and keeps the import graph light.
export const mmgisAPI = { emit: () => {} }
// stands in with a real mitt event bus (matching the production API, which
// also backs emit/on with mitt) and keeps the import graph light. Using a real
// bus lets the panel specs subscribe with mmgisAPI.on(...) and observe the
// layout-changed notifications the manager emits.
import mitt from 'mitt'

const events = mitt()

export const mmgisAPI = {
emit: events.emit,
on: events.on,
off: events.off,
}
export const mmgisAPI_ = {}
1 change: 0 additions & 1 deletion tests/unit/DashboardConfigValidator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ test.describe('DashboardConfigValidator', () => {
const result = validateModernConfig(config);
expect(result.valid).toBe(false);
expect(result.errors).toContain('Tool[1] must have a string "name"');
expect(result.errors).toContain('Tool[1] must have a string "icon"');
});
});
});
8 changes: 4 additions & 4 deletions tests/unit/panelManager/panelManager.layout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { test, expect, vi } from 'vitest'
vi.mock('../../../src/essence/mmgisAPI/mmgisAPI')
import { PanelManager } from '../../../src/essence/Basics/PanelManager_/PanelManager_.ts'
import { PANEL_POSITION } from '../../../src/essence/Basics/PanelManager_/types/layout.ts'
import { createMockPanelConfig, mockWindowDispatchEvent, setupWindowEnvironment } from './testHelpers.js'
import { createMockPanelConfig, mockLayoutChangedEvents, setupWindowEnvironment } from './testHelpers.js'

test.describe('PanelManager - Layout', () => {
let panelManager
Expand Down Expand Up @@ -93,7 +93,7 @@ test.describe('PanelManager - Layout', () => {
})
panelManager.registerPanel(config)

const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()

panelManager.resizePanel('test-panel', 300)

Expand Down Expand Up @@ -132,7 +132,7 @@ test.describe('PanelManager - Layout', () => {

test.describe('notifyLayoutChanged', () => {
test('dispatches custom event with panel data', () => {
const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()

const config = createMockPanelConfig()
panelManager.registerPanel(config)
Expand All @@ -150,7 +150,7 @@ test.describe('PanelManager - Layout', () => {
})

test('includes all panels sorted by priority in event', () => {
const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()

const config1 = createMockPanelConfig({ id: 'panel-1', priority: 2 })
const config2 = createMockPanelConfig({ id: 'panel-2', priority: 0 })
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/panelManager/panelManager.registration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { test, expect, vi } from 'vitest'
vi.mock('../../../src/essence/mmgisAPI/mmgisAPI')
import { PanelManager } from '../../../src/essence/Basics/PanelManager_/PanelManager_.ts'
import { PANEL_STATE } from '../../../src/essence/Basics/PanelManager_/types/layout.ts'
import { createMockPanelConfig, mockWindowDispatchEvent, setupWindowEnvironment } from './testHelpers.js'
import { createMockPanelConfig, mockLayoutChangedEvents, setupWindowEnvironment } from './testHelpers.js'

test.describe('PanelManager - Registration', () => {
let panelManager
Expand Down Expand Up @@ -81,7 +81,7 @@ test.describe('PanelManager - Registration', () => {
})

test('triggers layout recalculation after registration', () => {
const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()
const config = createMockPanelConfig()

panelManager.registerPanel(config)
Expand Down Expand Up @@ -113,7 +113,7 @@ test.describe('PanelManager - Registration', () => {
const config = createMockPanelConfig()
panelManager.registerPanel(config)

const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()

panelManager.unregisterPanel('test-panel')

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/panelManager/panelManager.stateManagement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { test, expect, vi } from 'vitest'
vi.mock('../../../src/essence/mmgisAPI/mmgisAPI')
import { PanelManager } from '../../../src/essence/Basics/PanelManager_/PanelManager_.ts'
import { PANEL_STATE } from '../../../src/essence/Basics/PanelManager_/types/layout.ts'
import { createMockPanelConfig, createMockToolMetadata, mockWindowDispatchEvent, setupWindowEnvironment } from './testHelpers.js'
import { createMockPanelConfig, createMockToolMetadata, mockLayoutChangedEvents, setupWindowEnvironment } from './testHelpers.js'

test.describe('PanelManager - State Management', () => {
let panelManager
Expand Down Expand Up @@ -40,7 +40,7 @@ test.describe('PanelManager - State Management', () => {
})

test('triggers layout recalculation after state change', () => {
const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()

panelManager.setPanelState('test-panel', PANEL_STATE.COLLAPSED)

Expand Down Expand Up @@ -149,7 +149,7 @@ test.describe('PanelManager - State Management', () => {
})

test('triggers layout recalculation', () => {
const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()

panelManager.focusTool('test-panel', 'test-tool')

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/panelManager/panelManager.toolManagement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ vi.mock('../../../src/essence/mmgisAPI/mmgisAPI')
import { PanelManager } from '../../../src/essence/Basics/PanelManager_/PanelManager_.ts'
import { PANEL_STATE } from '../../../src/essence/Basics/PanelManager_/types/layout.ts'
import { TOOL_ORIENTATION } from '../../../src/essence/Basics/ToolController_/types/tool.ts'
import { createMockPanelConfig, createMockToolMetadata, mockWindowDispatchEvent, setupWindowEnvironment } from './testHelpers.js'
import { createMockPanelConfig, createMockToolMetadata, mockLayoutChangedEvents, setupWindowEnvironment } from './testHelpers.js'

test.describe('PanelManager - Tool Management', () => {
let panelManager
Expand Down Expand Up @@ -209,7 +209,7 @@ test.describe('PanelManager - Tool Management', () => {
const toolMetadata = createMockToolMetadata()
panelManager.addToolToPanel('test-panel', toolMetadata)

const mock = mockWindowDispatchEvent()
const mock = mockLayoutChangedEvents()

panelManager.removeToolFromPanel('test-panel', 'test-tool')

Expand Down
37 changes: 21 additions & 16 deletions tests/unit/panelManager/testHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { PANEL_POSITION, PANEL_STATE, PANEL_LAYOUT_TYPE } from '../../../src/essence/Basics/PanelManager_/types/layout.ts'
import { TOOL_ORIENTATION } from '../../../src/essence/Basics/ToolController_/types/tool.ts'
import { mmgisAPI } from '../../../src/essence/mmgisAPI/mmgisAPI'
import { onTestFinished } from 'vitest'

const LAYOUT_CHANGED_EVENT = 'mmgis-panel-layout-changed'

/**
* Create a basic mock panel configuration for testing
Expand Down Expand Up @@ -34,31 +38,32 @@ export function createMockToolMetadata(overrides = {}) {
}

/**
* Mock window.dispatchEvent for testing layout changes
* Capture panel layout-changed notifications fired over the in-app event bus.
*
* PanelManager_ broadcasts layout changes via mmgisAPI.emit (mitt), not a
* window event. This subscribes to that bus and collects each notification,
* normalizing them to the shape the panel specs assert on:
* { type: 'mmgis-panel-layout-changed', detail: { panels } }
*/
export function mockWindowDispatchEvent() {
export function mockLayoutChangedEvents() {
const events = []

// Ensure global window object exists
if (typeof global.window === 'undefined') {
global.window = {}
const handler = (payload) => {
events.push({
type: LAYOUT_CHANGED_EVENT,
detail: payload,
})
}

const originalDispatchEvent = global.window.dispatchEvent

global.window.dispatchEvent = (event) => {
events.push(event)
return true
}
mmgisAPI.on(LAYOUT_CHANGED_EVENT, handler)
// Auto-unsubscribe when the current test ends, so a forgotten restore()
// cannot leak this handler onto the shared singleton bus.
onTestFinished(() => mmgisAPI.off(LAYOUT_CHANGED_EVENT, handler))

return {
events,
restore: () => {
if (originalDispatchEvent) {
global.window.dispatchEvent = originalDispatchEvent
} else {
delete global.window.dispatchEvent
}
mmgisAPI.off(LAYOUT_CHANGED_EVENT, handler)
},
}
}
Expand Down