| 1 | "use strict"; |
| 2 | |
| 3 | var sinon = require("sinon"); |
| 4 | var events = require("../../").Runner.constants; |
| 5 | var helpers = require("./helpers"); |
| 6 | var reporters = require("../../").reporters; |
| 7 | |
| 8 | var Base = reporters.Base; |
| 9 | var Progress = reporters.Progress; |
| 10 | var createMockRunner = helpers.createMockRunner; |
| 11 | var makeRunReporter = helpers.createRunReporterFunction; |
| 12 | |
| 13 | var EVENT_RUN_BEGIN = events.EVENT_RUN_BEGIN; |
| 14 | var EVENT_RUN_END = events.EVENT_RUN_END; |
| 15 | var EVENT_TEST_END = events.EVENT_TEST_END; |
| 16 | |
| 17 | describe("Progress reporter", function () { |
| 18 | var runReporter = makeRunReporter(Progress); |
| 19 | var noop = function () {}; |
| 20 | |
| 21 | afterEach(function () { |
| 22 | sinon.restore(); |
| 23 | }); |
| 24 | |
| 25 | describe("event handlers", function () { |
| 26 | describe("on 'start' event", function () { |
| 27 | it("should call cursor hide", function () { |
| 28 | var hideCursorStub = sinon.stub(Base.cursor, "hide"); |
| 29 | |
| 30 | var runner = createMockRunner("start", EVENT_RUN_BEGIN); |
| 31 | var options = {}; |
| 32 | runReporter({}, runner, options); |
| 33 | sinon.restore(); |
| 34 | |
| 35 | expect(hideCursorStub.called, "to be true"); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe("on 'test end' event", function () { |
| 40 | describe("when line has changed", function () { |
| 41 | it("should write expected progress of open and close options", function () { |
| 42 | var crCursorStub = sinon.stub(Base.cursor, "CR").callsFake(noop); |
| 43 | sinon.stub(Base, "useColors").value(false); |
| 44 | sinon.stub(Base.window, "width").value(5); |
| 45 | |
| 46 | var expectedTotal = 12; |
| 47 | var expectedOpen = "OpEn"; |
| 48 | var expectedClose = "cLoSe"; |
| 49 | var expectedIncomplete = "iNcOmPlEtE"; |
| 50 | var expectedOptions = { |
| 51 | open: expectedOpen, |
| 52 | complete: "cOmPlEtE", |
| 53 | incomplete: expectedIncomplete, |
| 54 | close: expectedClose, |
| 55 | }; |
| 56 | |
| 57 | var runner = createMockRunner("test end", EVENT_TEST_END); |
| 58 | runner.total = expectedTotal; |
| 59 | var options = { |
| 60 | reporterOptions: expectedOptions, |
| 61 | }; |
| 62 | var stdout = runReporter({}, runner, options); |
| 63 | sinon.restore(); |
| 64 | |
| 65 | var expectedArray = [ |
| 66 | "\u001b[J", |
| 67 | " " + expectedOpen, |
| 68 | "", |
| 69 | expectedIncomplete, |
| 70 | expectedClose, |
| 71 | ]; |
| 72 | |
| 73 | expect(crCursorStub.called, "to be true"); |
| 74 | expect(stdout, "to equal", expectedArray); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | describe("when line has not changed", function () { |
| 79 | it("should not write anything", function () { |
| 80 | sinon.stub(Base, "useColors").value(false); |
| 81 | sinon.stub(Base.cursor, "CR").callsFake(noop); |
| 82 | sinon.stub(Base.window, "width").value(-3); |
| 83 | |
| 84 | var expectedTotal = 1; |
| 85 | var runner = createMockRunner("test end", EVENT_TEST_END); |
| 86 | runner.total = expectedTotal; |
| 87 | var options = {}; |
| 88 | var stdout = runReporter({}, runner, options); |
| 89 | sinon.restore(); |
| 90 | |
| 91 | expect(stdout, "to equal", []); |
| 92 | }); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | describe("on 'end' event", function () { |
| 97 | it("should call cursor show and epilogue", function () { |
| 98 | var showCursorStub = sinon.stub(Base.cursor, "show"); |
| 99 | var fakeThis = { |
| 100 | epilogue: sinon.spy(), |
| 101 | }; |
| 102 | var runner = createMockRunner("end", EVENT_RUN_END); |
| 103 | var options = {}; |
| 104 | runReporter(fakeThis, runner, options); |
| 105 | sinon.restore(); |
| 106 | |
| 107 | expect(fakeThis.epilogue.calledOnce, "to be true"); |
| 108 | expect(showCursorStub.called, "to be true"); |
| 109 | }); |
| 110 | }); |
| 111 | }); |
| 112 | }); |