Skip to content

Commit 81cf07b

Browse files
committed
test: run minimap lifecycle against unmocked vueuse timing
Drops the useRafFn mock (fake timers drive requestAnimationFrame too) and tightens the show assertion: the immediate repaint comes from the visibility watcher, so interval resumption is proven by a change made after showing being observed only once the next interval elapses.
1 parent 40829f7 commit 81cf07b

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

src/renderer/extensions/minimap/composables/useMinimap.intervalLifecycle.test.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ const mockCanvas = {
3838
setDirty: vi.fn()
3939
}
4040

41-
// Only the RAF loop is mocked (happy-dom provides no real frame source); the
42-
// interval under test and the throttle are the real implementations.
43-
vi.mock('@vueuse/core', async (importOriginal) => ({
44-
...((await importOriginal()) as Record<string, unknown>),
45-
useRafFn: vi.fn(() => ({ pause: vi.fn(), resume: vi.fn() }))
46-
}))
47-
4841
vi.mock('@/renderer/core/canvas/canvasStore', () => ({
4942
useCanvasStore: vi.fn(() => ({ canvas: mockCanvas }))
5043
}))
@@ -125,6 +118,8 @@ describe('useMinimap change-detection interval', () => {
125118
}
126119

127120
beforeEach(() => {
121+
// Fakes intervals and requestAnimationFrame alike, so the real vueuse
122+
// timing primitives run under test control with nothing mocked.
128123
vi.useFakeTimers()
129124
context = createMockCanvas2DContext()
130125
mockNodes[0].pos = [0, 0]
@@ -166,12 +161,21 @@ describe('useMinimap change-detection interval', () => {
166161
await vi.advanceTimersByTimeAsync(POLL_MS * 5)
167162
expect(drawCalls()).toBe(hidden)
168163

169-
// Show again: the pending change is only observed on an interval tick,
170-
// plus the explicit refresh the visibility watcher performs.
164+
// Show again: the visibility watcher repaints the stale state immediately.
171165
await minimap.toggle()
172166
await nextTick()
173-
await vi.advanceTimersByTimeAsync(POLL_MS + 10)
174-
expect(drawCalls()).toBeGreaterThan(hidden)
167+
await vi.advanceTimersByTimeAsync(0)
168+
const afterShow = drawCalls()
169+
expect(afterShow).toBeGreaterThan(hidden)
170+
171+
// A change made after showing is only observed by polling: nothing before
172+
// the interval elapses, a redraw after it does.
173+
moveNode()
174+
await vi.advanceTimersByTimeAsync(POLL_MS - 20)
175+
expect(drawCalls()).toBe(afterShow)
176+
177+
await vi.advanceTimersByTimeAsync(40)
178+
expect(drawCalls()).toBeGreaterThan(afterShow)
175179
})
176180

177181
it('stops polling after destroy', async () => {

0 commit comments

Comments
 (0)