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

fix: mutate args for useSWRInfinite hook #1947

Merged
merged 8 commits into from May 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion infinite/index.ts
Expand Up @@ -180,9 +180,13 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
]
) => {
const data = args[0]

// When passing as a boolean, it's explicitly used to disable/enable
// revalidation.
const options = typeof args[1] === 'boolean' ? {revalidate: args[1]} : args[1] || {}

// Default to true.
const shouldRevalidate = args[1] !== false
const shouldRevalidate = options.revalidate !== false

// It is possible that the key is still falsy.
if (!infiniteKey) return
Expand Down
67 changes: 66 additions & 1 deletion test/use-swr-infinite.test.tsx
Expand Up @@ -1181,7 +1181,7 @@ describe('useSWRInfinite', () => {
await screen.findByText('data: B1,B2,B3')
})

it('should revalidate when the component is mounted and revalidateOnMount is enabled', async () => {
it('should revalidate the resource with bound mutate when arguments are passed', async () => {
const key = createKey()

let counter = 0
Expand Down Expand Up @@ -1220,4 +1220,69 @@ describe('useSWRInfinite', () => {
fireEvent.click(screen.getByText('mutate'))
await screen.findByText('data: 2')
})

// https://github.com/vercel/swr/issues/1899
it('should revalidate the resource with bound mutate when options is of Object type ', async () => {
let t = 0
const key = createKey()
const fetcher = jest.fn(async () =>
createResponse(`foo-${t++}`, { delay: 10 })
)
const logger = []
function Page() {
const { data, mutate } = useSWRInfinite(() => key, fetcher, {
dedupingInterval: 0
})
logger.push(data)
return (
<>
<div>data: {String(data)}</div>
<button onClick={() => mutate(data, { revalidate: true })}>
mutate
</button>
</>
)
}

renderWithConfig(<Page />)
await screen.findByText('data: foo-0')

fireEvent.click(screen.getByText('mutate'))
await screen.findByText('data: foo-1')
expect(fetcher).toBeCalledTimes(2)

expect(logger).toEqual([undefined, ['foo-0'], ['foo-1']])
})

// https://github.com/vercel/swr/issues/1899
it('should not revalidate the resource with bound mutate when options is of Object type', async () => {
let t = 0
const key = createKey()
const fetcher = jest.fn(async () =>
createResponse(`foo-${t++}`, { delay: 10 })
)
const logger = []
function Page() {
const { data, mutate } = useSWRInfinite(() => key, fetcher, {
dedupingInterval: 0
})
logger.push(data)
return (
<>
<div>data: {String(data)}</div>
<button onClick={() => mutate(data, { revalidate: false })}>
mutate
</button>
</>
)
}

renderWithConfig(<Page />)
await screen.findByText('data: foo-0')

fireEvent.click(screen.getByText('mutate'))
expect(fetcher).toBeCalledTimes(1)

expect(logger).toEqual([undefined, ['foo-0']])
})
})