Skip to content

Commit

Permalink
[ts] improve the return type of the render
Browse files Browse the repository at this point in the history
  • Loading branch information
valotas committed Apr 30, 2018
1 parent a1c8f6a commit da53bfd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/preact.d.ts
Expand Up @@ -98,7 +98,7 @@ declare namespace preact {

forceUpdate(callback?: () => void): void;

abstract render(props?: RenderableProps<P>, state?: Readonly<S>, context?: any): JSX.Element | null;
abstract render(props?: RenderableProps<P>, state?: Readonly<S>, context?: any): ComponentChild;
}

function h<P>(
Expand Down
32 changes: 32 additions & 0 deletions test/browser/components.js
Expand Up @@ -118,6 +118,38 @@ describe('Components', () => {
expect(clone.prototype).to.equal(instance.prototype);
});

it('should render string', () => {
class StringComponent extends Component {
render() {
return "Hi there";
}
}

render(<StringComponent />, scratch);
expect(scratch.innerHTML).to.equal('Hi there');
});

it('should render number as string', () => {
class NumberComponent extends Component {
render() {
return 42;
}
}

render(<NumberComponent />, scratch);
expect(scratch.innerHTML).to.equal('42');
});

it('should render null as empty string', () => {
class NullComponent extends Component {
render() {
return null;
}
}

render(<NullComponent />, scratch);
expect(scratch.innerHTML).to.equal('');
});

// Test for Issue #73
it('should remove orphaned elements replaced by Components', () => {
Expand Down
41 changes: 34 additions & 7 deletions test/ts/Component-test.tsx
Expand Up @@ -5,10 +5,11 @@ import {
Component,
FunctionalComponent,
ComponentConstructor,
RenderableProps
RenderableProps,
render
} from "../../src/preact";

export class ContextComponent extends Component<{ foo: string}> {
export class ContextComponent extends Component<{ foo: string }> {
getChildContext() {
return { something: 2 };
}
Expand All @@ -23,10 +24,13 @@ export interface SimpleComponentProps {
}

export interface SimpleState {
name: string | null
name: string | null;
}

export class SimpleComponent extends Component<SimpleComponentProps, SimpleState> {
export class SimpleComponent extends Component<
SimpleComponentProps,
SimpleState
> {
constructor(props: SimpleComponentProps) {
super(props);
this.state = {
Expand All @@ -50,9 +54,10 @@ export class SimpleComponent extends Component<SimpleComponentProps, SimpleState
}
}

class DestructuringRenderPropsComponent
extends Component<SimpleComponentProps, SimpleState> {

class DestructuringRenderPropsComponent extends Component<
SimpleComponentProps,
SimpleState
> {
constructor(props: SimpleComponentProps) {
super(props);
this.state = {
Expand All @@ -72,6 +77,28 @@ class DestructuringRenderPropsComponent
}
}

interface RandomChildrenComponenProps {
num?: number;
val?: string;
span?: boolean;
}

class RandomChildrenComponen extends Component<RandomChildrenComponenProps> {
render() {
const { num, val, span } = this.props;
if (num) {
return num;
}
if (val) {
return val;
}
if (span) {
return <span>hi</span>
}
return null;
}
}

describe("Component", () => {
const component = new SimpleComponent({ initialName: "da name" });

Expand Down

0 comments on commit da53bfd

Please sign in to comment.