Skip to content

Commit a631add

Browse files
committed
test(broadcast-client): also cover the query.setState throw path
The existing regression test only exercised queryCache.build (a brand-new remote query hash). Add a case where the incoming message targets an existing query, so an exception during query.setState is exercised too. Addresses CodeRabbit review feedback on #11242.
1 parent 857432a commit a631add

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

  • packages/query-broadcast-client-experimental/src/__tests__

‎packages/query-broadcast-client-experimental/src/__tests__/index.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,38 @@ describe('broadcastQueryClient', () => {
8484

8585
expect(mockPostMessage).toHaveBeenCalled()
8686
})
87+
88+
it('should keep broadcasting local changes after query.setState throws for an existing query', () => {
89+
broadcastQueryClient({
90+
queryClient,
91+
broadcastChannel: 'test_channel',
92+
})
93+
94+
// Populate the cache with an existing query so the incoming message
95+
// below is applied via `query.setState` rather than `queryCache.build`.
96+
queryClient.setQueryData(['existing'], { value: 0 })
97+
const existingQuery = queryCache.find({ queryKey: ['existing'] })!
98+
99+
const explodingUnsubscribe = queryCache.subscribe(() => {
100+
throw new Error('boom from an unrelated cache listener')
101+
})
102+
103+
expect(() => {
104+
lastCreatedChannel.onmessage?.({
105+
type: 'updated',
106+
queryHash: existingQuery.queryHash,
107+
queryKey: ['existing'],
108+
state: { data: 2 },
109+
})
110+
}).toThrow('boom')
111+
112+
explodingUnsubscribe()
113+
mockPostMessage.mockClear()
114+
115+
queryClient.setQueryData(['local2'], { value: 1 })
116+
117+
expect(mockPostMessage).toHaveBeenCalled()
118+
})
87119
})
88120

89121
describe('postMessage error handling', () => {

0 commit comments

Comments
 (0)