Files
frontend-miniapp/tests/unit/FloorPoiLoader.spec.ts
lyf 9ea1bfab71
Some checks failed
CI / verify (push) Has been cancelled
同步 sgs-frontend-mobile 源码
2026-07-27 11:26:01 +08:00

88 lines
2.9 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import type { GuideModelSource, GuideRenderPoi } from '@/domain/guideModel'
import { loadFloorPoisForInitialRender } from '@/components/map/floorPoiLoader'
const createPoi = (id: string): GuideRenderPoi => ({
id,
name: id,
floorId: 'floor-1',
primaryCategory: 'exhibition_hall',
primaryCategoryZh: '展厅',
iconType: 'hall'
})
describe('loadFloorPoisForInitialRender', () => {
it('keeps the model source instance context when using the fast loader', async () => {
const fastPois = [createPoi('fast-poi')]
const modelSource = {
fastPois,
loadPackage: vi.fn(),
loadFloorPois: vi.fn(),
async loadFloorPoisFast(floorId: string) {
expect(this).toBe(modelSource)
expect(floorId).toBe('floor-1')
return this.fastPois
}
} satisfies GuideModelSource & { fastPois: GuideRenderPoi[] }
const result = await loadFloorPoisForInitialRender(modelSource, 'floor-1')
expect(result).toEqual({
floorPois: fastPois,
dataTier: 'fast'
})
expect(modelSource.loadFloorPois).not.toHaveBeenCalled()
})
it('falls back to the full loader and marks the result as full when fast loading fails', async () => {
const fastError = new Error('fast loader unavailable')
const fullPois = [createPoi('full-poi')]
const modelSource = {
loadPackage: vi.fn(),
loadFloorPoisFast: vi.fn().mockRejectedValue(fastError),
loadFloorPois: vi.fn().mockResolvedValue(fullPois)
} satisfies GuideModelSource
const result = await loadFloorPoisForInitialRender(modelSource, 'floor-1')
expect(modelSource.loadFloorPoisFast).toHaveBeenCalledWith('floor-1')
expect(modelSource.loadFloorPois).toHaveBeenCalledWith('floor-1')
expect(result).toEqual({
floorPois: fullPois,
dataTier: 'full',
fastLoadError: fastError
})
})
it('returns fast data without invoking the full loader when fast loading succeeds', async () => {
const fastPois = [createPoi('fast-poi')]
const modelSource = {
loadPackage: vi.fn(),
loadFloorPoisFast: vi.fn().mockResolvedValue(fastPois),
loadFloorPois: vi.fn()
} satisfies GuideModelSource
const result = await loadFloorPoisForInitialRender(modelSource, 'floor-1')
expect(result.dataTier).toBe('fast')
expect(result.floorPois).toBe(fastPois)
expect(modelSource.loadFloorPois).not.toHaveBeenCalled()
})
it('uses and marks the full path when no fast loader is available', async () => {
const fullPois = [createPoi('full-poi')]
const modelSource = {
loadPackage: vi.fn(),
loadFloorPois: vi.fn().mockResolvedValue(fullPois)
} satisfies GuideModelSource
const result = await loadFloorPoisForInitialRender(modelSource, 'floor-1')
expect(modelSource.loadFloorPois).toHaveBeenCalledWith('floor-1')
expect(result).toEqual({
floorPois: fullPois,
dataTier: 'full'
})
})
})