Files
schemas-app/app/components/__tests__/OrganizationSwitcher.spec.ts
T
argoyleandClaude Opus 5 83576657f4
schemas-app / build (push) Skipped
schemas-app / build (pull_request) Successful in 2m41s
schemas-app / deploy-prod (pull_request) Skipped
test: unblock vitest 5 (localStorage accessor, hoisted vi.mock)
Two independent breakages from the vitest 4.1.11 -> 5.0.0 bump:

1. vitest 5 no longer shadows the jsdom window globals with writable ones, so
   `global.localStorage = ...` hits jsdom's getter-only accessor and throws
   "Cannot set property localStorage of [object Window] which has only a
   getter". test/setup.ts is a setup file, so both test files failed to
   collect. Object.defineProperty replaces the accessor outright.

2. vitest 5 rejects a vi.mock() call outside module top level instead of
   silently hoisting it. The call sat inside beforeEach, which was already
   misleading -- it is hoisted above the imports and its factory runs once, on
   first import of the mocked module, so it was never re-applied per test.
   Moved to top level, where it actually executes; the beforeEach keeps its
   resetModules/mockClear.

Verified against both vitest 4.1.11 and 5.0.0: 2 files / 9 tests pass on each.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T2BPPeWMdYEmYbN6eGCBSg
2026-09-10 21:59:07 +02:00

50 lines
1.7 KiB
TypeScript

import { mount } from '@vue/test-utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ref } from 'vue'
const mockSelectOrganization = vi.fn()
const mockOrganizations = [
{ id: '1', name: 'Org 1', apiKeys: [{ name: 'key1' }, { name: 'key2' }] },
{ id: '2', name: 'Org 2', apiKeys: [{ name: 'key3' }] },
{ id: '3', name: 'Org 3', apiKeys: [] },
]
// TODO: Add more comprehensive component tests once vitest module mocking issues are resolved
// For now, the component is indirectly tested through useOrganizationSelector tests
// and manual testing shows it works correctly
// vi.mock is hoisted above the imports and the factory runs once, on the first
// import of the mocked module -- it was never re-applied per test despite sitting
// in beforeEach. vitest 5 rejects the nested call outright rather than silently
// hoisting it, so declare it where it actually executes.
vi.mock('~/composables/useOrganizationSelector', () => ({
useOrganizationSelector: () => ({
organizations: ref([mockOrganizations[0]]),
selectedOrganization: ref(mockOrganizations[0]),
selectedOrgId: ref('1'),
selectOrganization: mockSelectOrganization,
}),
}))
describe('OrganizationSwitcher - Single Organization', () => {
beforeEach(() => {
vi.resetModules()
mockSelectOrganization.mockClear()
})
it('should show a chip for single organization', async () => {
const module = await import('../OrganizationSwitcher.vue')
const wrapper = mount(module.default)
const html = wrapper.html()
// Should show chip
expect(html).toContain('v-chip')
expect(html).toContain('Org 1')
// Menu should not exist
expect(html).not.toContain('v-menu')
})
})