Skip to content

Commit 58607fb

Browse files
committed
test: use fake timers for watch params test
1 parent 8b75f13 commit 58607fb

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

test/nuxt/use-async-data.test.ts

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -905,58 +905,63 @@ describe('useAsyncData', () => {
905905

906906
// https://github.com/nuxt/nuxt/issues/33777
907907
it('should continue watching params after reactive key changes', async () => {
908-
const id = ref('1')
909-
const page = ref(0)
910-
const promiseFn = vi.fn((id: string, page: number) => Promise.resolve(`id: ${id}, page: ${page}`))
911-
912-
const params = computed(() => ({ id: id.value, page: page.value }))
913-
914-
const { data, error } = await useAsyncData(
915-
() => `data-${id.value}`,
916-
() => promiseFn(params.value.id, params.value.page),
917-
{
918-
watch: [params],
919-
immediate: true,
920-
},
921-
)
922-
923-
// Initial call
924-
expect(data.value).toBe('id: 1, page: 0')
925-
expect(promiseFn).toHaveBeenCalledTimes(1)
926-
expect(promiseFn).toHaveBeenLastCalledWith('1', 0)
927-
928-
// Change key: id changes from '1' to '2'
929-
id.value = '2'
930-
await nextTick()
931-
await flushPromises()
932-
await new Promise(resolve => setTimeout(resolve, 5))
933-
934-
expect(promiseFn).toHaveBeenCalledTimes(2)
935-
expect(promiseFn).toHaveBeenLastCalledWith('2', 0)
936-
expect(error.value).toBe(undefined)
937-
expect(data.value).toBe('id: 2, page: 0')
938-
939-
// Verify params watcher continues to work after key change (issue #33777)
940-
page.value = 1
941-
await nextTick()
942-
await flushPromises()
943-
await new Promise(resolve => setTimeout(resolve, 5))
944-
945-
expect(promiseFn).toHaveBeenCalledTimes(3)
946-
expect(promiseFn).toHaveBeenLastCalledWith('2', 1)
947-
expect(error.value).toBe(undefined)
948-
expect(data.value).toBe('id: 2, page: 1')
908+
vi.useFakeTimers()
909+
try {
910+
const id = ref('1')
911+
const page = ref(0)
912+
const promiseFn = vi.fn((id: string, page: number) => Promise.resolve(`id: ${id}, page: ${page}`))
949913

950-
// Another params change to be thorough
951-
page.value = 2
952-
await nextTick()
953-
await flushPromises()
954-
await new Promise(resolve => setTimeout(resolve, 5))
914+
const params = computed(() => ({ id: id.value, page: page.value }))
955915

956-
expect(promiseFn).toHaveBeenCalledTimes(4)
957-
expect(promiseFn).toHaveBeenLastCalledWith('2', 2)
958-
expect(error.value).toBe(undefined)
959-
expect(data.value).toBe('id: 2, page: 2')
916+
const { data, error } = await useAsyncData(
917+
() => `data-${id.value}`,
918+
() => promiseFn(params.value.id, params.value.page),
919+
{
920+
watch: [params],
921+
immediate: true,
922+
},
923+
)
924+
925+
// Initial call
926+
expect(data.value).toBe('id: 1, page: 0')
927+
expect(promiseFn).toHaveBeenCalledTimes(1)
928+
expect(promiseFn).toHaveBeenLastCalledWith('1', 0)
929+
930+
// Change key: id changes from '1' to '2'
931+
id.value = '2'
932+
await nextTick()
933+
await flushPromises()
934+
await vi.advanceTimersByTimeAsync(5)
935+
936+
expect(promiseFn).toHaveBeenCalledTimes(2)
937+
expect(promiseFn).toHaveBeenLastCalledWith('2', 0)
938+
expect(error.value).toBe(undefined)
939+
expect(data.value).toBe('id: 2, page: 0')
940+
941+
// Verify params watcher continues to work after key change (issue #33777)
942+
page.value = 1
943+
await nextTick()
944+
await flushPromises()
945+
await vi.advanceTimersByTimeAsync(5)
946+
947+
expect(promiseFn).toHaveBeenCalledTimes(3)
948+
expect(promiseFn).toHaveBeenLastCalledWith('2', 1)
949+
expect(error.value).toBe(undefined)
950+
expect(data.value).toBe('id: 2, page: 1')
951+
952+
// Another params change to be thorough
953+
page.value = 2
954+
await nextTick()
955+
await flushPromises()
956+
await vi.advanceTimersByTimeAsync(5)
957+
958+
expect(promiseFn).toHaveBeenCalledTimes(4)
959+
expect(promiseFn).toHaveBeenLastCalledWith('2', 2)
960+
expect(error.value).toBe(undefined)
961+
expect(data.value).toBe('id: 2, page: 2')
962+
} finally {
963+
vi.useRealTimers()
964+
}
960965
})
961966

962967
it('should trigger AbortController on clear', () => {

0 commit comments

Comments
 (0)