Files
hnh-map/frontend-nuxt/composables/__tests__/useMapBookmarks.test.ts
Nikolay Tatarinov fd624c2357 Refactor frontend components for improved functionality and accessibility
- Consolidated global error handling in app.vue to redirect users to the login page on API authentication failure.
- Enhanced MapView component by reintroducing event listeners for selected map and marker updates, improving interactivity.
- Updated PasswordInput and various modal components to ensure proper input handling and accessibility compliance.
- Refactored MapControls and MapControlsContent to streamline prop management and enhance user experience.
- Improved error handling in local storage operations within useMapBookmarks and useRecentLocations composables.
- Standardized input elements across forms for consistency in user interaction.
2026-03-04 14:06:27 +03:00

80 lines
2.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { ref } from 'vue'
import { useMapBookmarks } from '../useMapBookmarks'
const stateByKey: Record<string, ReturnType<typeof ref>> = {}
const useStateMock = vi.fn((key: string, init: () => unknown) => {
if (!stateByKey[key]) {
stateByKey[key] = ref(init())
}
return stateByKey[key]
})
vi.stubGlobal('useState', useStateMock)
vi.stubGlobal('onMounted', vi.fn())
const storage: Record<string, string> = {}
const localStorageMock = {
getItem: vi.fn((key: string) => storage[key] ?? null),
setItem: vi.fn((key: string, value: string) => {
storage[key] = value
}),
clear: vi.fn(() => {
delete storage['hnh-map-bookmarks']
}),
}
vi.stubGlobal('localStorage', localStorageMock)
vi.stubGlobal('import.meta.server', false)
vi.stubGlobal('import.meta.client', true)
describe('useMapBookmarks', () => {
beforeEach(() => {
storage['hnh-map-bookmarks'] = '[]'
stateByKey['hnh-map-bookmarks-state'] = ref([])
localStorageMock.getItem.mockImplementation((key: string) => storage[key] ?? null)
localStorageMock.setItem.mockImplementation((key: string, value: string) => {
storage[key] = value
})
})
it('add adds a bookmark and refresh updates state', () => {
const { bookmarks, add, refresh } = useMapBookmarks()
refresh()
expect(bookmarks.value).toEqual([])
const id = add({ name: 'Home', mapId: 1, x: 0, y: 0 })
expect(id).toBeDefined()
expect(bookmarks.value).toHaveLength(1)
expect(bookmarks.value[0].name).toBe('Home')
expect(bookmarks.value[0].mapId).toBe(1)
expect(bookmarks.value[0].id).toBe(id)
})
it('update changes name', () => {
const { bookmarks, add, update, refresh } = useMapBookmarks()
refresh()
const id = add({ name: 'Old', mapId: 1, x: 0, y: 0 })
const ok = update(id, { name: 'New' })
expect(ok).toBe(true)
expect(bookmarks.value[0].name).toBe('New')
})
it('remove deletes bookmark', () => {
const { bookmarks, add, remove, refresh } = useMapBookmarks()
refresh()
const id = add({ name: 'X', mapId: 1, x: 0, y: 0 })
expect(bookmarks.value).toHaveLength(1)
remove(id)
expect(bookmarks.value).toHaveLength(0)
})
it('clear empties bookmarks', () => {
const { bookmarks, add, clear, refresh } = useMapBookmarks()
refresh()
add({ name: 'A', mapId: 1, x: 0, y: 0 })
expect(bookmarks.value).toHaveLength(1)
clear()
expect(bookmarks.value).toHaveLength(0)
})
})