Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactored tests
using chai and sinon.
  • Loading branch information
felixgirault committed Oct 28, 2016
1 parent 28488ef commit e8ad663
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 35 deletions.
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -19,8 +19,10 @@
"fbjs": "^0.8.0"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.3.3",
"react": "^15.0.0 || ^0.14.0"
"react": "^15.0.0 || ^0.14.0",
"sinon": "^1.17.6"
},
"scripts": {
"test": "mocha"
Expand Down
103 changes: 69 additions & 34 deletions test.js
Expand Up @@ -4,62 +4,97 @@
*/
'use strict';

var assert = require('assert');
var expect = require('chai').expect;
var sinon = require('sinon');
var warning = require('fbjs/lib/warning');
var decorate = require('./index');



/**
*
*/
describe('pure-render-decorator', function() {
function Component() {};
decorate(Component);

var component = new Component();
component.props = { foo: 1 };
component.state = { bar: 2 };
/**
*
*/
beforeEach(function() {
function Component() {};
decorate(Component);

it('should add a method named shouldComponentUpdate', function() {
assert.ok('shouldComponentUpdate' in component);
assert.ok(typeof component.shouldComponentUpdate === 'function');
this.component = new Component();
this.component.props = { foo: 1 };
this.component.state = { bar: 2 };
});

it('should throw an error if shouldComponentUpdate is already implemented', function() {
function ComponentWithShouldComponentUpdate() {};
ComponentWithShouldComponentUpdate.prototype.shouldComponentUpdate = () => false;
/**
*
*/
it('should warn if shouldComponentUpdate is already implemented', function() {
function Component() {};
Component.prototype.shouldComponentUpdate = () => true;

var sandbox = sinon.sandbox.create();
var mock = sandbox.mock(console);

mock
.expects('error')
.once()
.withArgs(
'Warning: Cannot decorate `Component` with @pureRenderDecorator, '
+ 'because it already implements `shouldComponentUpdate().'
);

decorate(Component);

var expectedErrorMessage = "Cannot add a pure render decorator to "
+ "ComponentWithShouldComponentUpdate, because it already implements "
+ "`shouldComponentUpdate\\(\\)`";
assert.throws(() => decorate(ComponentWithShouldComponentUpdate), new RegExp(expectedErrorMessage));
mock.verify();
sandbox.restore();
});

it('should return true if the props and state are different', function() {
assert.ok(component.shouldComponentUpdate({}, {}));
/**
*
*/
it('should add a method named shouldComponentUpdate', function() {
expect(this.component).to.respondTo('shouldComponentUpdate');
});

it('should return false if the props and state are reference-equals', function() {
assert.ok(
!component.shouldComponentUpdate(
component.props,
component.state
)
);
/**
*
*/
it('should return the component', function() {
function Component() {};
expect(decorate(Component)).to.equal(Component);
});

it('should return false if props and state are shallow-equals but different references', function() {
assert.ok(
!component.shouldComponentUpdate(
{ foo: 1 },
{ bar: 2 }
/**
*
*/
it('should update if the props and state are different', function() {
expect(this.component.shouldComponentUpdate({}, {})).to.be.true;
});

/**
*
*/
it('should not update if the props and state are reference-equals', function() {
expect(
this.component.shouldComponentUpdate(
this.component.props,
this.component.state
)
);
).to.be.false;
});

/**
*
*/
it('should return the component', function() {
var c = function() {};
assert.equal(c, decorate(c));
it('should not update if props and state are shallow-equals but different references', function() {
expect(
this.component.shouldComponentUpdate(
{ foo: 1 },
{ bar: 2 }
)
).to.be.false;
});
});

0 comments on commit e8ad663

Please sign in to comment.