Skip to content

Commit

Permalink
Converted inferno-server tests to TSX github#1632
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Apr 13, 2024
1 parent 7ea5df9 commit 6b2714a
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { streamQueueAsString } from 'inferno-server';
import concatStream from 'concat-stream';
import { createElement } from 'inferno-create-element';

class StatefulComponent extends Component {
class StatefulComponent extends Component<{value: string}> {
render() {
return createElement('span', null, `stateless ${this.props.value}!`);
}
Expand All @@ -14,9 +14,9 @@ function WrappedInput(props) {
return <input type="text" value={props.value} />;
}

class StatefulPromiseComponent extends Component {
class StatefulPromiseComponent extends Component<{index: number, value?: unknown}> {
async getInitialProps() {
return await new Promise((resolve, reject) => {
return await new Promise((resolve) => {
// Waits incremenetally for each subindex
setTimeout(() => {
resolve({
Expand All @@ -35,9 +35,9 @@ class StatefulPromiseComponent extends Component {
}
}

class StatefulHierchicalPromiseComponent extends Component {
class StatefulHierchicalPromiseComponent extends Component<{index: number, value?: unknown}> {
async getInitialProps() {
return await new Promise((resolve, reject) => {
return await new Promise((resolve) => {
// Waits incremenetally for each subindex
setTimeout(() => {
resolve({
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
},
{
description: 'should render a stateful component with promise',
template: (value) =>
template: () =>
createElement(
'div',
null,
Expand All @@ -257,7 +257,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
{
description:
'should render a stateful component with promise as hierarchy',
template: (value) =>
template: () =>
createElement(StatefulHierchicalPromiseComponent, { index: 1 }),
result: [
[
Expand All @@ -276,7 +276,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
},
{
description: 'should render a stack of stateful component with promise',
template: (value) =>
template: () =>
createElement(
'div',
null,
Expand Down Expand Up @@ -357,6 +357,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
},
{
description: 'should ignore children as props',
// @ts-expect-error
template: () => <p children="foo">foo</p>,
result: '<p>foo</p>',
},
Expand Down Expand Up @@ -423,7 +424,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
description: 'Should not render null styles',
template: () => (
<div
style={{ 'background-color': null, 'border-bottom-color': null }}
style={{ 'background-color': null as any, 'border-bottom-color': null as any }}
/>
),
result: '<div></div>',
Expand Down Expand Up @@ -461,17 +462,17 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
},
{
description: 'Should not render empty style attribute #3',
template: () => <div style={false} />,
template: () => <div style={false as any} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #4',
template: () => <div style={0} />,
template: () => <div style={0 as any} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #5',
template: () => <div style={true} />,
template: () => <div style={true as any} />,
result: '<div></div>',
},
{
Expand All @@ -481,7 +482,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
},
{
description: 'Should render div className as number',
template: () => <div className={123} />,
template: () => <div className={123 as any} />,
result: '<div class="123"></div>',
},
{
Expand Down Expand Up @@ -536,16 +537,16 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
];

for (const test of testEntries) {
it(test.description, async (done) => {
it(test.description, async () => {
const vDom = test.template('foo');
return await streamPromise(vDom).then(function (output) {

return streamPromise(vDom).then((value: string | string[]) => {
if (typeof test.result === 'object') {
expect(output[0]).toEqual(test.result[0]);
expect(output[1]).toBe(test.result[1]);
expect(value[0]).toEqual(test.result[0]);
expect(value[1]).toBe(test.result[1]);
} else {
expect(output[1]).toBe(test.result);
expect(value[1]).toBe(test.result);
}
done();
});
});
}
Expand All @@ -568,7 +569,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
}

render() {
return <div>{this.state.foo}</div>;
return <div>{this.state?.foo}</div>;
}
}

Expand All @@ -590,7 +591,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
render() {
return (
<div>
{this.state.foo}
{this.state?.foo}
<Another />
</div>
);
Expand All @@ -605,10 +606,10 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
});

describe('misc', () => {
it('Should render single text node using state', async (done) => {
it('Should render single text node using state', async () => {
class Foobar extends Component {
render() {
return this.state.text;
return this.state!.text;
}

componentWillMount() {
Expand All @@ -624,14 +625,13 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
</div>,
).then(function (output) {
expect(output[1]).toEqual('<div>foo</div>');
done();
});
});

it('Should render single (number) text node using state', async (done) => {
it('Should render single (number) text node using state', async () => {
class Foobar extends Component {
render() {
return this.state.text;
return this.state!.text;
}

componentWillMount() {
Expand All @@ -647,11 +647,10 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
</div>,
).then(function (output) {
expect(output[1]).toEqual('<div>331</div>');
done();
});
});

it('Should render single text node Functional Component', async (done) => {
it('Should render single text node Functional Component', async () => {
function Foobar() {
return 'foo';
}
Expand All @@ -662,11 +661,10 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
</div>,
).then(function (output) {
expect(output[1]).toEqual('<div>foo</div>');
done();
});
});

it('Should render single (number) text node Functional Component', async (done) => {
it('Should render single (number) text node Functional Component', async () => {
function Foobar() {
return 0;
}
Expand All @@ -677,13 +675,13 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
</div>,
).then(function (output) {
expect(output[1]).toEqual('<div>0</div>');
done();
});
});

it('Should render checked attribute for input when there is no checked in props', async (done) => {
it('Should render checked attribute for input when there is no checked in props', async () => {
class Foobar extends Component {
render() {
// @ts-expect-error
return <input count={1} type="checkbox" defaultChecked={true} />;
}
}
Expand All @@ -696,11 +694,10 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
expect(output[1]).toEqual(
'<div><input count="1" type="checkbox" checked="true"></div>',
);
done();
});
});

it('Should render comment when component returns invalid node', async (done) => {
it('Should render comment when component returns invalid node', async () => {
function Foobar() {
return null;
}
Expand All @@ -711,11 +708,10 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
</div>,
).then(function (output) {
expect(output[1]).toEqual('<div><!--!--></div>');
done();
});
});

it('Should render single text node Class Component', async (done) => {
it('Should render single text node Class Component', async () => {
class Foobar extends Component {
render() {
return null;
Expand All @@ -728,11 +724,10 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
</div>,
).then(function (output) {
expect(output[1]).toEqual('<div><!--!--></div>');
done();
});
});

it('Should be possible to use getDerivedStateFromProps', async (done) => {
it('Should be possible to use getDerivedStateFromProps', async () => {
class Test extends Component {
constructor(props) {
super(props);
Expand All @@ -742,27 +737,27 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
};
}

static getDerivedStateFromProps(props, state) {
static getDerivedStateFromProps(_props, state) {
return {
value: state.value + 1,
};
}

render() {
return <div>{this.state.value}</div>;
return <div>{this.state!.value}</div>;
}
}

return await streamPromise(<Test />).then(function (output) {
expect(output[1]).toEqual('<div>1</div>');
done();
});
});
});
});

async function streamPromise(dom) {
return await new Promise(function (res, rej) {
const chunks = [];
return await new Promise(function (res: (value: string[]) => void, rej) {
const chunks: string[] = [];
streamQueueAsString(dom)
.on('error', rej)
.on('data', (chunk) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('SSR Root Creation Streams - (non-JSX)', () => {
});

it('should throw with invalid children', async () => {
const test = (value) => createElement('a', null, true);
const test = () => createElement('a', null, true);

return await streamPromise(test('foo')).catch((err) => {
return await streamPromise(test()).catch((err) => {
expect(err.toString()).toBe('Error: invalid component');
});
});
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('SSR Root Creation Streams - (non-JSX)', () => {
}

render() {
return createElement('div', null, this.state.foo);
return createElement('div', null, this.state!.foo);
}
}

Expand All @@ -81,7 +81,7 @@ describe('SSR Root Creation Streams - (non-JSX)', () => {

render() {
return createElement('div', null, [
this.state.foo,
this.state!.foo,
createElement(Another),
]);
}
Expand Down
Loading

0 comments on commit 6b2714a

Please sign in to comment.