forked from jenseng/react-i18nliner
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing framework, split Translate into two components
- Loading branch information
Showing
9 changed files
with
163 additions
and
53 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 @@ | ||
/node_modules |
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,3 @@ | ||
{ | ||
"esnext": true | ||
} |
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,99 @@ | ||
/* | ||
Given: | ||
<Translate>Ohai {this.props.user}, click <Link>here</Link> right <b>now <i>please</i></b>!</Translate> | ||
Pre-process it into: | ||
<ComponentInterpolator string={I18n.t("Ohai, %{user}, click *here* right ***now **please** ***", {user: this.props.user})"> | ||
<Link /> | ||
<b><i /></b> | ||
</ComponentInterpolator> | ||
*/ | ||
|
||
var React = require('react'); | ||
var cloneWithProps = require('react/lib/cloneWithProps'); | ||
var invariant = require('react/lib/invariant'); | ||
var { string } = React.PropTypes; | ||
|
||
var OWN_PROPS = ['defaultValue', 'translateKey', 'children']; | ||
|
||
var ComponentInterpolator = React.createClass({ | ||
propTypes: { | ||
string: string.isRequired | ||
}, | ||
|
||
componentWillMount() { | ||
var textCount = this.textCount(); | ||
var componentCount = this.componentCount(); | ||
invariant( | ||
textCount === 0, | ||
'<ComponentInterpolator> cannot have any text children' | ||
); | ||
}, | ||
|
||
textCount(node) { | ||
node = node || this; | ||
count = 0; | ||
React.Children.forEach(node.props.children, function(child) { | ||
count += typeof child === 'string' ? 1 : this.textCount(child); | ||
}); | ||
return count; | ||
}, | ||
|
||
componentCount(node) { | ||
node = node || this; | ||
count = 0; | ||
React.Children.forEach(node.props.children, function(child) { | ||
count += typeof child === 'string' ? 0 : 1 + this.componentCount(child); | ||
}); | ||
return count; | ||
}, | ||
|
||
inferChildren(string, children) { | ||
var tokens = (string || '').split(/(\*+)/); | ||
return this.interpolateChildren(tokens, children); | ||
}, | ||
|
||
interpolateChildren(tokens, children, eof) { | ||
var token, child, newChildren = []; | ||
while (tokens.length) { | ||
token = tokens.shift(); | ||
if (token === eof) break; | ||
if (token.match(/\*/)) { | ||
child = children.shift(); | ||
child = cloneWithProps(child, { | ||
key: child.key, | ||
children: this.interpolateChildren(tokens, child.children, token) | ||
}); | ||
} | ||
else { | ||
child = token; | ||
} | ||
newChildren.push(child); | ||
} | ||
return newChildren; | ||
}, | ||
|
||
extraProps() { | ||
var props = {}; | ||
for (var key in this.props) { | ||
if (OWN_PROPS.indexOf(key) === -1) | ||
props[key] = this.props[key]; | ||
} | ||
return props; | ||
}, | ||
|
||
render() { | ||
var options = this.extraProps(); | ||
var translateKey = this.props.translateKey; | ||
var defaultValue = this.props.defaultValue || this.props.children; | ||
options.defaultValue = defaultValue; | ||
|
||
var children = this.inferChildren(this.props.string, this.props.children); | ||
return React.createElement('span', {}, children); | ||
} | ||
}); | ||
|
||
module.exports = ComponentInterpolator; | ||
|
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,15 @@ | ||
var subjector = require('../test_utils/subjector'); | ||
var Subject = subjector(__dirname + '/../ComponentInterpolator'); | ||
|
||
describe('ComponentInterpolator', function() { | ||
it('renders', function() { | ||
var subject = Subject({string: 'Hello World'}); | ||
expect(subject.isMounted()).toEqual(true); | ||
expect(subject.getDOMNode().textContent).toEqual('Hello World'); | ||
}); | ||
|
||
it('escapes html in the string', function() { | ||
var subject = Subject({string: 'My favorite tag is <script />'}); | ||
expect(subject.getDOMNode().textContent).toEqual('My favorite tag is <script />'); | ||
}); | ||
}); |
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 @@ | ||
jasmine.VERBOSE = true; |
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,6 @@ | ||
var ReactTools = require('react-tools'); | ||
module.exports = { | ||
process: function(src) { | ||
return ReactTools.transform(src, {harmony: true}); | ||
} | ||
}; |
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,14 @@ | ||
module.exports = function(path) { | ||
jest.dontMock(path); | ||
|
||
var React = require('react/addons'); | ||
var { addons: { TestUtils } } = React; | ||
var Component = require(path); | ||
|
||
return function(props) { | ||
return TestUtils.renderIntoDocument( | ||
React.createElement(Component, props) | ||
); | ||
}; | ||
}; | ||
|