Skip to content

Commit af1822f

Browse files
committed
test: add tests for use-query
1 parent 9638402 commit af1822f

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

packages/core/src/workloop/workloop.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
detectIsPromise,
2424
formatErrorMsg,
2525
createIndexKey,
26+
createError,
2627
trueFn,
2728
} from '../utils';
2829
import { type Scope, setRootId, $$scope, replaceScope } from '../scope';
@@ -80,19 +81,19 @@ function workLoop(isAsync: boolean): boolean | Promise<unknown> | null {
8081
if (!unit && wipFiber) {
8182
commit($scope);
8283
}
83-
} catch (err) {
84-
if (detectIsPromise(err)) {
85-
return err;
84+
} catch (error) {
85+
if (detectIsPromise(error)) {
86+
return error;
8687
} else {
8788
const emitter = $scope.getEmitter();
8889

8990
$scope.keepRoot(); // !
90-
emitter.emit('error', String(err));
91+
emitter.emit('error', createError(error));
9192

9293
if (!isAsync) {
93-
throw err;
94+
throw error;
9495
} else {
95-
logError('err', err);
96+
logError(error);
9697
}
9798

9899
return false;

packages/data/src/use-query/use-query.spec.tsx

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -291,26 +291,45 @@ describe('@data/use-query', () => {
291291
spy.mockClear();
292292
});
293293

294-
// test.only('xxx', async () => {
295-
// const DataLoader = component(() => {
296-
// const { data } = useQuery(Key.GET_DATA, ({ id }) => api.getData(id, true), {
297-
// variables: { id: 2 },
298-
// });
299-
300-
// return <div>{data}</div>;
301-
// });
302-
// const App = component(() => {
303-
// return (
304-
// <ErrorBoundary fallback={<div>ERROR!</div>}>
305-
// <DataLoader />
306-
// </ErrorBoundary>
307-
// );
308-
// });
309-
// const { renderToString } = createServerEnv();
310-
// const result = await renderToString(withProvider(<App />));
311-
312-
// expect(result).toMatchInlineSnapshot(
313-
// `"<div>ERROR!</div><script type="text/dark-state">"eyIxIjpbbnVsbCwiRXJyb3I6IG9vcHMhIl19"</script>"`,
314-
// );
315-
// });
294+
test('renders with error-boundary in the browser correctly', async () => {
295+
const DataLoader = component(() => {
296+
const { data } = useQuery(Key.GET_DATA, () => api.getData(2, true));
297+
298+
return <div>{data}</div>;
299+
});
300+
const App = component(() => {
301+
return (
302+
<ErrorBoundary fallback={<div>ERROR!</div>}>
303+
<DataLoader />
304+
</ErrorBoundary>
305+
);
306+
});
307+
308+
render(withProvider(<App />));
309+
await waitQuery();
310+
expect(host.innerHTML).toMatchInlineSnapshot(`"<div>ERROR!</div>"`);
311+
});
312+
313+
test('renders with error-boundary on the server correctly', async () => {
314+
const DataLoader = component(() => {
315+
const { data, error } = useQuery(Key.GET_DATA, () => api.getData(2, true));
316+
317+
if (error) return <div>{error}</div>;
318+
319+
return <div>{data}</div>;
320+
});
321+
const App = component(() => {
322+
return (
323+
<ErrorBoundary fallback={<div>it renders only on the client</div>}>
324+
<DataLoader />
325+
</ErrorBoundary>
326+
);
327+
});
328+
const { renderToString } = createServerEnv();
329+
const result = await renderToString(withProvider(<App />));
330+
331+
expect(result).toMatchInlineSnapshot(
332+
`"<div>Error: oops!</div><script type="text/dark-state">"eyIxIjpbbnVsbCwiRXJyb3I6IG9vcHMhIl19"</script>"`,
333+
);
334+
});
316335
});

0 commit comments

Comments
 (0)