-
-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { interval, map, pipe } from 'wonka'; | ||
import { RequestPolicy } from '@urql/core'; | ||
import { vi, expect, it, beforeEach, describe, beforeAll, Mock } from 'vitest'; | ||
|
@@ -87,22 +87,18 @@ describe('useQuery', () => { | |
}); | ||
|
||
it('should execute the subscription', async () => { | ||
const { waitForNextUpdate } = renderHook( | ||
({ query, variables }) => useQuery({ query, variables }), | ||
{ initialProps: { query: mockQuery, variables: mockVariables } } | ||
); | ||
renderHook(({ query, variables }) => useQuery({ query, variables }), { | ||
Check failure on line 90 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should execute the subscription
|
||
initialProps: { query: mockQuery, variables: mockVariables }, | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should pass query and variables to executeQuery', async () => { | ||
const { waitForNextUpdate } = renderHook( | ||
({ query, variables }) => useQuery({ query, variables }), | ||
{ initialProps: { query: mockQuery, variables: mockVariables } } | ||
); | ||
renderHook(({ query, variables }) => useQuery({ query, variables }), { | ||
Check failure on line 98 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should pass query and variables to executeQuery
|
||
initialProps: { query: mockQuery, variables: mockVariables }, | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
expect(client.executeQuery).toBeCalledWith( | ||
{ | ||
|
@@ -117,12 +113,12 @@ describe('useQuery', () => { | |
}); | ||
|
||
it('should return data from executeQuery', async () => { | ||
const { result, waitForNextUpdate } = renderHook( | ||
const { result } = renderHook( | ||
Check failure on line 116 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should return data from executeQuery
|
||
({ query, variables }) => useQuery({ query, variables }), | ||
{ initialProps: { query: mockQuery, variables: mockVariables } } | ||
); | ||
|
||
await waitForNextUpdate(); | ||
await new Promise(res => setTimeout(res, 30)); | ||
const [state] = result.current; | ||
expect(state).toEqual({ | ||
fetching: false, | ||
|
@@ -134,14 +130,13 @@ describe('useQuery', () => { | |
}); | ||
|
||
it('should update if a new query is received', async () => { | ||
const { rerender, waitForNextUpdate } = renderHook< | ||
{ query: string; variables?: object }, | ||
{} | ||
const { rerender } = renderHook< | ||
Check failure on line 133 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should update if a new query is received
|
||
any, | ||
{ query: string; variables: { id?: number } } | ||
>(({ query, variables }) => useQuery({ query, variables }), { | ||
initialProps: { query: mockQuery, variables: mockVariables }, | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
|
||
const newQuery = ` | ||
|
@@ -151,7 +146,7 @@ describe('useQuery', () => { | |
} | ||
`; | ||
|
||
rerender({ query: newQuery }); | ||
rerender({ query: newQuery, variables: {} }); | ||
expect(client.executeQuery).toBeCalledTimes(2); | ||
expect(client.executeQuery).toHaveBeenNthCalledWith( | ||
2, | ||
|
@@ -167,14 +162,13 @@ describe('useQuery', () => { | |
}); | ||
|
||
it('should update if new variables are received', async () => { | ||
const { rerender, waitForNextUpdate } = renderHook< | ||
{ query: string; variables: object }, | ||
{} | ||
>(({ query, variables }) => useQuery({ query, variables }), { | ||
initialProps: { query: mockQuery, variables: mockVariables }, | ||
}); | ||
const { rerender } = renderHook( | ||
Check failure on line 165 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should update if new variables are received
|
||
({ query, variables }) => useQuery({ query, variables }), | ||
{ | ||
initialProps: { query: mockQuery, variables: mockVariables }, | ||
} | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
|
||
const newVariables = { | ||
|
@@ -197,22 +191,21 @@ describe('useQuery', () => { | |
}); | ||
|
||
it('should not update if query and variables are unchanged', async () => { | ||
const { rerender, waitForNextUpdate } = renderHook( | ||
const { rerender } = renderHook( | ||
Check failure on line 194 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should not update if query and variables are unchanged
|
||
({ query, variables }) => useQuery({ query, variables }), | ||
{ | ||
initialProps: { query: mockQuery, variables: mockVariables }, | ||
} | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
|
||
rerender({ query: mockQuery, variables: mockVariables }); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should update if a new requestPolicy is provided', async () => { | ||
const { rerender, waitForNextUpdate } = renderHook( | ||
const { rerender } = renderHook( | ||
Check failure on line 208 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should update if a new requestPolicy is provided
|
||
({ query, variables, requestPolicy }) => | ||
useQuery({ query, variables, requestPolicy }), | ||
{ | ||
|
@@ -224,7 +217,6 @@ describe('useQuery', () => { | |
} | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
expect(client.executeQuery).toHaveBeenNthCalledWith( | ||
1, | ||
|
@@ -258,17 +250,15 @@ describe('useQuery', () => { | |
}); | ||
|
||
it('should provide an executeQuery function to be imperatively executed', async () => { | ||
const { result, waitForNextUpdate } = renderHook( | ||
const { result } = renderHook( | ||
Check failure on line 253 in packages/react-urql/src/hooks/useQuery.spec.ts GitHub Actions / Checkspackages/react-urql/src/hooks/useQuery.spec.ts > useQuery > should provide an executeQuery function to be imperatively executed
|
||
({ query, variables }) => useQuery({ query, variables }), | ||
{ initialProps: { query: mockQuery, variables: mockVariables } } | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
|
||
const [, executeQuery] = result.current; | ||
act(() => executeQuery()); | ||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(2); | ||
}); | ||
|
||
|
@@ -288,8 +278,11 @@ describe('useQuery', () => { | |
}); | ||
|
||
it('should pause executing the query if pause updates to true', async () => { | ||
const { rerender, waitForNextUpdate } = renderHook( | ||
({ query, variables, pause }) => useQuery({ query, variables, pause }), | ||
const { rerender } = renderHook( | ||
props => { | ||
const { query, variables, pause } = props; | ||
return useQuery({ query, variables, pause }); | ||
}, | ||
{ | ||
initialProps: { | ||
query: mockQuery, | ||
|
@@ -299,7 +292,6 @@ describe('useQuery', () => { | |
} | ||
); | ||
|
||
await waitForNextUpdate(); | ||
expect(client.executeQuery).toBeCalledTimes(1); | ||
|
||
rerender({ query: mockQuery, variables: mockVariables, pause: true }); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import { mergeConfig } from 'vitest/config'; | ||
import baseConfig from '../../vitest.config'; | ||
|
||
export default mergeConfig(baseConfig, {}); | ||
export default mergeConfig(baseConfig, { | ||
test: { | ||
environment: 'jsdom', | ||
}, | ||
}); |