| 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 Dot = reporters.Dot; |
| 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_FAIL = events.EVENT_TEST_FAIL; |
| 16 | var EVENT_TEST_PASS = events.EVENT_TEST_PASS; |
| 17 | var EVENT_TEST_PENDING = events.EVENT_TEST_PENDING; |
| 18 | |
| 19 | describe("Dot reporter", function () { |
| 20 | var windowWidthStub; |
| 21 | var runReporter = makeRunReporter(Dot); |
| 22 | var noop = function () {}; |
| 23 | |
| 24 | beforeEach(function () { |
| 25 | windowWidthStub = sinon.stub(Base.window, "width").value(0); |
| 26 | sinon.stub(Base, "useColors").value(false); |
| 27 | sinon.stub(Base, "color").callsFake(function (type, str) { |
| 28 | return type.replace(/ /g, "-") + "_" + str; |
| 29 | }); |
| 30 | }); |
| 31 | |
| 32 | afterEach(function () { |
| 33 | sinon.restore(); |
| 34 | }); |
| 35 | |
| 36 | describe("event handlers", function () { |
| 37 | describe("on 'start' event", function () { |
| 38 | it("should write a newline", function () { |
| 39 | var runner = createMockRunner("start", EVENT_RUN_BEGIN); |
| 40 | var options = {}; |
| 41 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 42 | sinon.restore(); |
| 43 | |
| 44 | var expectedArray = ["\n"]; |
| 45 | expect(stdout, "to equal", expectedArray); |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | describe("on 'pending' event", function () { |
| 50 | describe("when window width is greater than 1", function () { |
| 51 | beforeEach(function () { |
| 52 | windowWidthStub.value(2); |
| 53 | }); |
| 54 | |
| 55 | it("should write a newline followed by a comma", function () { |
| 56 | var runner = createMockRunner("pending", EVENT_TEST_PENDING); |
| 57 | var options = {}; |
| 58 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 59 | sinon.restore(); |
| 60 | |
| 61 | var expectedArray = ["\n ", "pending_" + Base.symbols.comma]; |
| 62 | expect(stdout, "to equal", expectedArray); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("when window width is less than or equal to 1", function () { |
| 67 | it("should write a comma", function () { |
| 68 | var runner = createMockRunner("pending", EVENT_TEST_PENDING); |
| 69 | var options = {}; |
| 70 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 71 | sinon.restore(); |
| 72 | |
| 73 | var expectedArray = ["pending_" + Base.symbols.comma]; |
| 74 | expect(stdout, "to equal", expectedArray); |
| 75 | }); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | describe("on 'pass' event", function () { |
| 80 | var test = { |
| 81 | duration: 1, |
| 82 | slow: function () { |
| 83 | return 2; |
| 84 | }, |
| 85 | }; |
| 86 | |
| 87 | describe("when window width is greater than 1", function () { |
| 88 | beforeEach(function () { |
| 89 | windowWidthStub.value(2); |
| 90 | }); |
| 91 | |
| 92 | describe("when test speed is fast", function () { |
| 93 | it("should write a newline followed by a dot", function () { |
| 94 | var runner = createMockRunner( |
| 95 | "pass", |
| 96 | EVENT_TEST_PASS, |
| 97 | null, |
| 98 | null, |
| 99 | test, |
| 100 | ); |
| 101 | var options = {}; |
| 102 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 103 | sinon.restore(); |
| 104 | |
| 105 | expect(test.speed, "to equal", "fast"); |
| 106 | var expectedArray = ["\n ", "fast_" + Base.symbols.dot]; |
| 107 | expect(stdout, "to equal", expectedArray); |
| 108 | }); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | describe("when window width is less than or equal to 1", function () { |
| 113 | describe("when test speed is fast", function () { |
| 114 | it("should write a grey dot", function () { |
| 115 | var runner = createMockRunner( |
| 116 | "pass", |
| 117 | EVENT_TEST_PASS, |
| 118 | null, |
| 119 | null, |
| 120 | test, |
| 121 | ); |
| 122 | var options = {}; |
| 123 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 124 | sinon.restore(); |
| 125 | |
| 126 | expect(test.speed, "to equal", "fast"); |
| 127 | var expectedArray = ["fast_" + Base.symbols.dot]; |
| 128 | expect(stdout, "to equal", expectedArray); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | describe("when test speed is medium", function () { |
| 133 | it("should write a yellow dot", function () { |
| 134 | test.duration = 2; |
| 135 | var runner = createMockRunner( |
| 136 | "pass", |
| 137 | EVENT_TEST_PASS, |
| 138 | null, |
| 139 | null, |
| 140 | test, |
| 141 | ); |
| 142 | var options = {}; |
| 143 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 144 | sinon.restore(); |
| 145 | |
| 146 | expect(test.speed, "to equal", "medium"); |
| 147 | var expectedArray = ["medium_" + Base.symbols.dot]; |
| 148 | expect(stdout, "to equal", expectedArray); |
| 149 | }); |
| 150 | }); |
| 151 | |
| 152 | describe("when test speed is slow", function () { |
| 153 | it("should write a bright yellow dot", function () { |
| 154 | test.duration = 3; |
| 155 | var runner = createMockRunner( |
| 156 | "pass", |
| 157 | EVENT_TEST_PASS, |
| 158 | null, |
| 159 | null, |
| 160 | test, |
| 161 | ); |
| 162 | var options = {}; |
| 163 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 164 | sinon.restore(); |
| 165 | |
| 166 | expect(test.speed, "to equal", "slow"); |
| 167 | var expectedArray = ["bright-yellow_" + Base.symbols.dot]; |
| 168 | expect(stdout, "to equal", expectedArray); |
| 169 | }); |
| 170 | }); |
| 171 | }); |
| 172 | }); |
| 173 | |
| 174 | describe("on 'fail' event", function () { |
| 175 | var test = { |
| 176 | test: { |
| 177 | err: "some error", |
| 178 | }, |
| 179 | }; |
| 180 | |
| 181 | describe("when window width is greater than 1", function () { |
| 182 | beforeEach(function () { |
| 183 | windowWidthStub.value(2); |
| 184 | }); |
| 185 | |
| 186 | it("should write a newline followed by an exclamation mark", function () { |
| 187 | var runner = createMockRunner( |
| 188 | "fail", |
| 189 | EVENT_TEST_FAIL, |
| 190 | null, |
| 191 | null, |
| 192 | test, |
| 193 | ); |
| 194 | var options = {}; |
| 195 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 196 | sinon.restore(); |
| 197 | |
| 198 | var expectedArray = ["\n ", "fail_" + Base.symbols.bang]; |
| 199 | expect(stdout, "to equal", expectedArray); |
| 200 | }); |
| 201 | }); |
| 202 | |
| 203 | describe("when window width is less than or equal to 1", function () { |
| 204 | it("should write an exclamation mark", function () { |
| 205 | var runner = createMockRunner( |
| 206 | "fail", |
| 207 | EVENT_TEST_FAIL, |
| 208 | null, |
| 209 | null, |
| 210 | test, |
| 211 | ); |
| 212 | var options = {}; |
| 213 | var stdout = runReporter({ epilogue: noop }, runner, options); |
| 214 | sinon.restore(); |
| 215 | |
| 216 | var expectedArray = ["fail_" + Base.symbols.bang]; |
| 217 | expect(stdout, "to equal", expectedArray); |
| 218 | }); |
| 219 | }); |
| 220 | }); |
| 221 | |
| 222 | describe("on 'end' event", function () { |
| 223 | it("should call epilogue", function () { |
| 224 | var runner = createMockRunner("end", EVENT_RUN_END); |
| 225 | var fakeThis = { |
| 226 | epilogue: sinon.stub(), |
| 227 | }; |
| 228 | var options = {}; |
| 229 | runReporter(fakeThis, runner, options); |
| 230 | sinon.restore(); |
| 231 | |
| 232 | expect(fakeThis.epilogue.called, "to be true"); |
| 233 | }); |
| 234 | }); |
| 235 | }); |
| 236 | }); |