Skip to content

Commit

Permalink
Merge branch 'master' into typed-defaultProps
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Sep 10, 2018
2 parents fc03c9a + 62c04e0 commit d1974c7
Show file tree
Hide file tree
Showing 5 changed files with 461 additions and 179 deletions.
2 changes: 1 addition & 1 deletion src/preact.d.ts
Expand Up @@ -114,7 +114,7 @@ declare namespace preact {

function render(node: ComponentChild, parent: Element | Document | ShadowRoot | DocumentFragment, mergeWith?: Element): Element;
function rerender(): void;
function cloneElement(element: JSX.Element, props: any): JSX.Element;
function cloneElement(element: JSX.Element, props: any, ...children: ComponentChildren[]): JSX.Element;

var options: {
syncComponentUpdates?: boolean;
Expand Down
14 changes: 13 additions & 1 deletion src/preact.js.flow
Expand Up @@ -9,5 +9,17 @@ declare function render(vnode: Node, parent: Element, toReplace?: Element): Elem
export { h, createElement, cloneElement, Component, render };
export default { h, createElement, cloneElement, Component, render };

declare type VNode<P> = {
nodeName: string | Function,
children: Array<VNode<P> | string>,
key?: string | number | void,
attributes: P,
};

declare export function rerender(): void;
declare export var options: Object;
declare export var options: {
syncComponentUpdates?: boolean,
vnode?: (vnode: VNode<any>) => void,
debounceRendering?: (rerender: () => void) => void,
event?: (event: Event) => Event | void,
};
6 changes: 0 additions & 6 deletions test/browser/components.js
Expand Up @@ -151,12 +151,6 @@ describe('Components', () => {
expect(scratch.innerHTML).to.equal('');
});

// Test for #651
it('should set enumerable boolean attribute', () => {
render(<input spellcheck={false} />, scratch);
expect(scratch.firstChild.spellcheck).to.equal(false);
});

// Test for Issue #73
it('should remove orphaned elements replaced by Components', () => {
class Comp extends Component {
Expand Down
140 changes: 140 additions & 0 deletions test/browser/lifecycle.js
Expand Up @@ -1019,6 +1019,146 @@ describe('Lifecycle methods', () => {
value: 4
});
});

it('prevState argument should be the same object if state doesn\'t change', () => {
let changeProps, cduPrevState, cduCurrentState;

class PropsProvider extends Component {
constructor() {
super();
this.state = { value: 0 };
changeProps = this.changeReceiverProps.bind(this);
}
changeReceiverProps() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
render() {
return <PropsReceiver value={this.state.value} />;
}
}

class PropsReceiver extends Component {
componentDidUpdate(prevProps, prevState) {
cduPrevState = prevState;
cduCurrentState = this.state;
}
render({ value }) {
return <div>{value}</div>;
}
}

render(<PropsProvider />, scratch);

changeProps();
rerender();

expect(cduPrevState).to.equal(cduCurrentState);
});

it('prevState argument should be a different object if state does change', () => {
let updateState, cduPrevState, cduCurrentState;

class Foo extends Component {
constructor() {
super();
this.state = { value: 0 };
updateState = this.updateState.bind(this);
}
updateState() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
componentDidUpdate(prevProps, prevState) {
cduPrevState = prevState;
cduCurrentState = this.state;
}
render() {
return <div>{this.state.value}</div>;
}
}

render(<Foo />, scratch);

updateState();
rerender();

expect(cduPrevState).to.not.equal(cduCurrentState);
});

it('prevProps argument should be the same object if props don\'t change', () => {
let updateState, cduPrevProps, cduCurrentProps;

class Foo extends Component {
constructor() {
super();
this.state = { value: 0 };
updateState = this.updateState.bind(this);
}
updateState() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
componentDidUpdate(prevProps) {
cduPrevProps = prevProps;
cduCurrentProps = this.props;
}
render() {
return <div>{this.state.value}</div>;
}
}

render(<Foo />, scratch);

updateState();
rerender();

expect(cduPrevProps).to.equal(cduCurrentProps);
});

it('prevProps argument should be a different object if props do change', () => {
let changeProps, cduPrevProps, cduCurrentProps;

class PropsProvider extends Component {
constructor() {
super();
this.state = { value: 0 };
changeProps = this.changeReceiverProps.bind(this);
}
changeReceiverProps() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
render() {
return <PropsReceiver value={this.state.value} />;
}
}

class PropsReceiver extends Component {
componentDidUpdate(prevProps) {
cduPrevProps = prevProps;
cduCurrentProps = this.props;
}
render({ value }) {
return <div>{value}</div>;
}
}

render(<PropsProvider />, scratch);

changeProps();
rerender();

expect(cduPrevProps).to.not.equal(cduCurrentProps);
});
});


Expand Down

0 comments on commit d1974c7

Please sign in to comment.