This repository has been archived by the owner on Sep 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
298 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
v2.1.0 - Mon, 28 Sep 2015 13:45:13 GMT | ||
-------------------------------------- | ||
|
||
- [df40903](../../commit/df40903) [added] shallow rendering utils | ||
|
||
|
||
|
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
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,126 @@ | ||
import React, { cloneElement, isValidElement } from 'react'; | ||
import TestUtils from 'react-addons-test-utils'; | ||
import { match as _match, selector as s } from 'bill'; | ||
|
||
let isRtq = item => item && item.__isRTQ | ||
|
||
rtq.s = rtq.selector = s; | ||
|
||
export default rtq; | ||
|
||
function match(selector, tree, includeSelf){ | ||
if (typeof selector === 'function') | ||
selector = s`${selector}` | ||
|
||
return _match(selector, tree, includeSelf) | ||
} | ||
|
||
function render(element){ | ||
let root = element; | ||
|
||
if (!(typeof root.type === 'string' && root.type.toLowerCase() === root.type)){ | ||
let renderer = TestUtils.createRenderer() | ||
renderer.render(element) | ||
root = renderer.getRenderOutput(); | ||
} | ||
|
||
return { | ||
root, | ||
setProps(props){ | ||
return render(cloneElement(element, props)) | ||
} | ||
} | ||
} | ||
|
||
function rtq(element) { | ||
var context, rerender; | ||
|
||
if (TestUtils.isElement(element)) { | ||
let { root, setProps } = render(element) | ||
element = context = root | ||
rerender = setProps | ||
} | ||
else if (isRtq(element)) { | ||
context = element.root | ||
element = element.get(); | ||
//rerender = element._rerender | ||
} | ||
|
||
return new ShallowCollection(element, context, rerender) | ||
} | ||
|
||
class ShallowCollection { | ||
constructor(elements, root, rerender){ | ||
elements = [].concat(elements).filter(el => isValidElement(el)) | ||
|
||
var idx = -1; | ||
|
||
while( ++idx < elements.length) | ||
this[idx] = elements[idx] | ||
|
||
this._rerender = rerender | ||
this.length = elements.length | ||
this.root = root | ||
} | ||
|
||
setProps(props){ | ||
this._rerender && this._rerender(props) | ||
return this | ||
} | ||
|
||
each(cb) { | ||
var idx = -1, len = this.length; | ||
while (++idx < len) cb(this[idx], idx, this) | ||
return this | ||
} | ||
|
||
get() { | ||
var result = [] | ||
this.each(el => result.push(el)) | ||
return result | ||
} | ||
|
||
reduce(cb, initial){ | ||
return new ShallowCollection( | ||
[].reduce.call(this, cb, initial) | ||
, this.root | ||
) | ||
} | ||
|
||
map(cb) { | ||
var result = [] | ||
this.each((v, i, l) => result.push(cb(v, i, l))) | ||
|
||
return new ShallowCollection(result, this.root) | ||
} | ||
|
||
find(selector) { | ||
return this.reduce((result, element) => { | ||
return result.concat(match(selector, element)) | ||
}, []) | ||
} | ||
|
||
children(selector) { | ||
return this | ||
.reduce((result, element) => result.concat(element.props.children || []), []) | ||
.filter(selector) | ||
} | ||
|
||
filter(selector) { | ||
if (!selector) | ||
return this | ||
|
||
if (typeof selector === 'function') | ||
return new ShallowCollection([].filter.call(this, selector), this.root) | ||
|
||
let matches = match(selector, this.root); | ||
|
||
return new ShallowCollection([].filter.call(this, el => { | ||
return matches.indexOf(el) !== -1 | ||
}), this.root) | ||
} | ||
|
||
is(selector) { | ||
return this.filter(selector).length === this.length | ||
} | ||
} |
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,110 @@ | ||
import React, { cloneElement } from 'react'; | ||
import $ from '../src/shallow'; | ||
import { selector as sel } from 'bill'; | ||
|
||
chai.use(require('sinon-chai')) | ||
|
||
describe('Shallow rendering', ()=> { | ||
let Stateless = props => <div onClick={props.onClick}>{props.children}</div> | ||
let List = class extends React.Component { | ||
render(){ | ||
return ( | ||
<div onClick={this.props.onClick}> | ||
<Stateless onClick={this.props.onClick}> | ||
<span className='stateless-inner' onClick={this.props.onClick}/> | ||
</Stateless> | ||
<ul onClick={this.props.onClick}> | ||
<li className='item' onClick={this.props.onClick}>hi 1</li> | ||
<li onClick={this.props.onClick}>hi 2</li> | ||
<li>hi 3</li> | ||
</ul> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
it('create rtq object', ()=>{ | ||
let instance = $(<div/>) | ||
|
||
instance.root.type.should.equal('div') | ||
instance.length.should.equal(1) | ||
}) | ||
|
||
it('should not try to render primitives', ()=>{ | ||
let el = <div/> | ||
, instance = $(el) | ||
|
||
instance.root.should.equal(el) | ||
}) | ||
|
||
it('should render Composite Components', ()=>{ | ||
let el = <div/> | ||
, Element = ()=> el | ||
, instance = $(<Element/>) | ||
|
||
instance.root.should.equal(el) | ||
}) | ||
|
||
it('should filter out invalid Elements', ()=>{ | ||
let instance = $( | ||
<ul> | ||
{ false } | ||
{ null} | ||
{'text'} | ||
<li>hi 1</li> | ||
<li>hi 2</li> | ||
<li>hi 3</li> | ||
</ul> | ||
) | ||
|
||
instance.children().length.should.equal(3) | ||
}) | ||
|
||
describe('querying', ()=> { | ||
let FancyList = props => <ul className='fancy-list'>{props.children}</ul> | ||
let List = ()=> ( | ||
<div> | ||
<FancyList> | ||
<li className='foo'>hi 1</li> | ||
<li className='foo'>hi 2</li> | ||
<li>hi 3</li> | ||
</FancyList> | ||
</div> | ||
) | ||
|
||
it('should: find()', ()=>{ | ||
$(<List/>).find('li').length.should.equal(3) | ||
}) | ||
|
||
it('should: children()', ()=> { | ||
$(<List/>).find(FancyList).children().length.should.equal(3) | ||
|
||
$(<List/>).find(FancyList).children('.foo').length.should.equal(2) | ||
}) | ||
|
||
it('should: filter()', ()=>{ | ||
let items = $(<List/>).find('li') | ||
|
||
items.length.should.equal(3) | ||
items.filter('.foo').length.should.equal(2) | ||
|
||
items.filter((el, idx) => idx === 0).length.should.equal(1) | ||
}) | ||
|
||
it('an empty filter should be a noop', ()=>{ | ||
let instance = $(<List/>) | ||
instance.filter().should.equal(instance) | ||
}) | ||
|
||
it('should: is()', ()=>{ | ||
$(<List/>).find('.foo') | ||
.is('li').should.equal(true) | ||
|
||
$(<List/>).find('.foo') | ||
.is(sel`${FancyList} > li`).should.equal(true) | ||
|
||
$(<List/>).find(FancyList) | ||
.is('div').should.equal(false) | ||
}) | ||
}) | ||
}) |