FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
frontend-app-learning/src/queryClient.test.ts at master · openedx/frontend-app-learning · GitHub
openedx
frontend-app-learning
Repository navigation
Code
Issues
90
(90)
Pull requests
45
(45)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
frontend-app-learning
/
src
/
queryClient.test.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
115 lines (93 loc) · 4.13 KB
Breadcrumbs
frontend-app-learning
/
src
/
queryClient.test.ts
Copy path
File metadata and controls
115 lines (93 loc) · 4.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import
{
QueryClient
}
from
'@tanstack/react-query'
;
import
{
createAppQueryCache
,
createQueryClient
,
shouldRetryQuery
}
from
'./queryClient'
;
import
{
NonRetryableError
}
from
'./data/http-error'
;
import
{
initializeMockApp
}
from
'./setupTest'
;
const
{
loggingService
}
=
initializeMockApp
(
)
;
describe
(
'app query cache'
,
(
)
=>
{
let
queryClient
:
QueryClient
;
beforeEach
(
(
)
=>
{
queryClient
=
new
QueryClient
(
{
defaultOptions
:
{
queries
:
{
retry
:
false
}
}
,
queryCache
:
createAppQueryCache
(
)
,
}
)
;
loggingService
.
logError
.
mockReset
(
)
;
loggingService
.
logInfo
.
mockReset
(
)
;
}
)
;
it
(
'reports query errors through onError'
,
async
(
)
=>
{
const
error
=
new
Error
(
'boom'
)
;
await
queryClient
.
fetchQuery
(
{
queryKey
:
[
'failing'
]
,
queryFn
:
(
)
=>
Promise
.
reject
(
error
)
,
}
)
.
catch
(
(
)
=>
{
}
)
;
expect
(
loggingService
.
logError
)
.
toHaveBeenCalledWith
(
error
,
undefined
)
;
}
)
;
it
(
'logs a status listed in `logStatusAs` at that level instead of as an error'
,
async
(
)
=>
{
const
error
=
{
response
:
{
status
:
403
}
}
;
await
queryClient
.
fetchQuery
(
{
queryKey
:
[
'forbidden'
]
,
queryFn
:
(
)
=>
Promise
.
reject
(
error
)
,
meta
:
{
logStatusAs
:
{
403
:
'info'
}
}
,
}
)
.
catch
(
(
)
=>
{
}
)
;
expect
(
loggingService
.
logInfo
)
.
toHaveBeenCalledWith
(
error
,
undefined
)
;
expect
(
loggingService
.
logError
)
.
not
.
toHaveBeenCalled
(
)
;
}
)
;
it
(
'does not log a status mapped to `silent`'
,
async
(
)
=>
{
const
error
=
{
response
:
{
status
:
422
}
}
;
await
queryClient
.
fetchQuery
(
{
queryKey
:
[
'maybe-unit'
]
,
queryFn
:
(
)
=>
Promise
.
reject
(
error
)
,
meta
:
{
logStatusAs
:
{
422
:
'silent'
}
}
,
}
)
.
catch
(
(
)
=>
{
}
)
;
expect
(
loggingService
.
logError
)
.
not
.
toHaveBeenCalled
(
)
;
expect
(
loggingService
.
logInfo
)
.
not
.
toHaveBeenCalled
(
)
;
}
)
;
}
)
;
describe
(
'shouldRetryQuery'
,
(
)
=>
{
const
httpError
=
(
status
:
number
)
=>
(
{
response
:
{
status
}
}
)
;
it
.
each
(
[
400
,
401
,
403
,
404
,
422
]
)
(
'does not retry client error %s'
,
(
status
)
=>
{
expect
(
shouldRetryQuery
(
0
,
httpError
(
status
)
)
)
.
toBe
(
false
)
;
}
)
;
it
(
'does not retry a NonRetryableError (our own deterministic throw)'
,
(
)
=>
{
expect
(
shouldRetryQuery
(
0
,
new
NonRetryableError
(
'bad response'
)
)
)
.
toBe
(
false
)
;
}
)
;
it
.
each
(
[
500
,
502
,
503
]
)
(
'retries server error %s until 3 attempts'
,
(
status
)
=>
{
expect
(
shouldRetryQuery
(
0
,
httpError
(
status
)
)
)
.
toBe
(
true
)
;
expect
(
shouldRetryQuery
(
2
,
httpError
(
status
)
)
)
.
toBe
(
true
)
;
expect
(
shouldRetryQuery
(
3
,
httpError
(
status
)
)
)
.
toBe
(
false
)
;
}
)
;
it
(
'retries network (undefined-status) errors until 3 attempts'
,
(
)
=>
{
const
networkError
=
new
Error
(
'Network Error'
)
;
expect
(
shouldRetryQuery
(
0
,
networkError
)
)
.
toBe
(
true
)
;
expect
(
shouldRetryQuery
(
2
,
networkError
)
)
.
toBe
(
true
)
;
expect
(
shouldRetryQuery
(
3
,
networkError
)
)
.
toBe
(
false
)
;
}
)
;
it
(
'is wired in as the default query retry policy on createQueryClient'
,
(
)
=>
{
expect
(
createQueryClient
(
)
.
getDefaultOptions
(
)
.
queries
?.
retry
)
.
toBe
(
shouldRetryQuery
)
;
}
)
;
}
)
;
describe
(
'createQueryClient retry behavior (integration)'
,
(
)
=>
{
const
attemptsFor
=
async
(
error
:
unknown
)
:
Promise
<
number
>
=>
{
const
queryFn
=
jest
.
fn
(
)
.
mockRejectedValue
(
error
)
;
await
createQueryClient
(
)
.
fetchQuery
(
{
queryKey
:
[
'retry-test'
]
,
queryFn
,
retryDelay
:
0
,
}
)
.
catch
(
(
)
=>
{
}
)
;
return
queryFn
.
mock
.
calls
.
length
;
}
;
it
(
'does not retry a 4xx (1 attempt)'
,
async
(
)
=>
{
expect
(
await
attemptsFor
(
{
response
:
{
status
:
403
}
}
)
)
.
toBe
(
1
)
;
}
)
;
it
(
'does not retry a NonRetryableError (1 attempt)'
,
async
(
)
=>
{
expect
(
await
attemptsFor
(
new
NonRetryableError
(
'bad response'
)
)
)
.
toBe
(
1
)
;
}
)
;
it
(
'retries a 5xx server error (4 attempts: 1 + 3 retries)'
,
async
(
)
=>
{
expect
(
await
attemptsFor
(
{
response
:
{
status
:
500
}
}
)
)
.
toBe
(
4
)
;
}
)
;
it
(
'retries a network error (4 attempts: 1 + 3 retries)'
,
async
(
)
=>
{
expect
(
await
attemptsFor
(
new
Error
(
'network'
)
)
)
.
toBe
(
4
)
;
}
)
;
}
)
;
describe
(
'createQueryClient defaults'
,
(
)
=>
{
it
(
'disables refetchOnWindowFocus (parity with the pre-RQ no-focus-refetch behavior)'
,
(
)
=>
{
expect
(
createQueryClient
(
)
.
getDefaultOptions
(
)
.
queries
?.
refetchOnWindowFocus
)
.
toBe
(
false
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL