Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this work more like toThrow where it'll set a message - https://github.com/jasmine/jasmine/blob/1_3_x/src/core/Matchers.js#L320-L346

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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: {}
};
Expand Down
25 changes: 14 additions & 11 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});
});