Skip to content
Merged
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
47 changes: 46 additions & 1 deletion ui/web/tests/unit/skins/modern/SimulatorBar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,49 @@
* @description Server switcher, simulator state display, action buttons.
*/
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { SKIN_IDS, THEME_IDS } from 'ui-common'
import { afterEach, describe, expect, it, vi } from 'vitest'

import SimulatorBar from '@/skins/modern/components/SimulatorBar.vue'

import { createUIServerConfig } from '../../constants.js'

// Mock useTheme/useSkin to avoid post-teardown console.warn from
// validateTokenContract() (scheduled via requestAnimationFrame) and from
// the floating switchSkin() Promise. Vitest 4.x rejects pending RPC at
// teardown ("Closing rpc while \"onUserConsoleLog\" was pending"), and
// jsdom does not resolve --color-* CSS variables so the contract check
// would log ~24 warnings per <select> change.
const switchThemeMock = vi.fn()
const switchSkinMock = vi.fn().mockResolvedValue(true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Mock-level state is not reset between tests, so switchThemeMock and switchSkinMock call counts and recorded arguments accumulate across the entire test suite run. If any test triggers these mocks, later assertions using toHaveBeenCalledWith become unreliable.

Should add a beforeEach to clear both mocks:

beforeEach(() => {
  switchThemeMock.mockClear()
  switchSkinMock.mockClear()
})

Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed: vitest.config.ts already sets clearMocks: true (line 13) and restoreMocks: true (line 36), which run before each test and reset .mock.calls / .mock.results. An explicit beforeEach(mockClear) would be redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated assessment: while vitest.config.ts already sets clearMocks: true and restoreMocks: true globally (so the suggested beforeEach(mockClear) is technically redundant), the dominant convention in this test suite (Dialogs, StationCard, ConnectorRow, ClassicLayout, Actions, ...) is to also add an explicit afterEach(() => { vi.clearAllMocks() }) for visibility. Aligned with that convention in 2293a7d.


vi.mock('@/shared/composables/useTheme.js', async importOriginal => {
const { readonly, ref } = await import('vue')
return {
...(await importOriginal<Record<string, unknown>>()),
useTheme: () => ({
activeThemeId: readonly(ref(THEME_IDS[0])),
availableThemes: THEME_IDS,
lastError: readonly(ref(null)),
switchTheme: switchThemeMock,
}),
}
})

vi.mock('@/shared/composables/useSkin.js', async importOriginal => {
const { readonly, ref } = await import('vue')
return {
...(await importOriginal<Record<string, unknown>>()),
useSkin: () => ({
activeSkinId: readonly(ref<(typeof SKIN_IDS)[number]>('modern')),
availableSkins: SKIN_IDS.map(id => ({ id, label: id })),
isSwitching: readonly(ref(false)),
lastError: readonly(ref(null)),
switchSkin: switchSkinMock,
}),
}
})

const baseServer = createUIServerConfig({ name: 'Alpha' })
const altServer = createUIServerConfig({ host: 'beta', name: 'Beta' })

Expand All @@ -28,6 +65,10 @@ function mountBar (props: Record<string, unknown> = {}) {
}

describe('SimulatorBar', () => {
afterEach(() => {
vi.clearAllMocks()
})

it('should show Disconnected pill when simulatorState is undefined', () => {
const wrapper = mountBar()
expect(wrapper.text()).toContain('Disconnected')
Expand Down Expand Up @@ -122,14 +163,18 @@ describe('SimulatorBar', () => {
const wrapper = mountBar()
const themeSelect = wrapper.find('.modern-bar__select[aria-label="Theme"]')
expect(themeSelect.exists()).toBe(true)
await themeSelect.setValue('dracula')
await themeSelect.trigger('change')
expect(switchThemeMock).toHaveBeenCalledWith('dracula')
})

it('should call switchSkin when skin select changes', async () => {
const wrapper = mountBar()
const skinSelect = wrapper.find('.modern-bar__select[aria-label="Skin"]')
expect(skinSelect.exists()).toBe(true)
await skinSelect.setValue('classic')
await skinSelect.trigger('change')
expect(switchSkinMock).toHaveBeenCalledWith('classic')
})

it('should emit switch-server with selectedIndex when server select changes via trigger', async () => {
Expand Down
Loading