forked from cyclejs/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes cyclejs#8 Implementation of `jsxFactory` is just a copy and paste from @sliptype's pull request (cyclejs/react-dom#3). Test cases have been rewritten in a DOM-free way (using enzyme).
- Loading branch information
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {createElement, ReactElement, ReactType} from 'react'; | ||
import {incorporate} from './incorporate'; | ||
export {Attributes} from 'react'; | ||
|
||
declare global { | ||
namespace JSX { | ||
interface IntrinsicAttributes { | ||
sel?: string | symbol; | ||
} | ||
} | ||
namespace React { | ||
interface ClassAttributes<T> extends Attributes { | ||
sel?: string | symbol; | ||
} | ||
} | ||
} | ||
|
||
type PropsExtensions = { | ||
sel?: string | symbol; | ||
} | ||
|
||
function createIncorporatedElement<P = any>( | ||
type: ReactType<P>, | ||
props: P & PropsExtensions | null, | ||
...children: Array<string | ReactElement<any>> | ||
): ReactElement<P> { | ||
if (!props || !props.sel) { | ||
return createElement(type, props, ...children); | ||
} else { | ||
return createElement(incorporate(type), props, ...children); | ||
} | ||
} | ||
|
||
export default { | ||
createElement: createIncorporatedElement | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import * as React from 'react'; | ||
import * as Adapter from 'enzyme-adapter-react-16'; | ||
import * as Enzyme from 'enzyme'; | ||
const assert = require('assert'); | ||
import {jsxFactory, h, ReactSource, makeCycleReactComponent} from '../src'; | ||
|
||
Enzyme.configure({adapter: new Adapter()}); | ||
|
||
const {shallow} = Enzyme; | ||
|
||
describe('jsxFactory', function () { | ||
it('w/ nothing', () => { | ||
const wrapper = shallow(<h1></h1>); | ||
|
||
assert.strictEqual(wrapper.find('h1').length, 1); | ||
}); | ||
|
||
it('w/ text child', () => { | ||
const wrapper = shallow(<h1>heading 1</h1>); | ||
|
||
const h1 = wrapper.find('h1'); | ||
assert.strictEqual(h1.text(), 'heading 1'); | ||
}); | ||
|
||
it('w/ children array', () => { | ||
const wrapper = shallow( | ||
<section> | ||
<h1>heading 1</h1> | ||
<h2>heading 2</h2> | ||
<h3>heading 3</h3> | ||
</section> | ||
); | ||
|
||
const section = wrapper.find('section'); | ||
assert.strictEqual(section.children().length, 3); | ||
}); | ||
|
||
it('w/ props', () => { | ||
const wrapper = shallow(<section data-foo='bar' />); | ||
|
||
assert(wrapper.exists('[data-foo="bar"]')); | ||
}); | ||
|
||
it('w/ props and children', () => { | ||
const wrapper = shallow( | ||
<section data-foo="bar"> | ||
<h1>heading 1</h1> | ||
<h2>heading 2</h2> | ||
<h3>heading 3</h3> | ||
</section> | ||
); | ||
|
||
const section = wrapper.first(); | ||
assert.strictEqual(section.length, 1); | ||
assert(section.exists('[data-foo="bar"]')); | ||
assert.deepStrictEqual(section.children().length, 3); | ||
}); | ||
|
||
it('w/ className', () => { | ||
const wrapper = shallow(<section className="foo" />); | ||
|
||
assert(wrapper.hasClass('foo')); | ||
}); | ||
|
||
it('renders functional component', () => { | ||
const Test = () => <h1>Functional</h1>; | ||
const wrapper = shallow(<Test />); | ||
|
||
assert.strictEqual(wrapper.first().type(), 'h1'); | ||
}); | ||
|
||
it('renders class component', () => { | ||
class Test extends React.Component { | ||
render() { | ||
return <h1>Class</h1>; | ||
} | ||
} | ||
|
||
const wrapper = shallow(<Test />); | ||
|
||
assert.strictEqual(wrapper.first().type(), 'h1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters