Skip to content

Commit

Permalink
Full test coverage
Browse files Browse the repository at this point in the history
Chai plugin buried for now
  • Loading branch information
glenjamin committed Jun 29, 2015
1 parent 5345974 commit 3463452
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
26 changes: 26 additions & 0 deletions chai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = function(chai, utils) {
var Assertion = chai.Assertion;
var flag = utils.flag;

function inRenderedOutput(query, msg) {
if (msg) flag(this, 'message', msg);
var renderer = flag(this, 'object');

var node = renderer.findNode(query);
this.assert(node,
'Expected to find #{exp} in #{act}',
'Expected not to find #{exp} in #{act}',
query, renderer
);
}

Assertion.addMethod('inRenderedOutput', inRenderedOutput);

chai.assert.inRenderedOutput = function(renderer, query, msg) {
new Assertion(renderer, msg).to.have.inRenderedOutput(query, msg);
};

chai.assert.notInRenderedOutput = function(renderer, query, msg) {
new Assertion(renderer, msg).to.not.have.inRenderedOutput(query, msg);
};
};
35 changes: 5 additions & 30 deletions skin-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ function shallowRender(elementOrFunction, context) {
shallowRenderer.render(element, context);
ReactContext.current = {};
return {
findNode: findNodeIn.bind(0, shallowRenderer),
findNode: function(query) {
return findNodeIn(shallowRenderer, query);
},
textIn: function(query) {
var node = findNodeIn(shallowRenderer, query);
return node && node.props && String(node.props.children);
return getTextFromNode(node);
},
text: function() {
return getTextFromNode(shallowRenderer.getRenderOutput());
Expand Down Expand Up @@ -95,7 +97,7 @@ function getTextFromNode(node) {
}
// Iterables get combined with spaces
if (typeof node.map === 'function') {
return node.map(getTextFromNode).join(' ');
return node.map(getTextFromNode).join(' ').replace(/\s+/, ' ');
}
// Non-dom components are a black box
if (TestUtils.isElement(node) && typeof node.type !== 'string') {
Expand All @@ -110,30 +112,3 @@ function getTextFromNode(node) {
// Otherwise, stop
return '';
}

exports.chaiShallowRender = function(chai, utils) {
var Assertion = chai.Assertion;
var flag = utils.flag;

function inRenderedOutput(query, msg) {
if (msg) flag(this, 'message', msg);
var renderer = flag(this, 'object');

var node = renderer.findNode(query);
this.assert(node,
'Expected to find #{exp} in #{act}',
'Expected not to find #{exp} in #{act}',
query, React.renderToString(renderer.getRenderOutput())
);
}

Assertion.addMethod('inRenderedOutput', inRenderedOutput);

chai.assert.inRenderedOutput = function(renderer, query, msg) {
new Assertion(renderer, msg).to.have.inRenderedOutput(query, msg);
};

chai.assert.notInRenderedOutput = function(renderer, query, msg) {
new Assertion(renderer, msg).to.not.have.inRenderedOutput(query, msg);
};
};
10 changes: 9 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ describe("skin-deep", function() {
.to.have.property("value", "glenjamin");
});

it("should no-op on field without change handler", function() {
var before = tree.findNode(".nickname");

tree.fillField(".nickname", "glenjamin");

expect(tree.findNode(".nickname")).to.eql(before);
});

it.skip("should set value of uncontrolled text field", function() {
// Can this be done?
});
Expand Down Expand Up @@ -194,7 +202,7 @@ describe("skin-deep", function() {
var tree = sd.shallowRender($('h1', { title: "blah" },
"Heading!",
$('div', { title: "blah" },
123,
123, $('hr'),
'Some text.',
'More text.',
[ React.createElement(Widget, { key: 1 }),
Expand Down

0 comments on commit 3463452

Please sign in to comment.