From af1822fdff6f5db84f0835c93dea4253de6b9abf Mon Sep 17 00:00:00 2001 From: Alex Plex Date: Sun, 29 Dec 2024 10:13:09 +0500 Subject: [PATCH] test: add tests for use-query --- packages/core/src/workloop/workloop.ts | 13 ++-- .../data/src/use-query/use-query.spec.tsx | 63 ++++++++++++------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/packages/core/src/workloop/workloop.ts b/packages/core/src/workloop/workloop.ts index 274a829b..a3e7eeaf 100644 --- a/packages/core/src/workloop/workloop.ts +++ b/packages/core/src/workloop/workloop.ts @@ -23,6 +23,7 @@ import { detectIsPromise, formatErrorMsg, createIndexKey, + createError, trueFn, } from '../utils'; import { type Scope, setRootId, $$scope, replaceScope } from '../scope'; @@ -80,19 +81,19 @@ function workLoop(isAsync: boolean): boolean | Promise | null { if (!unit && wipFiber) { commit($scope); } - } catch (err) { - if (detectIsPromise(err)) { - return err; + } catch (error) { + if (detectIsPromise(error)) { + return error; } else { const emitter = $scope.getEmitter(); $scope.keepRoot(); // ! - emitter.emit('error', String(err)); + emitter.emit('error', createError(error)); if (!isAsync) { - throw err; + throw error; } else { - logError('err', err); + logError(error); } return false; diff --git a/packages/data/src/use-query/use-query.spec.tsx b/packages/data/src/use-query/use-query.spec.tsx index aea8c649..bb9402d7 100644 --- a/packages/data/src/use-query/use-query.spec.tsx +++ b/packages/data/src/use-query/use-query.spec.tsx @@ -291,26 +291,45 @@ describe('@data/use-query', () => { spy.mockClear(); }); - // test.only('xxx', async () => { - // const DataLoader = component(() => { - // const { data } = useQuery(Key.GET_DATA, ({ id }) => api.getData(id, true), { - // variables: { id: 2 }, - // }); - - // return
{data}
; - // }); - // const App = component(() => { - // return ( - // ERROR!}> - // - // - // ); - // }); - // const { renderToString } = createServerEnv(); - // const result = await renderToString(withProvider()); - - // expect(result).toMatchInlineSnapshot( - // `"
ERROR!
"`, - // ); - // }); + test('renders with error-boundary in the browser correctly', async () => { + const DataLoader = component(() => { + const { data } = useQuery(Key.GET_DATA, () => api.getData(2, true)); + + return
{data}
; + }); + const App = component(() => { + return ( + ERROR!}> + + + ); + }); + + render(withProvider()); + await waitQuery(); + expect(host.innerHTML).toMatchInlineSnapshot(`"
ERROR!
"`); + }); + + test('renders with error-boundary on the server correctly', async () => { + const DataLoader = component(() => { + const { data, error } = useQuery(Key.GET_DATA, () => api.getData(2, true)); + + if (error) return
{error}
; + + return
{data}
; + }); + const App = component(() => { + return ( + it renders only on the client}> + + + ); + }); + const { renderToString } = createServerEnv(); + const result = await renderToString(withProvider()); + + expect(result).toMatchInlineSnapshot( + `"
Error: oops!
"`, + ); + }); });