Skip to content

Commit

Permalink
TASK: Integrate the test utility package for stateless pure react com…
Browse files Browse the repository at this point in the history
…ponents #18
  • Loading branch information
Inkdpixels committed Jan 27, 2016
1 parent 332bd45 commit 5959380
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Resources/Private/JavaScript/Host/Components/Button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ const Button = props => {
ref: btn => {
const method = isFocused ? 'focus' : 'blur';

//
// Initially focus the btn if the propType was set.
//
if (btn !== null) {
btn[method]();
}
}
};

//
// Disable the btn if the prop was set.
//
if (isDisabled) {
attributes.disabled = 'disabled';
}
Expand Down
51 changes: 37 additions & 14 deletions Resources/Private/JavaScript/Host/Components/Button/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
import React from 'react';
import chai from 'chai';
import TestUtils from 'react-addons-test-utils';
import {shallowRender} from 'skin-deep';
import Button from './index.js';

const expect = chai.expect;
const {expect} = chai;

function renderTree(props) {
return shallowRender(
<Button {...props}>
My button text
</Button>
);
}

describe('Button Component', () => {
let renderer;
it('should render a `<button>` element and the passed text as the label.', () => {
const tree = renderTree({
isFocused: false,
isDisabled: false,
style: 'clean',
hoverStyle: 'clean',
onClick: () => null
});

beforeEach(() => {
renderer = TestUtils.createRenderer();
expect(tree.getRenderOutput().type).to.equal('button');
expect(tree.text()).to.equal('My button text');
});

it('should render a button element and the passed children.', () => {
const onClickHandler = () => null;
it('should add the `disabled` attribute to the `<button>` node if the `isDisabled` prop was passed as `true`.', () => {
const disabledTree = renderTree({
isFocused: false,
isDisabled: true,
style: 'clean',
hoverStyle: 'clean',
onClick: () => null
});

renderer.render(
<Button isFocused={false} isDisabled={false} style="clean" hoverStyle="clean" onClick={onClickHandler}>
"test"
</Button>
);
expect(disabledTree.toString().indexOf('disabled') > -1).to.equal(true);

const actualElement = renderer.getRenderOutput();
const tree = renderTree({
isFocused: false,
isDisabled: false,
style: 'clean',
hoverStyle: 'clean',
onClick: () => null
});

expect(actualElement.type).to.equal('button');
expect(tree.toString().indexOf('disabled') > -1).to.equal(false);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"redux-devtools": "^2.1.3",
"sinon": "^1.17.2",
"sinon-chai": "^2.8.0",
"skin-deep": "^0.14.0",
"style-loader": "^0.13.0",
"url-loader": "^0.5.7",
"webpack": "^1.12.9"
Expand Down
12 changes: 10 additions & 2 deletions webpack.testing.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const webpack = require('webpack');
const path = require('path');
const glob = require('glob');
const config = require('./webpack.shared.config.js');
Expand All @@ -16,14 +17,20 @@ const loaders = Object.create(config.module.loaders);
const resolve = Object.create(config.resolve);
loaders.push({
test: /sinon\.js$/,
loader: "imports?define=>false,require=>false"
loader: 'imports?define=>false,require=>false'
});

if (!resolve.alias) {
resolve.alias = {};
}
resolve.alias.sinon = 'sinon/pkg/sinon';

//
// Setup for the
//
const plugins = [].concat(config.plugins);
plugins.push(new webpack.IgnorePlugin(/ReactContext/));

//
// Export the webpack configuration for the test environment.
//
Expand All @@ -41,5 +48,6 @@ module.exports = Object.assign({}, config, {
loaders
},

resolve
resolve,
plugins
});

0 comments on commit 5959380

Please sign in to comment.