wallet-core/thirdparty/preact/test/browser/context.js

175 lines
5.4 KiB
JavaScript
Raw Normal View History

import { h, render, Component } from '../../src/preact';
/** @jsx h */
Squashed 'thirdparty/preact/' changes from b2d90cc..ba094e2 ba094e2 Run only local tests for pull requests (#390) e9fc3c2 Fix CI build (#386) 70a5ca3 This adds a link to preact-i18nline. (#382) 5dffd85 Merge branch 'pr-fix-build-for-windows' of https://github.com/Download/preact f14edf7 kilobits => kilobytes (#383) c193547 Test for #292 284e4aa 6.4.0 24eab2f Prevent accidental duplicate recycling of elements when swapping the base element of a component. Fixes #373. 76c5ef7 fix lint error 8008886 When swapping the base of a composed child component, update its parent's base reference. Fixes #349. fd4f21f Add an equality check prior to setting `.nodeValue` on text nodes, since Firefox (and maybe others) don't do this internally by default. Fixes #368 - thanks @zbinlin! 1555e2b Add CDNJS version badge in readme (#365) 79c8bae Disable React Developer Tools integration tests under IE (#362) 84f4eeb Refactor `linkState()` a bit to drop around 40 bytes. Coincidentally, that's the exact size of the hooks just added for DevTools... 👌 22bbfcb Little tweaks 👯 f8b326e Document how to use the React DevTools with Preact (#354) 1f4a8eb Correct "preact/devtools" type definitions (#355) 68f22eb Add React Developer Tools integration (#339) 2a7a027 Add ref and allow objects in className (#316) 4a59cca fix readme todomvc link (#345) 37ca4e0 Fixes build for Windows #343 cf93387 6.3.0 ff05818 Make `VNode.children` *always* be an Array, even when there are no children. 9b095f4 Added link to preact-layout (#342) git-subtree-dir: thirdparty/preact git-subtree-split: ba094e27b602cb16aded7dcad95f71e44b7b0476
2016-11-08 15:07:07 +01:00
const CHILDREN_MATCHER = sinon.match( v => v==null || Array.isArray(v) && !v.length , '[empty children]');
describe('context', () => {
let scratch;
before( () => {
scratch = document.createElement('div');
(document.body || document.documentElement).appendChild(scratch);
});
beforeEach( () => {
scratch.innerHTML = '';
});
after( () => {
scratch.parentNode.removeChild(scratch);
scratch = null;
});
it('should pass context to grandchildren', () => {
const CONTEXT = { a:'a' };
const PROPS = { b:'b' };
// let inner;
class Outer extends Component {
getChildContext() {
return CONTEXT;
}
render(props) {
return <div><Inner {...props} /></div>;
}
}
sinon.spy(Outer.prototype, 'getChildContext');
class Inner extends Component {
// constructor() {
// super();
// inner = this;
// }
shouldComponentUpdate() { return true; }
componentWillReceiveProps() {}
componentWillUpdate() {}
componentDidUpdate() {}
render(props, state, context) {
return <div>{ context && context.a }</div>;
}
}
sinon.spy(Inner.prototype, 'shouldComponentUpdate');
sinon.spy(Inner.prototype, 'componentWillReceiveProps');
sinon.spy(Inner.prototype, 'componentWillUpdate');
sinon.spy(Inner.prototype, 'componentDidUpdate');
sinon.spy(Inner.prototype, 'render');
render(<Outer />, scratch, scratch.lastChild);
expect(Outer.prototype.getChildContext).to.have.been.calledOnce;
// initial render does not invoke anything but render():
Squashed 'thirdparty/preact/' changes from b2d90cc..ba094e2 ba094e2 Run only local tests for pull requests (#390) e9fc3c2 Fix CI build (#386) 70a5ca3 This adds a link to preact-i18nline. (#382) 5dffd85 Merge branch 'pr-fix-build-for-windows' of https://github.com/Download/preact f14edf7 kilobits => kilobytes (#383) c193547 Test for #292 284e4aa 6.4.0 24eab2f Prevent accidental duplicate recycling of elements when swapping the base element of a component. Fixes #373. 76c5ef7 fix lint error 8008886 When swapping the base of a composed child component, update its parent's base reference. Fixes #349. fd4f21f Add an equality check prior to setting `.nodeValue` on text nodes, since Firefox (and maybe others) don't do this internally by default. Fixes #368 - thanks @zbinlin! 1555e2b Add CDNJS version badge in readme (#365) 79c8bae Disable React Developer Tools integration tests under IE (#362) 84f4eeb Refactor `linkState()` a bit to drop around 40 bytes. Coincidentally, that's the exact size of the hooks just added for DevTools... 👌 22bbfcb Little tweaks 👯 f8b326e Document how to use the React DevTools with Preact (#354) 1f4a8eb Correct "preact/devtools" type definitions (#355) 68f22eb Add React Developer Tools integration (#339) 2a7a027 Add ref and allow objects in className (#316) 4a59cca fix readme todomvc link (#345) 37ca4e0 Fixes build for Windows #343 cf93387 6.3.0 ff05818 Make `VNode.children` *always* be an Array, even when there are no children. 9b095f4 Added link to preact-layout (#342) git-subtree-dir: thirdparty/preact git-subtree-split: ba094e27b602cb16aded7dcad95f71e44b7b0476
2016-11-08 15:07:07 +01:00
expect(Inner.prototype.render).to.have.been.calledWith({ children:CHILDREN_MATCHER }, {}, CONTEXT);
CONTEXT.foo = 'bar';
render(<Outer {...PROPS} />, scratch, scratch.lastChild);
expect(Outer.prototype.getChildContext).to.have.been.calledTwice;
Squashed 'thirdparty/preact/' changes from b2d90cc..ba094e2 ba094e2 Run only local tests for pull requests (#390) e9fc3c2 Fix CI build (#386) 70a5ca3 This adds a link to preact-i18nline. (#382) 5dffd85 Merge branch 'pr-fix-build-for-windows' of https://github.com/Download/preact f14edf7 kilobits => kilobytes (#383) c193547 Test for #292 284e4aa 6.4.0 24eab2f Prevent accidental duplicate recycling of elements when swapping the base element of a component. Fixes #373. 76c5ef7 fix lint error 8008886 When swapping the base of a composed child component, update its parent's base reference. Fixes #349. fd4f21f Add an equality check prior to setting `.nodeValue` on text nodes, since Firefox (and maybe others) don't do this internally by default. Fixes #368 - thanks @zbinlin! 1555e2b Add CDNJS version badge in readme (#365) 79c8bae Disable React Developer Tools integration tests under IE (#362) 84f4eeb Refactor `linkState()` a bit to drop around 40 bytes. Coincidentally, that's the exact size of the hooks just added for DevTools... 👌 22bbfcb Little tweaks 👯 f8b326e Document how to use the React DevTools with Preact (#354) 1f4a8eb Correct "preact/devtools" type definitions (#355) 68f22eb Add React Developer Tools integration (#339) 2a7a027 Add ref and allow objects in className (#316) 4a59cca fix readme todomvc link (#345) 37ca4e0 Fixes build for Windows #343 cf93387 6.3.0 ff05818 Make `VNode.children` *always* be an Array, even when there are no children. 9b095f4 Added link to preact-layout (#342) git-subtree-dir: thirdparty/preact git-subtree-split: ba094e27b602cb16aded7dcad95f71e44b7b0476
2016-11-08 15:07:07 +01:00
let props = { children: CHILDREN_MATCHER, ...PROPS };
expect(Inner.prototype.shouldComponentUpdate).to.have.been.calledOnce.and.calledWith(props, {}, CONTEXT);
expect(Inner.prototype.componentWillReceiveProps).to.have.been.calledWith(props, CONTEXT);
expect(Inner.prototype.componentWillUpdate).to.have.been.calledWith(props, {});
expect(Inner.prototype.componentDidUpdate).to.have.been.calledWith({ children:CHILDREN_MATCHER }, {});
expect(Inner.prototype.render).to.have.been.calledWith(props, {}, CONTEXT);
/* Future:
* Newly created context objects are *not* currently cloned.
* This test checks that they *are* cloned.
*/
// Inner.prototype.render.reset();
// CONTEXT.foo = 'baz';
// inner.forceUpdate();
// expect(Inner.prototype.render).to.have.been.calledWith(PROPS, {}, { a:'a', foo:'bar' });
});
it('should pass context to direct children', () => {
const CONTEXT = { a:'a' };
const PROPS = { b:'b' };
class Outer extends Component {
getChildContext() {
return CONTEXT;
}
render(props) {
return <Inner {...props} />;
}
}
sinon.spy(Outer.prototype, 'getChildContext');
class Inner extends Component {
shouldComponentUpdate() { return true; }
componentWillReceiveProps() {}
componentWillUpdate() {}
componentDidUpdate() {}
render(props, state, context) {
return <div>{ context && context.a }</div>;
}
}
sinon.spy(Inner.prototype, 'shouldComponentUpdate');
sinon.spy(Inner.prototype, 'componentWillReceiveProps');
sinon.spy(Inner.prototype, 'componentWillUpdate');
sinon.spy(Inner.prototype, 'componentDidUpdate');
sinon.spy(Inner.prototype, 'render');
render(<Outer />, scratch, scratch.lastChild);
expect(Outer.prototype.getChildContext).to.have.been.calledOnce;
// initial render does not invoke anything but render():
Squashed 'thirdparty/preact/' changes from b2d90cc..ba094e2 ba094e2 Run only local tests for pull requests (#390) e9fc3c2 Fix CI build (#386) 70a5ca3 This adds a link to preact-i18nline. (#382) 5dffd85 Merge branch 'pr-fix-build-for-windows' of https://github.com/Download/preact f14edf7 kilobits => kilobytes (#383) c193547 Test for #292 284e4aa 6.4.0 24eab2f Prevent accidental duplicate recycling of elements when swapping the base element of a component. Fixes #373. 76c5ef7 fix lint error 8008886 When swapping the base of a composed child component, update its parent's base reference. Fixes #349. fd4f21f Add an equality check prior to setting `.nodeValue` on text nodes, since Firefox (and maybe others) don't do this internally by default. Fixes #368 - thanks @zbinlin! 1555e2b Add CDNJS version badge in readme (#365) 79c8bae Disable React Developer Tools integration tests under IE (#362) 84f4eeb Refactor `linkState()` a bit to drop around 40 bytes. Coincidentally, that's the exact size of the hooks just added for DevTools... 👌 22bbfcb Little tweaks 👯 f8b326e Document how to use the React DevTools with Preact (#354) 1f4a8eb Correct "preact/devtools" type definitions (#355) 68f22eb Add React Developer Tools integration (#339) 2a7a027 Add ref and allow objects in className (#316) 4a59cca fix readme todomvc link (#345) 37ca4e0 Fixes build for Windows #343 cf93387 6.3.0 ff05818 Make `VNode.children` *always* be an Array, even when there are no children. 9b095f4 Added link to preact-layout (#342) git-subtree-dir: thirdparty/preact git-subtree-split: ba094e27b602cb16aded7dcad95f71e44b7b0476
2016-11-08 15:07:07 +01:00
expect(Inner.prototype.render).to.have.been.calledWith({ children: CHILDREN_MATCHER }, {}, CONTEXT);
CONTEXT.foo = 'bar';
render(<Outer {...PROPS} />, scratch, scratch.lastChild);
expect(Outer.prototype.getChildContext).to.have.been.calledTwice;
Squashed 'thirdparty/preact/' changes from b2d90cc..ba094e2 ba094e2 Run only local tests for pull requests (#390) e9fc3c2 Fix CI build (#386) 70a5ca3 This adds a link to preact-i18nline. (#382) 5dffd85 Merge branch 'pr-fix-build-for-windows' of https://github.com/Download/preact f14edf7 kilobits => kilobytes (#383) c193547 Test for #292 284e4aa 6.4.0 24eab2f Prevent accidental duplicate recycling of elements when swapping the base element of a component. Fixes #373. 76c5ef7 fix lint error 8008886 When swapping the base of a composed child component, update its parent's base reference. Fixes #349. fd4f21f Add an equality check prior to setting `.nodeValue` on text nodes, since Firefox (and maybe others) don't do this internally by default. Fixes #368 - thanks @zbinlin! 1555e2b Add CDNJS version badge in readme (#365) 79c8bae Disable React Developer Tools integration tests under IE (#362) 84f4eeb Refactor `linkState()` a bit to drop around 40 bytes. Coincidentally, that's the exact size of the hooks just added for DevTools... 👌 22bbfcb Little tweaks 👯 f8b326e Document how to use the React DevTools with Preact (#354) 1f4a8eb Correct "preact/devtools" type definitions (#355) 68f22eb Add React Developer Tools integration (#339) 2a7a027 Add ref and allow objects in className (#316) 4a59cca fix readme todomvc link (#345) 37ca4e0 Fixes build for Windows #343 cf93387 6.3.0 ff05818 Make `VNode.children` *always* be an Array, even when there are no children. 9b095f4 Added link to preact-layout (#342) git-subtree-dir: thirdparty/preact git-subtree-split: ba094e27b602cb16aded7dcad95f71e44b7b0476
2016-11-08 15:07:07 +01:00
let props = { children: CHILDREN_MATCHER, ...PROPS };
expect(Inner.prototype.shouldComponentUpdate).to.have.been.calledOnce.and.calledWith(props, {}, CONTEXT);
expect(Inner.prototype.componentWillReceiveProps).to.have.been.calledWith(props, CONTEXT);
expect(Inner.prototype.componentWillUpdate).to.have.been.calledWith(props, {});
expect(Inner.prototype.componentDidUpdate).to.have.been.calledWith({ children: CHILDREN_MATCHER }, {});
expect(Inner.prototype.render).to.have.been.calledWith(props, {}, CONTEXT);
// make sure render() could make use of context.a
expect(Inner.prototype.render).to.have.returned(sinon.match({ children:['a'] }));
});
it('should preserve existing context properties when creating child contexts', () => {
let outerContext = { outer:true },
innerContext = { inner:true };
class Outer extends Component {
getChildContext() {
return { outerContext };
}
render() {
return <div><Inner /></div>;
}
}
class Inner extends Component {
getChildContext() {
return { innerContext };
}
render() {
return <InnerMost />;
}
}
class InnerMost extends Component {
render() {
return <strong>test</strong>;
}
}
sinon.spy(Inner.prototype, 'render');
sinon.spy(InnerMost.prototype, 'render');
render(<Outer />, scratch);
Squashed 'thirdparty/preact/' changes from b2d90cc..ba094e2 ba094e2 Run only local tests for pull requests (#390) e9fc3c2 Fix CI build (#386) 70a5ca3 This adds a link to preact-i18nline. (#382) 5dffd85 Merge branch 'pr-fix-build-for-windows' of https://github.com/Download/preact f14edf7 kilobits => kilobytes (#383) c193547 Test for #292 284e4aa 6.4.0 24eab2f Prevent accidental duplicate recycling of elements when swapping the base element of a component. Fixes #373. 76c5ef7 fix lint error 8008886 When swapping the base of a composed child component, update its parent's base reference. Fixes #349. fd4f21f Add an equality check prior to setting `.nodeValue` on text nodes, since Firefox (and maybe others) don't do this internally by default. Fixes #368 - thanks @zbinlin! 1555e2b Add CDNJS version badge in readme (#365) 79c8bae Disable React Developer Tools integration tests under IE (#362) 84f4eeb Refactor `linkState()` a bit to drop around 40 bytes. Coincidentally, that's the exact size of the hooks just added for DevTools... 👌 22bbfcb Little tweaks 👯 f8b326e Document how to use the React DevTools with Preact (#354) 1f4a8eb Correct "preact/devtools" type definitions (#355) 68f22eb Add React Developer Tools integration (#339) 2a7a027 Add ref and allow objects in className (#316) 4a59cca fix readme todomvc link (#345) 37ca4e0 Fixes build for Windows #343 cf93387 6.3.0 ff05818 Make `VNode.children` *always* be an Array, even when there are no children. 9b095f4 Added link to preact-layout (#342) git-subtree-dir: thirdparty/preact git-subtree-split: ba094e27b602cb16aded7dcad95f71e44b7b0476
2016-11-08 15:07:07 +01:00
expect(Inner.prototype.render).to.have.been.calledWith({ children: CHILDREN_MATCHER }, {}, { outerContext });
expect(InnerMost.prototype.render).to.have.been.calledWith({ children: CHILDREN_MATCHER }, {}, { outerContext, innerContext });
});
});