import React, { render, createClass, createContext, createElement, cloneElement, findDOMNode, Component, PropTypes, unstable_renderSubtreeIntoContainer, __spread } from '../src'; describe('preact-compat', () => { describe('render()', () => { it('should be exported', () => { expect(React) .to.have.property('render') .that.is.a('function') .that.equals(render); }); it('should replace isomorphic content', () => { let ce = (type) => document.createElement(type); let Text = (text) => document.createTextNode(text); let root = ce('div'); let initialChild = ce('div'); initialChild.appendChild(Text('initial content')); root.appendChild(initialChild); render(
dynamic content
, root); expect(root) .to.have.property('textContent') .that.is.a('string') .that.equals('dynamic content'); }); it('should remove extra elements', () => { let ce = (type) => document.createElement(type); let Text = (text) => document.createTextNode(text); let root = ce('div'); let c1 = ce('div'); c1.appendChild(Text('isomorphic content')); root.appendChild(c1); let c2 = ce('div'); c2.appendChild(Text('extra content')); root.appendChild(c2); render(
dynamic content
, root); expect(root) .to.have.property('textContent') .that.is.a('string') .that.equals('dynamic content'); }); it('should remove text nodes', () => { let ce = (type) => document.createElement(type); let Text = (text) => document.createTextNode(text); let root = ce('div'); root.appendChild(Text('Text Content in the root')); root.appendChild(Text('More Text Content')); render(
dynamic content
, root); expect(root) .to.have.property('textContent') .that.is.a('string') .that.equals('dynamic content'); }); it('should support defaultValue', () => { let scratch = document.createElement('div'); (document.body || document.documentElement).appendChild(scratch); render(, scratch); expect(scratch.firstElementChild).to.have.property('value', 'foo'); }); }); describe('createClass()', () => { it('should be exported', () => { expect(React) .to.have.property('createClass') .that.is.a('function') .that.equals(createClass); }); it('should create a Component', () => { let specState = { something: 1 }; let spec = { foo: 'bar', getInitialState() { return specState; }, method: sinon.spy() }; const C = createClass(spec); let inst = new C(); expect(inst).to.have.property('foo', 'bar'); expect(inst).to.have.property('state', specState); expect(inst).to.have.property('method').that.is.a('function'); expect(inst).to.be.an.instanceof(Component); inst.method('a', 'b'); expect(spec.method) .to.have.been.calledOnce .and.calledOn(inst) .and.calledWithExactly('a', 'b'); }); it('should not bind blacklisted methods', () => { let constructor = () => { }; let render = () => null; const C = createClass({ constructor, render }); let c = new C(); expect(c).to.have.property('constructor').that.equals(constructor); expect(c).to.have.property('render').not.with.property('__bound'); }); it('should copy statics', () => { let def = { statics: { foo: 'bar', baz() { } } }; let c = createClass(def); expect(c).to.have.property('foo', def.statics.foo); expect(c).to.have.property('baz', def.statics.baz); }); it('should support mixins', () => { let def = { mixins: [ { foo: sinon.spy(), bar: sinon.spy() }, { bar: sinon.spy(), componentWillMount: sinon.spy(), render: 'nothing here' }, { componentWillMount: sinon.spy() } ], foo: sinon.spy(), componentWillMount: sinon.spy(), render: sinon.stub().returns(null) }; let C = createClass(def); let inst = new C(); inst.foo(); expect(def.foo).to.have.been.calledOnce; expect(def.mixins[0].foo).to.have.been.calledOnce.and.calledBefore(def.foo); inst.bar(); expect(def.mixins[0].bar).to.have.been.calledOnce; expect(def.mixins[1].bar).to.have.been.calledOnce.and.calledAfter(def.mixins[0].bar); let props = {}, state = {}; inst.componentWillMount(props, state); expect(def.mixins[1].componentWillMount) .to.have.been.calledOnce .and.calledWithExactly(props, state); expect(def.mixins[2].componentWillMount) .to.have.been.calledOnce .and.calledWithExactly(props, state) .and.calledAfter(def.mixins[1].componentWillMount); expect(inst.render(props, state)).to.equal(null); }); }); describe('createContext()', () => { it('should be exported', () => { expect(React) .to.have.property('createContext') .that.is.a('function') .that.equals(createContext); }); it('should create a context with a Provider and Consumer', () => { const Context = createContext(); expect(Context).to.have.property('Provider'); expect(Context).to.have.property('Consumer'); }); }); describe('createElement()', () => { it('should be exported', () => { expect(React) .to.have.property('createElement') .that.is.a('function') .that.equals(createElement); }); it('should normalize vnodes', () => { let vnode =
t
; let $$typeof = 0xeac7; try { // eslint-disable-next-line if (Function.prototype.toString.call(eval('Sym' + 'bol.for')).match(/\[native code\]/)) { // eslint-disable-next-line $$typeof = eval('Sym' + 'bol.for("react.element")'); } } catch (e) { } expect(vnode).to.have.property('$$typeof', $$typeof); expect(vnode).to.have.property('type', 'div'); expect(vnode).to.have.property('props').that.is.an('object'); expect(vnode.props).to.have.property('children'); expect(vnode.props.children[0]).to.have.property('$$typeof', $$typeof); expect(vnode.props.children[0]).to.have.property('type', 'a'); expect(vnode.props.children[0]).to.have.property('props').that.is.an('object'); expect(vnode.props.children[0].props).to.eql({ children: ['t'] }); }); it('should normalize onChange', () => { let props = { onChange() { } }; function expectToBeNormalized(vnode, desc) { expect(vnode, desc).to.have.property('props').with.all.keys(['oninput'].concat(vnode.props.type ? 'type' : [])).and.property('oninput').that.is.a('function'); } function expectToBeUnmodified(vnode, desc) { expect(vnode, desc).to.have.property('props').eql({ ...props, ...(vnode.props.type ? { type: vnode.props.type } : {}) }); } expectToBeUnmodified(
, '
'); expectToBeUnmodified(, ''); expectToBeUnmodified(, ''); expectToBeUnmodified(, ''); expectToBeNormalized(