Skip to content

Commit

Permalink
feat: more tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 27, 2023
1 parent 6be0c2e commit 4569910
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"bench": "tsc && node --expose-gc dist/benchmark",
"format": "prettier --write .",
"test": "tsc && c8 --reporter lcov node --trace-warnings --test dist/test",
"test": "tsc && c8 --reporter lcov --reporter text node --trace-warnings --test dist/test",
"test:only": "tsc; node --trace-warnings --test-only --test dist/test"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let root;
try {
root = Function('return this')();
/* c8 ignore next 3 */
} catch (_) {
root = window;
}
Expand Down
2 changes: 1 addition & 1 deletion suspense.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function Suspense(props) {
const fallback = contentsToString([props.fallback]);

if (!props.children) {
return fallback;
return '';
}

const children = contentsToString([props.children]);
Expand Down
32 changes: 31 additions & 1 deletion test/error-boundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'node:assert';
import { describe, it } from 'node:test';
import { setTimeout } from 'timers/promises';
import Html from '../';
import { ErrorBoundary } from '../error-boundary';
import { ErrorBoundary, isTimeoutError } from '../error-boundary';

describe('Error Boundary', () => {
it('should render error boundary', async () => {
Expand Down Expand Up @@ -63,4 +63,34 @@ describe('Error Boundary', () => {

assert.equal(html, <div>2</div>);
});

it('Catches timed out promise', async () => {
const html = await (
<>
<ErrorBoundary
catch={(err) => {
assert.ok(isTimeoutError(err));
return <div>1</div>;
}}
timeout={5}
>
{setTimeout(10, <div>2</div>)}
</ErrorBoundary>
</>
);

assert.equal(html, '<div>1</div>');
});

it('doesnt do nothing on sync children', () => {
const html = (
<>
<ErrorBoundary catch={<div>1</div>}>
<div>2</div>
</ErrorBoundary>
</>
);

assert.equal(html, '<div>2</div>');
});
});
49 changes: 49 additions & 0 deletions test/suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,29 @@ describe('Suspense', () => {

assert.ok(stream.closed);
});

it('tests suspense without children', async () => {
assert.equal(
await renderToString((r) => (
//@ts-expect-error - testing invalid children
<Suspense rid={r} fallback={<div>1</div>} />
)),
''
);
});

it('throws if no stream is used', () => {
SUSPENSE_ROOT.enabled = true;

assert.throws(
() => (
<Suspense rid={1} fallback={'1'}>
{Promise.resolve(2)}
</Suspense>
),
/Suspense resource closed before all suspense components were resolved./
);
});
});

describe('Suspense errors', () => {
Expand Down Expand Up @@ -690,4 +713,30 @@ describe('Suspense errors', () => {
</>
);
});

it('tests suspense with error boundary', async () => {
const err = new Error('component failed');

// Sync does not needs autoScript
assert.equal(
await renderToString((r) => (
<Suspense rid={r} fallback={<div>1</div>} catch={<div>3</div>}>
{Promise.reject(err)}
</Suspense>
)),
<>
<div id="B:1" data-sf>
<div>1</div>
</div>

{SuspenseScript}
<template id="N:1" data-sr>
<div>3</div>
</template>
<script id="S:1" data-ss>
$RC(1)
</script>
</>
);
});
});

0 comments on commit 4569910

Please sign in to comment.