Add support to fallback report providing on ReportDispatcher

This commit is contained in:
Mauricio Borges Silva
2015-12-30 14:48:47 -02:00
committed by Mauricio Borges Silva
parent e0541eca80
commit 5e3582d8e7
2 changed files with 32 additions and 0 deletions

View File

@@ -37,4 +37,27 @@ describe("ReportDispatcher", function() {
dispatcher.foo(123, 456); dispatcher.foo(123, 456);
}).not.toThrow(); }).not.toThrow();
}); });
it("allows providing a fallback reporter in case there's no other report", function() {
var dispatcher = new jasmineUnderTest.ReportDispatcher(['foo', 'bar']),
reporter = jasmine.createSpyObj('reporter', ['foo', 'bar']);
dispatcher.provideFallbackReporter(reporter);
dispatcher.foo(123, 456);
expect(reporter.foo).toHaveBeenCalledWith(123, 456);
});
it("does not call fallback reporting methods when another report is provided", function() {
var dispatcher = new jasmineUnderTest.ReportDispatcher(['foo', 'bar']),
reporter = jasmine.createSpyObj('reporter', ['foo', 'bar']),
fallbackReporter = jasmine.createSpyObj('otherReporter', ['foo', 'bar']);
dispatcher.provideFallbackReporter(fallbackReporter);
dispatcher.addReporter(reporter);
dispatcher.foo(123, 456);
expect(reporter.foo).toHaveBeenCalledWith(123, 456);
expect(fallbackReporter.foo).not.toHaveBeenCalledWith(123, 456);
});
}); });

View File

@@ -13,14 +13,23 @@ getJasmineRequireObj().ReportDispatcher = function() {
} }
var reporters = []; var reporters = [];
var fallbackReporter = null;
this.addReporter = function(reporter) { this.addReporter = function(reporter) {
reporters.push(reporter); reporters.push(reporter);
}; };
this.provideFallbackReporter = function(reporter) {
fallbackReporter = reporter;
};
return this; return this;
function dispatch(method, args) { function dispatch(method, args) {
if (reporters.length === 0 && fallbackReporter !== null) {
reporters.push(fallbackReporter);
}
for (var i = 0; i < reporters.length; i++) { for (var i = 0; i < reporters.length; i++) {
var reporter = reporters[i]; var reporter = reporters[i];
if (reporter[method]) { if (reporter[method]) {