Page MenuHomeSealhub

jsx.test.tsx
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

jsx.test.tsx

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
import assert, { deepEqual } from "assert";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { TempstreamJSX, tempstream } from "../src/index";
import streamToString from "../src/tostring";
import { prettify } from "./prettify";
describe("jsx tempstream", () => {
it("renders properly in the basic case", async () => {
const Component = () => <div>Hello Wolrd</div>;
const component = Component();
const result = await streamToString(component);
deepEqual(prettify(result), "<div>Hello Wolrd</div>\n");
});
it("renders properly with properties", async () => {
const Component = () => (
<div id="topbar" class="navbar">
Hello Wolrd
</div>
);
const component = Component();
const result = await streamToString(component);
deepEqual(
prettify(result),
`<div id="topbar" class="navbar">Hello Wolrd</div>\n`
);
});
it("renders properly with nested components", async () => {
const Component1 = () => <h1>Hello Wolrd</h1>;
const Component2 = () => (
<div>
<Component1 />
<p>Hi!</p>
</div>
);
const component = Component2();
const result = await streamToString(component);
deepEqual(
prettify(result),
`<div>\n <h1>Hello Wolrd</h1>\n <p>Hi!</p>\n</div>\n`
);
});
it("renders properly with some logic", async () => {
const Component1 = ({
data,
}: {
data: {
title: string;
description: string;
iteration: number;
annotations: string[];
};
}) => {
const title = `${data.iteration}. ${data.title}`;
return (
<div id="card">
<h1>{title}</h1>
<p>{data.description}</p>
{data.annotations.map((annotation) => (
<p>{annotation}</p>
))}
</div>
);
};
const Component2 = () => (
<div id="container">
<Component1
data={{
title: "Hello",
description: "Description.",
iteration: 4,
annotations: ["First", "Second"],
}}
/>
</div>
);
const component = Component2();
const result = await streamToString(component);
const expected =
'<div id="container">\n' +
' <div id="card">\n' +
" <h1>4. Hello</h1>\n" +
" <p>Description.</p>\n" +
" <p>First</p>\n" +
" <p>Second</p>\n" +
" </div>\n" +
"</div>\n";
deepEqual(prettify(result), expected);
});
it("Typescript should be fine with promises inside of attributes", async () => {
const attributeValue = Promise.resolve("test");
const Component = () => <div class={attributeValue}></div>;
const component = Component();
const result = await streamToString(component);
deepEqual(prettify(result), '<div class="test"></div>\n');
});
it("Should render boolean attribuites properly", async () => {
const ComponentChecked = () => <input type="checkbox" checked={true} />;
const ComponentUnChecked = () => (
<input type="checkbox" checked={false} />
);
const checkedResults = await streamToString(ComponentChecked());
const unCheckedResults = await streamToString(ComponentUnChecked());
const expectedChecked = '<input type="checkbox" checked />\n';
const expectedUnChecked = '<input type="checkbox" />\n';
deepEqual(prettify(checkedResults), expectedChecked);
deepEqual(prettify(unCheckedResults), expectedUnChecked);
});
it("Should render void tag properly", async () => {
const ComponentChecked = () => <input type="checkbox" />;
const result = await streamToString(ComponentChecked());
const expectedResult = '<input type="checkbox" />\n';
deepEqual(prettify(result), expectedResult);
});
it("handles arrays properly", async () => {
const document = (
<div>
{[<strong></strong>]}
<span></span>
</div>
);
const result = await streamToString(document);
const expectedResult = /* HTML */ `<div>
<strong></strong><span></span>
</div>`;
deepEqual(prettify(result), prettify(expectedResult));
});
it("should support providing classnames as object", async () => {
const DivUsingList = () => (
<div class={["test", { test2: false }, { test3: true }]}></div>
);
let result = await streamToString(DivUsingList());
let expectedResult = '<div class="test test3"></div>\n';
deepEqual(prettify(result), expectedResult);
const DivUsingObject = () => (
<div class={{ test: true, test2: false, test3: true }}></div>
);
result = await streamToString(DivUsingObject());
expectedResult = '<div class="test test3"></div>\n';
deepEqual(prettify(result), expectedResult);
});
it("handles null values properly", async () => {
const stream = tempstream`<div>${null}</div>`;
const result = await streamToString(stream);
assert.strictEqual(result, "<div></div>");
});
it("Doesn't insert unnecessary whitespace that changes the semantics of html", async () => {
const stream = (
<div>
{21}:{37}
</div>
);
const result = await streamToString(stream);
assert.strictEqual(prettify(result), prettify("<div>21:37</div>"));
});
it("Does insert whitespace where it matters", async () => {
const stream = <input checked type="checkbox" />;
const result = await streamToString(stream);
assert.strictEqual(
prettify(result),
prettify(`<input checked type="checkbox" />`)
);
});
});

File Metadata

Mime Type
application/javascript
Expires
Fri, Nov 27, 18:17 (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1229337
Default Alt Text
jsx.test.tsx (5 KB)

Event Timeline