Skip to content

Commit

Permalink
get rid of warnings during unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca committed Jan 23, 2025
1 parent bdebe1e commit dad1431
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
28 changes: 19 additions & 9 deletions spx-gui/src/utils/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { ref } from 'vue'
import { flushPromises } from '@vue/test-utils'
import { describe, it, expect, vi } from 'vitest'
import { useQuery, composeQuery, type QueryContext } from './query'
import { withSetup } from './test'

describe('composeQuery', () => {
it('should work well', async () => {
const valueRef = ref(0)
const queryFn1 = vi.fn(async () => valueRef.value)
const ret1 = useQuery(queryFn1)
const queryFn2 = vi.fn(async (ctx: QueryContext) => composeQuery(ctx, ret1))
const ret2 = useQuery(queryFn2)
const [queryFn1, ret1, queryFn2, ret2] = withSetup(() => {
const queryFn1 = vi.fn(async () => valueRef.value)
const ret1 = useQuery(queryFn1)
const queryFn2 = vi.fn(async (ctx: QueryContext) => composeQuery(ctx, ret1))
const ret2 = useQuery(queryFn2)
return [queryFn1, ret1, queryFn2, ret2] as const
})
expect(ret2.isLoading.value).toBe(true)
expect(ret2.data.value).toBe(null)
expect(ret2.error.value).toBe(null)
Expand Down Expand Up @@ -56,8 +60,11 @@ describe('composeQuery', () => {

it('should work well with exception', async () => {
const err = new Error('test')
const ret1 = useQuery(makeErrorFn(3, err))
const ret2 = useQuery(async (ctx: QueryContext) => composeQuery(ctx, ret1))
const [ret1, ret2] = withSetup(() => {
const ret1 = useQuery(makeErrorFn(3, err))
const ret2 = useQuery(async (ctx: QueryContext) => composeQuery(ctx, ret1))
return [ret1, ret2] as const
})

await flushPromises()
expect(ret2.isLoading.value).toBe(false)
Expand Down Expand Up @@ -86,9 +93,12 @@ describe('composeQuery', () => {
it('should work well with multiple dependencies', async () => {
const err1 = new Error('test1')
const err2 = new Error('test2')
const ret1 = useQuery(makeErrorFn(1, err1))
const ret2 = useQuery(makeErrorFn(2, err2))
const ret3 = useQuery(async (ctx: QueryContext) => Promise.all([composeQuery(ctx, ret1), composeQuery(ctx, ret2)]))
const ret3 = withSetup(() => {
const ret1 = useQuery(makeErrorFn(1, err1))
const ret2 = useQuery(makeErrorFn(2, err2))
const ret3 = useQuery(async (ctx: QueryContext) => Promise.all([composeQuery(ctx, ret1), composeQuery(ctx, ret2)]))
return ret3
})

await flushPromises()
expect(ret3.isLoading.value).toBe(false)
Expand Down
2 changes: 1 addition & 1 deletion spx-gui/src/utils/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function useQuery<T>(
},
(e) => {
if (e instanceof Cancelled) return
console.warn(e)
if (process.env.NODE_ENV !== 'test') console.warn(e)
error.value = e
isLoading.value = false
}
Expand Down
16 changes: 16 additions & 0 deletions spx-gui/src/utils/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @desc Helpers for testing
*/

import { createApp } from 'vue'
import { File } from '@/models/common/file'

export function sleep(duration = 1000) {
Expand All @@ -13,3 +14,18 @@ export function delayFile(file: File, duration = 1000) {
type: file!.type
})
}

/**
* Helper for testing composable functions that rely on a host component instance.
* For more information, check https://vuejs.org/guide/scaling-up/testing#testing-composables.
*/
export function withSetup<R>(composable: () => R) {
let result: R
createApp({
setup() {
result = composable()
return () => {}
}
}).mount(document.createElement('div'))
return result!
}

0 comments on commit dad1431

Please sign in to comment.