Added jasmine matcher that expects calls to console.warn.
Jim committed
Dec 25, 2014 at 13:43 UTC
bd4aff062d5459802ae659c65d26b2dfb06f00e5
2 files changed
+74
-11
src/test/ReactTestUtils.js
+60
@@ -309,6 +309,66 @@ var ReactTestUtils = {
309
return new ReactShallowRenderer();
310
},
311
312
+ jasmineMatchers: {
313
+ /**
314
+ * Expect function to perform a console.warn.
315
+ * If `expectation` is a number, we expect the function to make `expectation` calls to warn.
316
+ * If `expectation` is a string, we expect that string to appear in the warn output.
317
+ **/
318
+ toWarn: function(expectation) {
319
+ var mocks = require('mocks');
320
+ var warn = console.warn;
321
+ console.warn = mocks.getMockFunction();
322
+ try {
323
+ this.actual();
324
+ var consoleLog = console.warn.mock.calls;
325
+ var result = { pass: false };
326
+ if (typeof expectation === 'number') {
327
+ result.pass = consoleLog.length === expectation;
328
+ } else {
329
+ // TODO: We may want to additionally handle a javascript regex
330
+ for (var i = 0; i < consoleLog.length; i++) {
331
+ if (consoleLog[i][0].indexOf(expectation) >= 0) {
332
+ result.pass = true;
333
+ }
334
+ }
335
+ }
336
+
337
+ this.message = function() {
338
+ if (result.pass) {
339
+ if (typeof expectation === 'number') {
340
+ return 'Expected [' + consoleLog + '].length=' +
341
+ consoleLog.length + ' NOT to equal expectation (' +
342
+ expectation + ').';
343
+ }
344
+ if (typeof expectation === 'string') {
345
+ return 'Expected [' + consoleLog +
346
+ '] NOT to contain expectation (' +
347
+ expectation + ').';
348
+ }
349
+ } else {
350
+ if (typeof expectation === 'number') {
351
+ return 'Expected [' + consoleLog + '].length=' +
352
+ consoleLog.length + ' to equal expectation (' +
353
+ expectation + ').';
354
+ }
355
+ if (typeof expectation === 'string') {
356
+ return 'Expected [' + consoleLog +
357
+ '] to contain expectation (' +
358
+ expectation + ').';
359
+ }
360
+ }
361
+ throw new Error('Assert not reached, unknown expectation type: ' + (typeof expectation));
362
+ };
363
+
364
+ return result.pass;
365
+ }
366
+ finally {
367
+ console.warn = warn;
368
+ }
369
+ }
370
+ },
371
+
372
Simulate: null,
373
SimulateNative: {}
374
};
src/test/__tests__/ReactTestUtils-test.js
+14
-11
@@ -14,23 +14,13 @@
14
var React;
15
var ReactTestUtils;
16
17
-var mocks;
18
-var warn;
19
-
17
describe('ReactTestUtils', function() {
18
19
beforeEach(function() {
23
- mocks = require('mocks');
24
-
20
React = require('React');
21
ReactTestUtils = require('ReactTestUtils');
22
28
- warn = console.warn;
29
- console.warn = mocks.getMockFunction();
30
- });
31
-
32
- afterEach(function() {
33
- console.warn = warn;
23
+ this.addMatchers(ReactTestUtils.jasmineMatchers);
24
});
25
26
it('should have shallow rendering', function() {
@@ -120,4 +110,17 @@ describe('ReactTestUtils', function() {
110
expect(scryResults.length).toBe(0);
111
112
});
113
+
114
+ it('expect console warn message success (jasmine integration)', function() {
115
+ expect(function(){console.warn('candy');}).toWarn('candy');
116
+ expect(function(){console.warn('candy');}).toWarn(1);
117
+ });
118
+
119
+ it('expect console warn to return true/false (direct invocation)', function() {
120
+ var scope = { actual: function(){console.warn('candy');} };
121
+ expect((ReactTestUtils.jasmineMatchers.toWarn.bind(scope))('candy')).toBe(true);
122
+ expect(ReactTestUtils.jasmineMatchers.toWarn.bind(scope)('ice cream')).toBe(false);
123
+ expect(ReactTestUtils.jasmineMatchers.toWarn.bind(scope)(1)).toBe(true);
124
+ expect(ReactTestUtils.jasmineMatchers.toWarn.bind(scope)(2)).toBe(false);
125
+ });
126
});