diff --git a/test/reporters/doc.spec.js b/test/reporters/doc.spec.js new file mode 100644 index 0000000000..2e0d1e029e --- /dev/null +++ b/test/reporters/doc.spec.js @@ -0,0 +1,213 @@ +'use strict'; + +var reporters = require('../../').reporters; +var Doc = reporters.Doc; + +describe('Doc reporter', function () { + var stdout; + var stdoutWrite; + var runner = {}; + beforeEach(function () { + stdout = []; + stdoutWrite = process.stdout.write; + process.stdout.write = function (string) { + stdout.push(string); + }; + }); + + describe('on suite', function () { + describe('if suite root does not exist', function () { + var expectedTitle = 'expectedTitle'; + var unescapedTitle = '
' + expectedTitle + '
'; + var suite = { + root: false, + title: expectedTitle + }; + it('should log html with indents and expected title', function () { + runner.on = function (event, callback) { + if (event === 'suite') { + callback(suite); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
\n', + '

' + expectedTitle + '

\n', + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + it('should escape title where necessary', function () { + var suite = { + root: false, + title: unescapedTitle + }; + expectedTitle = '<div>' + expectedTitle + '</div>'; + runner.on = function (event, callback) { + if (event === 'suite') { + callback(suite); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
\n', + '

' + expectedTitle + '

\n', + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + }); + describe('if suite root does exist', function () { + var suite = { + root: true + }; + it('should not log any html', function () { + runner.on = function (event, callback) { + if (event === 'suite') { + callback(suite); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + stdout.should.be.empty(); + }); + }); + }); + + describe('on suite end', function () { + describe('if suite root does not exist', function () { + var suite = { + root: false + }; + it('should log expected html with indents', function () { + runner.on = function (event, callback) { + if (event === 'suite end') { + callback(suite); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
\n', '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + }); + describe('if suite root does exist', function () { + var suite = { + root: true + }; + it('should not log any html', function () { + runner.on = function (event, callback) { + if (event === 'suite end') { + callback(suite); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + stdout.should.be.empty(); + }); + }); + }); + + describe('on pass', function () { + var expectedTitle = 'some tite'; + var expectedBody = 'some body'; + var test = { + title: expectedTitle, + body: expectedBody, + slow: function () { + return ''; + } + }; + it('should log html with indents and expected title and body', function () { + runner.on = function (event, callback) { + if (event === 'pass') { + callback(test); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedTitle + '
\n', + '
' + expectedBody + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + it('should escape title and body where necessary', function () { + var unescapedTitle = '
' + expectedTitle + '
'; + var unescapedBody = '
' + expectedBody + '
'; + test.title = unescapedTitle; + test.body = unescapedBody; + + var expectedEscapedTitle = '<div>' + expectedTitle + '</div>'; + var expectedEscapedBody = '<div>' + expectedBody + '</div>'; + runner.on = function (event, callback) { + if (event === 'pass') { + callback(test); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedEscapedTitle + '
\n', + '
' + expectedEscapedBody + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + }); + + describe('on fail', function () { + var expectedTitle = 'some tite'; + var expectedBody = 'some body'; + var expectedError = 'some error'; + var test = { + title: expectedTitle, + body: expectedBody, + slow: function () { + return ''; + } + }; + it('should log html with indents and expected title, body and error', function () { + runner.on = function (event, callback) { + if (event === 'fail') { + callback(test, expectedError); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedTitle + '
\n', + '
' + expectedBody + '
\n', + '
' + expectedError + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + it('should escape title, body and error where necessary', function () { + var unescapedTitle = '
' + expectedTitle + '
'; + var unescapedBody = '
' + expectedBody + '
'; + var unescapedError = '
' + expectedError + '
'; + test.title = unescapedTitle; + test.body = unescapedBody; + + var expectedEscapedTitle = '<div>' + expectedTitle + '</div>'; + var expectedEscapedBody = '<div>' + expectedBody + '</div>'; + var expectedEscapedError = '<div>' + expectedError + '</div>'; + runner.on = function (event, callback) { + if (event === 'fail') { + callback(test, unescapedError); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedEscapedTitle + '
\n', + '
' + expectedEscapedBody + '
\n', + '
' + expectedEscapedError + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + }); +});