From b0ec3bead644ec3aa3d6dcaf9dacc566f4a4ee33 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Thu, 1 Oct 2026 03:20:11 +0900 Subject: [PATCH] test(lit-query/{useIsFetching,useIsMutating}): add tests for not processing cache updates while disconnected --- .../src/__tests__/useIsFetching.test.ts | 42 +++++++++++++++++++ .../src/__tests__/useIsMutating.test.ts | 40 ++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/packages/lit-query/src/__tests__/useIsFetching.test.ts b/packages/lit-query/src/__tests__/useIsFetching.test.ts index 4271259cbc..167ba6dcf2 100644 --- a/packages/lit-query/src/__tests__/useIsFetching.test.ts +++ b/packages/lit-query/src/__tests__/useIsFetching.test.ts @@ -158,6 +158,48 @@ describe('useIsFetching', () => { expect(host.shadowRoot).toHaveTextContent('fetching: 0') }) + it('should not process query cache updates while disconnected', async () => { + const key = queryKey() + + class Producer extends LitElement { + readonly query = createQueryController( + this, + { + queryKey: key, + queryFn: () => sleep(10).then(() => 'data'), + }, + queryClient, + ) + } + customElements.define(generateElementName(), Producer) + + class Host extends LitElement { + updatesRequested = 0 + + readonly isFetching = useIsFetching(this, {}, queryClient) + + override requestUpdate( + ...args: Parameters + ): void { + this.updatesRequested += 1 + super.requestUpdate(...args) + } + } + customElements.define(generateElementName(), Host) + const host = new Host() + container.append(host) + await host.updateComplete + + host.remove() + await host.updateComplete + const updatesAfterDisconnect = host.updatesRequested + + container.append(new Producer()) + + await vi.advanceTimersByTimeAsync(10) + expect(host.updatesRequested).toBe(updatesAfterDisconnect) + }) + it('should count all fetching queries when filters are omitted', async () => { const key1 = queryKey() const key2 = queryKey() diff --git a/packages/lit-query/src/__tests__/useIsMutating.test.ts b/packages/lit-query/src/__tests__/useIsMutating.test.ts index 54923674f7..f18b0501eb 100644 --- a/packages/lit-query/src/__tests__/useIsMutating.test.ts +++ b/packages/lit-query/src/__tests__/useIsMutating.test.ts @@ -159,6 +159,46 @@ describe('useIsMutating', () => { expect(host.shadowRoot).toHaveTextContent('mutating: 0') }) + it('should not process mutation cache updates while disconnected', async () => { + class Producer extends LitElement { + readonly mutation = createMutationController( + this, + { + mutationFn: (value: number) => sleep(10).then(() => value + 10), + }, + queryClient, + ) + } + customElements.define(generateElementName(), Producer) + + class Host extends LitElement { + updatesRequested = 0 + + readonly isMutating = useIsMutating(this, {}, queryClient) + + override requestUpdate( + ...args: Parameters + ): void { + this.updatesRequested += 1 + super.requestUpdate(...args) + } + } + customElements.define(generateElementName(), Host) + const producer = new Producer() + const host = new Host() + container.append(producer, host) + const { mutation } = producer + await host.updateComplete + + host.remove() + await host.updateComplete + const updatesAfterDisconnect = host.updatesRequested + + mutation.mutate(1) + await vi.advanceTimersByTimeAsync(10) + expect(host.updatesRequested).toBe(updatesAfterDisconnect) + }) + it('should count all mutating mutations when filters are omitted', async () => { class Producer extends LitElement { readonly mutation1 = createMutationController(this, {