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(core): do not update completed mutations #7104

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/query-core/src/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export class MutationObserver<
hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)
) {
this.reset()
} else {
this.#currentMutation?.setOptions(this.options)
} else if (this.#currentMutation?.state.status === 'pending') {
this.#currentMutation.setOptions(this.options)
}
}

Expand Down
154 changes: 154 additions & 0 deletions packages/query-core/src/tests/mutationObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,158 @@ describe('mutationObserver', () => {

unsubscribe()
})

test('changing mutation meta should not affect successful mutations', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to also test the following scenario:

  • set meta to { a: 1 }
  • call mutate, wait until it's done
  • trigger a re-render, update meta to { a: 2 }
  • call mutate again, wait until it's done

after that, we should have two mutations in the cache, where the first one has { a: 1 } and the second one has { a: 2 }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, added a test mutation cache should have different meta when updated between mutations in the latest commit. Also changed meta property names from count to a.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a was just an example, you didn't need to rename it 😂

const mutationObserver = new MutationObserver(queryClient, {
meta: { a: 1 },
mutationFn: async (text: string) => {
await sleep(5)
return text
},
})

const subscriptionHandler = vi.fn()

const unsubscribe = mutationObserver.subscribe(subscriptionHandler)

await mutationObserver.mutate('input')

expect(queryClient.getMutationCache().find({})).toMatchObject({
options: { meta: { a: 1 } },
state: {
status: 'success',
data: 'input',
},
})

mutationObserver.setOptions({
meta: { a: 2 },
})

expect(queryClient.getMutationCache().find({})).toMatchObject({
options: { meta: { a: 1 } },
state: {
status: 'success',
data: 'input',
},
})

unsubscribe()
})

test('mutation cache should have different meta when updated between mutations', async () => {
const mutationFn = async (text: string) => {
await sleep(5)
return text
}
const mutationObserver = new MutationObserver(queryClient, {
meta: { a: 1 },
mutationFn,
})

const subscriptionHandler = vi.fn()

const unsubscribe = mutationObserver.subscribe(subscriptionHandler)

await mutationObserver.mutate('input')

mutationObserver.setOptions({
meta: { a: 2 },
mutationFn,
})

await mutationObserver.mutate('input')

const mutations = queryClient.getMutationCache().findAll()
expect(mutations[0]).toMatchObject({
options: { meta: { a: 1 } },
state: {
status: 'success',
data: 'input',
},
})
expect(mutations[1]).toMatchObject({
options: { meta: { a: 2 } },
state: {
status: 'success',
data: 'input',
},
})

unsubscribe()
})

test('changing mutation meta should not affect rejected mutations', async () => {
const mutationObserver = new MutationObserver(queryClient, {
meta: { a: 1 },
mutationFn: async (_: string) => {
await sleep(5)
return Promise.reject(new Error('err'))
},
})

const subscriptionHandler = vi.fn()

const unsubscribe = mutationObserver.subscribe(subscriptionHandler)

await mutationObserver.mutate('input').catch(() => undefined)

expect(queryClient.getMutationCache().find({})).toMatchObject({
options: { meta: { a: 1 } },
state: {
status: 'error',
},
})

mutationObserver.setOptions({
meta: { a: 2 },
})

expect(queryClient.getMutationCache().find({})).toMatchObject({
options: { meta: { a: 1 } },
state: {
status: 'error',
},
})

unsubscribe()
})

test('changing mutation meta should affect pending mutations', async () => {
const mutationObserver = new MutationObserver(queryClient, {
meta: { a: 1 },
mutationFn: async (text: string) => {
await sleep(20)
return text
},
})

const subscriptionHandler = vi.fn()

const unsubscribe = mutationObserver.subscribe(subscriptionHandler)

mutationObserver.mutate('input')

await sleep(0)

expect(queryClient.getMutationCache().find({})).toMatchObject({
options: { meta: { a: 1 } },
state: {
status: 'pending',
},
})

mutationObserver.setOptions({
meta: { a: 2 },
})

expect(queryClient.getMutationCache().find({})).toMatchObject({
options: { meta: { a: 2 } },
state: {
status: 'pending',
},
})

unsubscribe()
})
})