From 00ed78eceda3e1b8df77876b6f368ec8ee4b236a Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 15 Apr 2024 15:40:05 -0700 Subject: [PATCH] test(dav): Add test for request cancelation Signed-off-by: Christopher Ng --- lib/composables/dav.spec.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/composables/dav.spec.ts b/lib/composables/dav.spec.ts index 8be7af86..ec9f9c2e 100644 --- a/lib/composables/dav.spec.ts +++ b/lib/composables/dav.spec.ts @@ -23,7 +23,7 @@ import type { Ref } from 'vue' import { describe, it, expect, vi, afterEach } from 'vitest' import { shallowMount } from '@vue/test-utils' -import { defineComponent, ref, toRef } from 'vue' +import { defineComponent, ref, toRef, nextTick } from 'vue' import { useDAVFiles } from './dav' const nextcloudFiles = vi.hoisted(() => ({ @@ -228,4 +228,33 @@ describe('dav composable', () => { await waitRefLoaded(isLoading) expect(nextcloudFiles.getFavoriteNodes).toBeCalled() }) + + it('request cancelation works', async () => { + const client = { + stat: vi.fn((v) => ({ data: { path: v } })), + getDirectoryContents: vi.fn((p, o) => ({ data: [] })), + search: vi.fn((p, o) => ({ data: { results: [], truncated: false } })), + } + nextcloudFiles.davGetClient.mockImplementationOnce(() => client) + nextcloudFiles.davResultToNode.mockImplementationOnce((v) => v) + + const view = ref<'files' | 'recent' | 'favorites'>('files') + const path = ref('/') + const { loadFiles, isLoading } = useDAVFiles(view, path, ref(false)) + + const abort = vi.spyOn(AbortController.prototype, 'abort') + + loadFiles() + view.value = 'recent' + await waitRefLoaded(isLoading) + expect(abort).toBeCalledTimes(1) + + view.value = 'files' + await nextTick() + view.value = 'recent' + await nextTick() + view.value = 'favorites' + await waitRefLoaded(isLoading) + expect(abort).toBeCalledTimes(2) + }) })