Skip to content

Commit 0bb74b2

Browse files
committed
test: improve test coverage
1 parent c652229 commit 0bb74b2

File tree

7 files changed

+221
-27
lines changed

7 files changed

+221
-27
lines changed

infinite/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
115115

116116
let previousPageData = null
117117
for (let i = 0; i < pageSize; ++i) {
118-
const [pageKey, pageArgs] = serialize(
119-
getKey ? getKey(i, previousPageData) : null
120-
)
118+
const [pageKey, pageArgs] = serialize(getKey(i, previousPageData))
121119

122120
if (!pageKey) {
123121
// `pageKey` is falsy, stop fetching new pages.
@@ -208,7 +206,7 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
208206

209207
let previousPageData = null
210208
for (let i = 0; i < pageSize; ++i) {
211-
const [pageKey] = serialize(getKey ? getKey(i, previousPageData) : null)
209+
const [pageKey] = serialize(getKey(i, previousPageData))
212210

213211
// Get the cached page data.
214212
const pageData = pageKey ? cache.get(pageKey) : UNDEFINED

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
"@testing-library/jest-dom": "5.14.1",
7676
"@testing-library/react": "12.0.0",
7777
"@types/react": "17.0.20",
78-
"@typescript-eslint/eslint-plugin": "5.4.0",
79-
"@typescript-eslint/parser": "5.4.0",
78+
"@typescript-eslint/eslint-plugin": "5.8.0",
79+
"@typescript-eslint/parser": "5.8.0",
8080
"bunchee": "1.7.1",
8181
"eslint": "8.3.0",
8282
"eslint-config-prettier": "8.3.0",

src/utils/config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { stableHash } from './hash'
2-
import { initCache } from './cache'
3-
import { preset } from './web-preset'
4-
import { slowConnection } from './env'
5-
import {
1+
import type {
62
PublicConfiguration,
73
FullConfiguration,
84
RevalidatorOptions,
95
Revalidator,
106
ScopedMutator,
117
Cache
128
} from '../types'
9+
import { stableHash } from './hash'
10+
import { initCache } from './cache'
11+
import { preset } from './web-preset'
12+
import { slowConnection } from './env'
1313
import { isUndefined, noop, mergeObjects } from './helper'
1414

1515
// error retry

test/use-swr-infinite.test.tsx

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,58 @@ describe('useSWRInfinite', () => {
1515
it('should render the first page component', async () => {
1616
const key = createKey()
1717
function Page() {
18-
const { data } = useSWRInfinite(
18+
const { data, error, isValidating } = useSWRInfinite(
1919
index => `page-${index}-${key}`,
2020
infiniteKey => createResponse(infiniteKey)
2121
)
2222

23-
return <div>data:{data}</div>
23+
return (
24+
<div>
25+
<div>data:{data}</div>
26+
<div>error:{error}</div>
27+
<div>isValidating:{isValidating.toString()}</div>
28+
</div>
29+
)
2430
}
2531

2632
renderWithConfig(<Page />)
2733
screen.getByText('data:')
2834

2935
await screen.findByText(`data:page-0-${key}`)
36+
await screen.findByText(`error:`)
37+
await screen.findByText(`isValidating:false`)
38+
})
39+
40+
it('should not render anything if getKey throw error and call mutate wont cause error', async () => {
41+
function Page() {
42+
const { data, error, isValidating, mutate } = useSWRInfinite(
43+
() => {
44+
throw new Error('error')
45+
},
46+
infiniteKey => createResponse(infiniteKey)
47+
)
48+
49+
return (
50+
<div>
51+
<div onClick={() => mutate()}>data:{data}</div>
52+
<div>error:{error}</div>
53+
<div>isValidating:{isValidating.toString()}</div>
54+
</div>
55+
)
56+
}
57+
58+
renderWithConfig(<Page />)
59+
screen.getByText('data:')
60+
61+
await screen.findByText(`data:`)
62+
await screen.findByText(`error:`)
63+
await screen.findByText(`isValidating:false`)
64+
65+
fireEvent.click(screen.getByText('data:'))
66+
67+
await screen.findByText(`data:`)
68+
await screen.findByText(`error:`)
69+
await screen.findByText(`isValidating:false`)
3070
})
3171

3272
it('should render the multiple pages', async () => {
@@ -773,6 +813,39 @@ describe('useSWRInfinite', () => {
773813
expect(loggedValues).toEqual([1])
774814
})
775815

816+
it('setSize should only accept number', async () => {
817+
const key = createKey()
818+
function Comp() {
819+
const { data, size, setSize } = useSWRInfinite(
820+
index => [key, index],
821+
(_, index) => createResponse(`page ${index}`)
822+
)
823+
824+
return (
825+
<>
826+
<div
827+
onClick={() => {
828+
// load next page
829+
// @ts-ignore
830+
setSize('2')
831+
}}
832+
>
833+
data:{data}
834+
</div>
835+
<div>size:{size}</div>
836+
</>
837+
)
838+
}
839+
renderWithConfig(<Comp></Comp>)
840+
await screen.findByText('data:page 0')
841+
await screen.findByText('size:1')
842+
843+
fireEvent.click(screen.getByText('data:page 0'))
844+
845+
await screen.findByText('data:page 0')
846+
await screen.findByText('size:1')
847+
})
848+
776849
it('should correctly set size when setSize receives a callback', async () => {
777850
const key = createKey()
778851

test/use-swr-key.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,23 @@ describe('useSWR - key', () => {
141141
await screen.findByText(`${baseKey}-second`)
142142
})
143143

144+
it('should not fetch if the function key throws an error', async () => {
145+
let value = 0
146+
const fetcher = jest.fn(() => value++)
147+
const key = () => {
148+
throw new Error('error')
149+
}
150+
151+
function Page() {
152+
const { data } = useSWR(key, fetcher)
153+
return <div>{`key-${data}`}</div>
154+
}
155+
156+
renderWithConfig(<Page />)
157+
await screen.findByText(`key-undefined`)
158+
expect(fetcher).toBeCalledTimes(0)
159+
})
160+
144161
it('should cleanup state when key turns to empty', async () => {
145162
const key = createKey()
146163
function Page() {

test/use-swr-local-mutation.test.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ describe('useSWR - local mutation', () => {
323323
).rejects.toBeInstanceOf(Error)
324324
})
325325

326+
it('globalMutate should return undefined if the key is serialized to "" ', async () => {
327+
// returns the data if promise resolved
328+
expect(globalMutate(null, Promise.resolve('data'))).resolves.toBe(undefined)
329+
330+
// throw the error if promise rejected
331+
expect(
332+
globalMutate(() => {
333+
throw new Error('error')
334+
}, Promise.resolve('data'))
335+
).resolves.toBe(undefined)
336+
})
337+
326338
it('should get bound mutate from useSWR', async () => {
327339
const key = createKey()
328340
function Page() {
@@ -922,4 +934,52 @@ describe('useSWR - local mutation', () => {
922934
'async3'
923935
])
924936
})
937+
938+
it('should ignore in flight mutation error when calling another async mutate', async () => {
939+
const key = createKey()
940+
const errorMutate = () =>
941+
new Promise<string>((_, reject) => {
942+
setTimeout(() => reject('error'), 200)
943+
})
944+
945+
const successMutate = () =>
946+
new Promise<string>(resolve => {
947+
setTimeout(() => resolve('success'), 100)
948+
})
949+
function Page() {
950+
const { data, mutate: boundMutate } = useSWR(key, () =>
951+
createResponse('data', { delay: 100 })
952+
)
953+
return (
954+
<div>
955+
<div>{data}</div>
956+
<button
957+
onClick={() => {
958+
boundMutate(successMutate, false)
959+
}}
960+
>
961+
success-mutate
962+
</button>
963+
<button
964+
onClick={() => {
965+
boundMutate(errorMutate, false).catch(() => {})
966+
}}
967+
>
968+
error-mutate
969+
</button>
970+
</div>
971+
)
972+
}
973+
renderWithConfig(<Page />)
974+
await screen.findByText('data')
975+
976+
fireEvent.click(screen.getByText('error-mutate'))
977+
await sleep(50)
978+
979+
fireEvent.click(screen.getByText('success-mutate'))
980+
await screen.findByText('success')
981+
982+
await sleep(300)
983+
await screen.findByText('success')
984+
})
925985
})

yarn.lock

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,21 +1593,33 @@
15931593
dependencies:
15941594
"@types/yargs-parser" "*"
15951595

1596-
"@typescript-eslint/eslint-plugin@5.4.0":
1597-
version "5.4.0"
1598-
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.4.0.tgz#05e711a2e7b68342661fde61bccbd1531c19521a"
1599-
integrity sha512-9/yPSBlwzsetCsGEn9j24D8vGQgJkOTr4oMLas/w886ZtzKIs1iyoqFrwsX2fqYEeUwsdBpC21gcjRGo57u0eg==
1596+
"@typescript-eslint/eslint-plugin@5.8.0":
1597+
version "5.8.0"
1598+
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.8.0.tgz#52cd9305ceef98a5333f9492d519e6c6c7fe7d43"
1599+
integrity sha512-spu1UW7QuBn0nJ6+psnfCc3iVoQAifjKORgBngKOmC8U/1tbe2YJMzYQqDGYB4JCss7L8+RM2kKLb1B1Aw9BNA==
16001600
dependencies:
1601-
"@typescript-eslint/experimental-utils" "5.4.0"
1602-
"@typescript-eslint/scope-manager" "5.4.0"
1601+
"@typescript-eslint/experimental-utils" "5.8.0"
1602+
"@typescript-eslint/scope-manager" "5.8.0"
16031603
debug "^4.3.2"
16041604
functional-red-black-tree "^1.0.1"
16051605
ignore "^5.1.8"
16061606
regexpp "^3.2.0"
16071607
semver "^7.3.5"
16081608
tsutils "^3.21.0"
16091609

1610-
"@typescript-eslint/[email protected]", "@typescript-eslint/experimental-utils@^5.0.0":
1610+
"@typescript-eslint/[email protected]":
1611+
version "5.8.0"
1612+
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-5.8.0.tgz#0916ffe98d34b3c95e3652efa0cace61a7b25728"
1613+
integrity sha512-KN5FvNH71bhZ8fKtL+lhW7bjm7cxs1nt+hrDZWIqb6ViCffQcWyLunGrgvISgkRojIDcXIsH+xlFfI4RCDA0xA==
1614+
dependencies:
1615+
"@types/json-schema" "^7.0.9"
1616+
"@typescript-eslint/scope-manager" "5.8.0"
1617+
"@typescript-eslint/types" "5.8.0"
1618+
"@typescript-eslint/typescript-estree" "5.8.0"
1619+
eslint-scope "^5.1.1"
1620+
eslint-utils "^3.0.0"
1621+
1622+
"@typescript-eslint/experimental-utils@^5.0.0":
16111623
version "5.4.0"
16121624
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-5.4.0.tgz#238a7418d2da3b24874ba35385eb21cc61d2a65e"
16131625
integrity sha512-Nz2JDIQUdmIGd6p33A+naQmwfkU5KVTLb/5lTk+tLVTDacZKoGQisj8UCxk7onJcrgjIvr8xWqkYI+DbI3TfXg==
@@ -1619,14 +1631,14 @@
16191631
eslint-scope "^5.1.1"
16201632
eslint-utils "^3.0.0"
16211633

1622-
"@typescript-eslint/parser@5.4.0":
1623-
version "5.4.0"
1624-
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.4.0.tgz#3aa83ce349d66e39b84151f6d5464928044ca9e3"
1625-
integrity sha512-JoB41EmxiYpaEsRwpZEYAJ9XQURPFer8hpkIW9GiaspVLX8oqbqNM8P4EP8HOZg96yaALiLEVWllA2E8vwsIKw==
1634+
"@typescript-eslint/parser@5.8.0":
1635+
version "5.8.0"
1636+
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.8.0.tgz#b39970b21c1d7bc4a6018507fb29b380328d2587"
1637+
integrity sha512-Gleacp/ZhRtJRYs5/T8KQR3pAQjQI89Dn/k+OzyCKOsLiZH2/Vh60cFBTnFsHNI6WAD+lNUo/xGZ4NeA5u0Ipw==
16261638
dependencies:
1627-
"@typescript-eslint/scope-manager" "5.4.0"
1628-
"@typescript-eslint/types" "5.4.0"
1629-
"@typescript-eslint/typescript-estree" "5.4.0"
1639+
"@typescript-eslint/scope-manager" "5.8.0"
1640+
"@typescript-eslint/types" "5.8.0"
1641+
"@typescript-eslint/typescript-estree" "5.8.0"
16301642
debug "^4.3.2"
16311643

16321644
"@typescript-eslint/[email protected]":
@@ -1637,11 +1649,24 @@
16371649
"@typescript-eslint/types" "5.4.0"
16381650
"@typescript-eslint/visitor-keys" "5.4.0"
16391651

1652+
"@typescript-eslint/[email protected]":
1653+
version "5.8.0"
1654+
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.8.0.tgz#2371095b4fa4c7be6a80b380f4e1b49c715e16f4"
1655+
integrity sha512-x82CYJsLOjPCDuFFEbS6e7K1QEWj7u5Wk1alw8A+gnJiYwNnDJk0ib6PCegbaPMjrfBvFKa7SxE3EOnnIQz2Gg==
1656+
dependencies:
1657+
"@typescript-eslint/types" "5.8.0"
1658+
"@typescript-eslint/visitor-keys" "5.8.0"
1659+
16401660
"@typescript-eslint/[email protected]":
16411661
version "5.4.0"
16421662
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.4.0.tgz#b1c130f4b381b77bec19696c6e3366f9781ce8f2"
16431663
integrity sha512-GjXNpmn+n1LvnttarX+sPD6+S7giO+9LxDIGlRl4wK3a7qMWALOHYuVSZpPTfEIklYjaWuMtfKdeByx0AcaThA==
16441664

1665+
"@typescript-eslint/[email protected]":
1666+
version "5.8.0"
1667+
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.8.0.tgz#e7fa74ec35d9dbe3560d039d3d8734986c3971e0"
1668+
integrity sha512-LdCYOqeqZWqCMOmwFnum6YfW9F3nKuxJiR84CdIRN5nfHJ7gyvGpXWqL/AaW0k3Po0+wm93ARAsOdzlZDPCcXg==
1669+
16451670
"@typescript-eslint/[email protected]":
16461671
version "5.4.0"
16471672
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.4.0.tgz#fe524fb308973c68ebeb7428f3b64499a6ba5fc0"
@@ -1655,6 +1680,19 @@
16551680
semver "^7.3.5"
16561681
tsutils "^3.21.0"
16571682

1683+
"@typescript-eslint/[email protected]":
1684+
version "5.8.0"
1685+
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.8.0.tgz#900469ba9d5a37f4482b014ecce4a5dbb86cb4dd"
1686+
integrity sha512-srfeZ3URdEcUsSLbkOFqS7WoxOqn8JNil2NSLO9O+I2/Uyc85+UlfpEvQHIpj5dVts7KKOZnftoJD/Fdv0L7nQ==
1687+
dependencies:
1688+
"@typescript-eslint/types" "5.8.0"
1689+
"@typescript-eslint/visitor-keys" "5.8.0"
1690+
debug "^4.3.2"
1691+
globby "^11.0.4"
1692+
is-glob "^4.0.3"
1693+
semver "^7.3.5"
1694+
tsutils "^3.21.0"
1695+
16581696
"@typescript-eslint/[email protected]":
16591697
version "5.4.0"
16601698
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.4.0.tgz#09bc28efd3621f292fe88c86eef3bf4893364c8c"
@@ -1663,6 +1701,14 @@
16631701
"@typescript-eslint/types" "5.4.0"
16641702
eslint-visitor-keys "^3.0.0"
16651703

1704+
"@typescript-eslint/[email protected]":
1705+
version "5.8.0"
1706+
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.8.0.tgz#22d4ed96fe2451135299239feedb9fe1dcec780c"
1707+
integrity sha512-+HDIGOEMnqbxdAHegxvnOqESUH6RWFRR2b8qxP1W9CZnnYh4Usz6MBL+2KMAgPk/P0o9c1HqnYtwzVH6GTIqug==
1708+
dependencies:
1709+
"@typescript-eslint/types" "5.8.0"
1710+
eslint-visitor-keys "^3.0.0"
1711+
16661712
abab@^2.0.3, abab@^2.0.5:
16671713
version "2.0.5"
16681714
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"

0 commit comments

Comments
 (0)