diff --git a/src/test/ReactTestUtils.js b/src/test/ReactTestUtils.js index 196c63d5816..4b9f9971800 100644 --- a/src/test/ReactTestUtils.js +++ b/src/test/ReactTestUtils.js @@ -309,6 +309,66 @@ var ReactTestUtils = { return new ReactShallowRenderer(); }, + jasmineMatchers: { + /** + * Expect function to perform a console.warn. + * If `expectation` is a number, we expect the function to make `expectation` calls to warn. + * If `expectation` is a string, we expect that string to appear in the warn output. + **/ + toWarn: function(expectation) { + var mocks = require('mocks'); + var warn = console.warn; + console.warn = mocks.getMockFunction(); + try { + this.actual(); + var consoleLog = console.warn.mock.calls; + var result = { pass: false }; + if (typeof expectation === 'number') { + result.pass = consoleLog.length === expectation; + } else { + // TODO: We may want to additionally handle a javascript regex + for (var i = 0; i < consoleLog.length; i++) { + if (consoleLog[i][0].indexOf(expectation) >= 0) { + result.pass = true; + } + } + } + + this.message = function() { + if (result.pass) { + if (typeof expectation === 'number') { + return 'Expected [' + consoleLog + '].length=' + + consoleLog.length + ' NOT to equal expectation (' + + expectation + ').'; + } + if (typeof expectation === 'string') { + return 'Expected [' + consoleLog + + '] NOT to contain expectation (' + + expectation + ').'; + } + } else { + if (typeof expectation === 'number') { + return 'Expected [' + consoleLog + '].length=' + + consoleLog.length + ' to equal expectation (' + + expectation + ').'; + } + if (typeof expectation === 'string') { + return 'Expected [' + consoleLog + + '] to contain expectation (' + + expectation + ').'; + } + } + throw new Error('Assert not reached, unknown expectation type: ' + (typeof expectation)); + }; + + return result.pass; + } + finally { + console.warn = warn; + } + } + }, + Simulate: null, SimulateNative: {} }; diff --git a/src/test/__tests__/ReactTestUtils-test.js b/src/test/__tests__/ReactTestUtils-test.js index 796ac1b4b66..41a08773732 100644 --- a/src/test/__tests__/ReactTestUtils-test.js +++ b/src/test/__tests__/ReactTestUtils-test.js @@ -14,23 +14,13 @@ var React; var ReactTestUtils; -var mocks; -var warn; - describe('ReactTestUtils', function() { beforeEach(function() { - mocks = require('mocks'); - React = require('React'); ReactTestUtils = require('ReactTestUtils'); - warn = console.warn; - console.warn = mocks.getMockFunction(); - }); - - afterEach(function() { - console.warn = warn; + this.addMatchers(ReactTestUtils.jasmineMatchers); }); it('should have shallow rendering', function() { @@ -120,4 +110,17 @@ describe('ReactTestUtils', function() { expect(scryResults.length).toBe(0); }); + + it('expect console warn message success (jasmine integration)', function() { + expect(function(){console.warn('candy');}).toWarn('candy'); + expect(function(){console.warn('candy');}).toWarn(1); + }); + + it('expect console warn to return true/false (direct invocation)', function() { + var scope = { actual: function(){console.warn('candy');} }; + expect((ReactTestUtils.jasmineMatchers.toWarn.bind(scope))('candy')).toBe(true); + expect(ReactTestUtils.jasmineMatchers.toWarn.bind(scope)('ice cream')).toBe(false); + expect(ReactTestUtils.jasmineMatchers.toWarn.bind(scope)(1)).toBe(true); + expect(ReactTestUtils.jasmineMatchers.toWarn.bind(scope)(2)).toBe(false); + }); });