Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a custom wrapper for useQuery hook from react-query #2992

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import testingLibrary from 'eslint-plugin-testing-library'
import globals from 'globals'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import pluginQuery from '@tanstack/eslint-plugin-query'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Expand All @@ -19,6 +20,7 @@ const compat = new FlatCompat({
})

export default [
...pluginQuery.configs['flat/recommended'],
{
ignores: ['src/assets/*', 'src/stories/assets/*', '.storybook/**/*']
},
Expand Down
228 changes: 228 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@mui/material": "^5.14.16",
"@mui/x-date-pickers": "^5.0.20",
"@reduxjs/toolkit": "^2.2.7",
"@tanstack/react-query": "^5.62.7",
"@tinymce/tinymce-react": "^5.1.1",
"allotment": "^1.19.3",
"axios": "^1.6.0",
Expand Down Expand Up @@ -57,6 +58,8 @@
"@storybook/react": "^8.2.6",
"@storybook/react-vite": "^8.2.6",
"@storybook/test": "^8.2.6",
"@tanstack/eslint-plugin-query": "^5.62.1",
"@tanstack/react-query-devtools": "^5.62.7",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.1.4",
"@testing-library/react": "^16.0.0",
Expand Down
13 changes: 8 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { Outlet } from 'react-router-dom'
import { StyledEngineProvider, ThemeProvider } from '@mui/material/styles'
import { ChatProvider } from '~/context/chat-context'
import { theme } from './styles/app-theme/custom-mui.styles'
import QueryProvider from '~/QueryProvider'
import PopupsProvider from './PopupsProvider'

const App = () => {
return (
<StyledEngineProvider injectFirst>
<ThemeProvider theme={theme}>
<PopupsProvider>
<ChatProvider>
<Outlet />
</ChatProvider>
</PopupsProvider>
<QueryProvider>
<PopupsProvider>
<ChatProvider>
<Outlet />
</ChatProvider>
</PopupsProvider>
</QueryProvider>
</ThemeProvider>
</StyledEngineProvider>
)
Expand Down
20 changes: 20 additions & 0 deletions src/QueryProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FC, ReactNode } from 'react'
import { QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'

import { queryClient } from '~/plugins/queryClient'

type QueryProviderProps = {
children: ReactNode
}

const QueryProvider: FC<QueryProviderProps> = ({ children }) => {
return (
<QueryClientProvider client={queryClient}>
<ReactQueryDevtools />
{children}
</QueryClientProvider>
)
}

export default QueryProvider
17 changes: 17 additions & 0 deletions src/hooks/use-query.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useQuery as useReactQuery } from '@tanstack/react-query'

type UseQueryProps<TData> = {
queryKey: string[]
queryFn: () => Promise<TData>
}

const useQuery = <TData,>({ queryKey, queryFn }: UseQueryProps<TData>) => {
const { isLoading, error, data, refetch } = useReactQuery<TData>({
queryKey,
queryFn
})

return { isLoading, error, data, refetch }
}

export default useQuery
3 changes: 3 additions & 0 deletions src/plugins/queryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { QueryClient } from '@tanstack/react-query'

export const queryClient = new QueryClient()
18 changes: 18 additions & 0 deletions tests/unit/hooks/use-query.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { renderHook } from '@testing-library/react-hooks'
import { vi } from 'vitest'

import QueryProvider from '~/QueryProvider'
import useQuery from '~/hooks/use-query'

describe('useQuery', () => {
const queryKey = ['testKey']
const queryFn = vi.fn()

it('should return loading state initially', () => {
const { result } = renderHook(() => useQuery({ queryKey, queryFn }), {
wrapper: QueryProvider
})

expect(result.current.isLoading).toBe(true)
})
})
Loading