diff --git a/package.json b/package.json index 4a4bbc7..eb0de28 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test.js b/test.js index 9dfb2e7..0670085 100644 --- a/test.js +++ b/test.js @@ -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; }); });