-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
188 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { enableAutoUnmount, mount } from '@vue/test-utils' | ||
import { createPinia } from 'pinia' | ||
import { ref } from 'vue' | ||
import { QueryPlugin } from './query-plugin' | ||
import { defineQuery } from './define-query' | ||
import { useQuery } from './use-query' | ||
|
||
describe('defineQuery', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
enableAutoUnmount(afterEach) | ||
|
||
it('reuses the query in multiple places', async () => { | ||
const useTodoList = defineQuery({ | ||
key: ['todos'], | ||
query: async () => [{ id: 1 }], | ||
}) | ||
|
||
let returendValues!: ReturnType<typeof useTodoList> | ||
mount( | ||
{ | ||
setup() { | ||
returendValues = useTodoList() | ||
return { ...returendValues } | ||
}, | ||
template: `<div></div>`, | ||
}, | ||
{ | ||
global: { | ||
plugins: [createPinia(), QueryPlugin], | ||
}, | ||
}, | ||
) | ||
|
||
const { data } = useTodoList() | ||
expect(data).toBe(useTodoList().data) | ||
expect(data).toBe(returendValues.data) | ||
}) | ||
|
||
it('reuses the query in multiple places with a setup function', async () => { | ||
const useTodoList = defineQuery(() => { | ||
const todoFilter = ref<'all' | 'finished' | 'unfinished'>('all') | ||
const { data, ...rest } = useQuery({ | ||
key: ['todos', { filter: todoFilter.value }], | ||
query: async () => [{ id: 1 }], | ||
}) | ||
return { ...rest, todoList: data, todoFilter } | ||
}) | ||
|
||
let returendValues!: ReturnType<typeof useTodoList> | ||
mount( | ||
{ | ||
setup() { | ||
returendValues = useTodoList() | ||
return { ...returendValues } | ||
}, | ||
template: `<div></div>`, | ||
}, | ||
{ | ||
global: { | ||
plugins: [createPinia(), QueryPlugin], | ||
}, | ||
}, | ||
) | ||
|
||
const { todoList, todoFilter } = useTodoList() | ||
expect(todoList).toBe(useTodoList().todoList) | ||
expect(todoList).toBe(returendValues.todoList) | ||
expect(todoFilter).toBe(useTodoList().todoFilter) | ||
expect(todoFilter).toBe(returendValues.todoFilter) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { expectTypeOf, it } from 'vitest' | ||
import { defineQuery } from './define-query' | ||
import { useQuery } from './use-query' | ||
|
||
it('can define a query with an options object', () => { | ||
const useMyMutation = defineQuery({ | ||
key: ['todos'], | ||
query: async () => [{ id: 1 }], | ||
}) | ||
|
||
const { data, refresh } = useMyMutation() | ||
|
||
expectTypeOf(data.value).toEqualTypeOf<{ id: number }[] | undefined>() | ||
expectTypeOf(refresh()).toEqualTypeOf<Promise<{ id: number }[]>>() | ||
}) | ||
|
||
it('can define a query with a function', () => { | ||
const useMyQuery = defineQuery(() => { | ||
return { | ||
foo: 'bar', | ||
...useQuery({ | ||
key: ['todos'], | ||
query: async () => [{ id: 1 }], | ||
}), | ||
} | ||
}) | ||
|
||
const { data, refresh, foo } = useMyQuery() | ||
|
||
expectTypeOf(data.value).toEqualTypeOf<{ id: number }[] | undefined>() | ||
expectTypeOf(refresh()).toEqualTypeOf<Promise<{ id: number }[]>>() | ||
expectTypeOf(foo).toEqualTypeOf<string>() | ||
}) | ||
|
||
it.todo('can type the error', () => {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { UseQueryOptions } from './query-options' | ||
import { useQueryCache } from './query-store' | ||
import type { ErrorDefault } from './types-extension' | ||
import type { UseQueryReturn } from './use-query' | ||
import { useQuery } from './use-query' | ||
|
||
/** | ||
* Define a query with the given options. Similar to `useQuery(options)` but allows you to reuse the query in multiple | ||
* places. | ||
* | ||
* @param options - the options to define the query | ||
* @example | ||
* ```ts | ||
* const useTodoList = defineQuery({ | ||
* key: ['todos'], | ||
* query: () => fetch('/api/todos', { method: 'GET' }), | ||
* }) | ||
* ``` | ||
*/ | ||
export function defineQuery< | ||
TResult, | ||
TError = ErrorDefault, | ||
>( | ||
options: UseQueryOptions<TResult, TError>, | ||
): () => UseQueryReturn<TResult, TError> | ||
|
||
/** | ||
* Define a query with a setup function. Allows to return arbitrary values from the query function, create contextual | ||
* refs, rename the returned values, etc. | ||
* | ||
* @param setup - a function to setup the query | ||
* @example | ||
* ```ts | ||
* const useFilteredTodos = defineQuery(() => { | ||
* const todoFilter = ref<'all' | 'finished' | 'unfinished'>('all') | ||
* const { data, ...rest } = useQuery({ | ||
* key: ['todos', { filter: todoFilter.value }], | ||
* query: () => | ||
* fetch(`/api/todos?filter=${todoFilter.value}`, { method: 'GET' }), | ||
* }) | ||
* // expose the todoFilter ref and rename data for convenience | ||
* return { ...rest, todoList: data, todoFilter } | ||
* }) | ||
* ``` | ||
*/ | ||
export function defineQuery<T>(setup: () => T): () => T | ||
export function defineQuery( | ||
optionsOrSetup: UseQueryOptions | (() => unknown), | ||
): () => unknown { | ||
const setupFn = typeof optionsOrSetup === 'function' ? optionsOrSetup : () => useQuery(optionsOrSetup) | ||
return () => useQueryCache().ensureDefinedQuery(setupFn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters