-
Feature RequestAdding a mechanism to support the Dependency Injection (DI) of arbitrary values into Background and Current SituationAs of now, the
I propose to extend this support to allow end-users to add arbitrary values, thereby facilitating a more flexible usage of the Detailed ProposalMy current idea is to enable this through the Dependency Injection (DI) of these values at the time of creating the const queryClient = new QueryClient({
...
extraArgument: { /** inject something useful here */ }
}) This mechanism would resemble the withExtraArgument function utilized in redux-thunk. MotivationImplementing the above proposal would facilitate easier writing of queryFn tests. Although queryFn generally involves API requests, which usually result in the following type of code: // CURRENT
// queryFn
const fetchTodoList = () => {
const { data } = api.get('/todos'); // api refers to any http client of your preference.
return data
}
// usecase
const { data } = useQuery({
queryKey: ['todo', 'list'],
queryFn: fetchTodoList
})
// test
test('fetchTodoList', async () => {
const api = { get: jest.fn() }
jest.mock('path/to/api', () => api); // you have to mock http client...
await fetchTodoList();
expect(api.get).toHaveBeenCalledWith('/todos')
}) To test If the proposed DI into // PROPOSED
// init
const queryClient = new QueryClient({
extraArgument: { api } // inject http client
})
// queryFn
const fetchTodoList = ({ api }) => {
const { data } = api.get('/todos'); // no need to api directly!!!
return data
}
// usecase
const { data } = useQuery({
queryKey: ['todo', 'list'],
queryFn: fetchTodoList
})
// test
test('fetchTodoList', async () => {
const api = { get: jest.fn() }
await fetchTodoList({ api }); // no need to mock api!!
expect(api.get).toHaveBeenCalledWith('/todos')
}) ConclusionI believe that the addition of this feature to the I eagerly await your feedback and am open to discussing the technical feasibility and implications of this feature request further. Any suggestions or opinions on this feature request would be highly valued. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
I would say this is exactly what
|
Beta Was this translation helpful? Give feedback.
I would say this is exactly what
meta
is for 😅 . From the docs: