| 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 NyanCat = reporters.Nyan; |
| 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("Nyan reporter", function () { |
| 20 | var noop = function () {}; |
| 21 | |
| 22 | afterEach(function () { |
| 23 | sinon.restore(); |
| 24 | }); |
| 25 | |
| 26 | describe("event handlers", function () { |
| 27 | var runReporter = makeRunReporter(NyanCat); |
| 28 | |
| 29 | describe("on 'start' event", function () { |
| 30 | it("should call draw", function () { |
| 31 | var fakeThis = { |
| 32 | draw: sinon.stub().callsFake(noop), |
| 33 | generateColors: noop, |
| 34 | }; |
| 35 | |
| 36 | var runner = createMockRunner("start", EVENT_RUN_BEGIN); |
| 37 | var options = {}; |
| 38 | runReporter(fakeThis, runner, options); |
| 39 | |
| 40 | expect(fakeThis.draw.called, "to be true"); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe("on 'pending' event", function () { |
| 45 | it("should call draw", function () { |
| 46 | var fakeThis = { |
| 47 | draw: sinon.stub().callsFake(noop), |
| 48 | generateColors: noop, |
| 49 | }; |
| 50 | var runner = createMockRunner("pending", EVENT_TEST_PENDING); |
| 51 | var options = {}; |
| 52 | runReporter(fakeThis, runner, options); |
| 53 | |
| 54 | expect(fakeThis.draw.called, "to be true"); |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | describe("on 'pass' event", function () { |
| 59 | it("should call draw", function () { |
| 60 | var test = { |
| 61 | duration: "", |
| 62 | slow: noop, |
| 63 | }; |
| 64 | var fakeThis = { |
| 65 | draw: sinon.stub().callsFake(noop), |
| 66 | generateColors: noop, |
| 67 | }; |
| 68 | var runner = createMockRunner( |
| 69 | "pass", |
| 70 | EVENT_TEST_PASS, |
| 71 | null, |
| 72 | null, |
| 73 | test, |
| 74 | ); |
| 75 | var options = {}; |
| 76 | runReporter(fakeThis, runner, options); |
| 77 | |
| 78 | expect(fakeThis.draw.called, "to be true"); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe("on 'fail' event", function () { |
| 83 | it("should call draw", function () { |
| 84 | var test = { |
| 85 | err: "", |
| 86 | }; |
| 87 | var fakeThis = { |
| 88 | draw: sinon.stub().callsFake(noop), |
| 89 | generateColors: noop, |
| 90 | }; |
| 91 | var runner = createMockRunner( |
| 92 | "fail", |
| 93 | EVENT_TEST_FAIL, |
| 94 | null, |
| 95 | null, |
| 96 | test, |
| 97 | ); |
| 98 | var options = {}; |
| 99 | runReporter(fakeThis, runner, options); |
| 100 | |
| 101 | expect(fakeThis.draw.called, "to be true"); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | describe("on 'end' event", function () { |
| 106 | it("should call epilogue", function () { |
| 107 | var fakeThis = { |
| 108 | draw: noop, |
| 109 | epilogue: sinon.stub().callsFake(noop), |
| 110 | generateColors: noop, |
| 111 | }; |
| 112 | var runner = createMockRunner("end", EVENT_RUN_END); |
| 113 | var options = {}; |
| 114 | runReporter(fakeThis, runner, options); |
| 115 | |
| 116 | expect(fakeThis.epilogue.called, "to be true"); |
| 117 | }); |
| 118 | |
| 119 | it("should write numberOfLines amount of newlines", function () { |
| 120 | var expectedNumberOfLines = 4; |
| 121 | var fakeThis = { |
| 122 | draw: noop, |
| 123 | epilogue: noop, |
| 124 | generateColors: noop, |
| 125 | }; |
| 126 | var runner = createMockRunner("end", EVENT_RUN_END); |
| 127 | var options = {}; |
| 128 | var stdout = runReporter(fakeThis, runner, options); |
| 129 | |
| 130 | var isBlankLine = function (value) { |
| 131 | return value === "\n"; |
| 132 | }; |
| 133 | |
| 134 | expect( |
| 135 | stdout.filter(isBlankLine), |
| 136 | "to have length", |
| 137 | expectedNumberOfLines, |
| 138 | ); |
| 139 | }); |
| 140 | |
| 141 | it("should call Base show", function () { |
| 142 | var showCursorStub = sinon.stub(Base.cursor, "show"); |
| 143 | var fakeThis = { |
| 144 | draw: noop, |
| 145 | epilogue: noop, |
| 146 | generateColors: noop, |
| 147 | }; |
| 148 | var runner = createMockRunner("end", EVENT_RUN_END); |
| 149 | var options = {}; |
| 150 | runReporter(fakeThis, runner, options); |
| 151 | sinon.restore(); |
| 152 | |
| 153 | expect(showCursorStub.called, "to be true"); |
| 154 | }); |
| 155 | }); |
| 156 | }); |
| 157 | |
| 158 | describe("#draw", function () { |
| 159 | var stdoutWriteStub; |
| 160 | var stdout; |
| 161 | |
| 162 | beforeEach(function () { |
| 163 | stdoutWriteStub = sinon.stub(process.stdout, "write"); |
| 164 | stdoutWriteStub.callsFake(function (chunk) { |
| 165 | stdout.push(chunk); |
| 166 | }); |
| 167 | stdout = []; |
| 168 | }); |
| 169 | |
| 170 | describe("when 'tick' is false", function () { |
| 171 | it("should draw face with expected spaces, _ and ^", function () { |
| 172 | var runner = { on: noop, once: noop }; |
| 173 | var options = {}; |
| 174 | var nyanCat = new NyanCat(runner, options); |
| 175 | nyanCat.stats = { passes: 2, pending: 1, failures: 0 }; |
| 176 | var fakeThis = { |
| 177 | tick: false, |
| 178 | appendRainbow: noop, |
| 179 | rainbowify: noop, |
| 180 | drawScoreboard: noop, |
| 181 | drawRainbow: noop, |
| 182 | drawNyanCat: NyanCat.prototype.drawNyanCat, |
| 183 | scoreboardWidth: 0, |
| 184 | trajectories: [[]], |
| 185 | face: noop, |
| 186 | cursorUp: noop, |
| 187 | }; |
| 188 | |
| 189 | try { |
| 190 | nyanCat.draw.call(fakeThis); |
| 191 | } finally { |
| 192 | sinon.restore(); |
| 193 | } |
| 194 | |
| 195 | var expectedArray = [ |
| 196 | "\u001b[0C", |
| 197 | "_,------,", |
| 198 | "\n", |
| 199 | "\u001b[0C", |
| 200 | "_| /\\_/\\ ", |
| 201 | "\n", |
| 202 | "\u001b[0C", |
| 203 | "^|__undefined ", |
| 204 | "\n", |
| 205 | "\u001b[0C", |
| 206 | ' "" "" ', |
| 207 | "\n", |
| 208 | ]; |
| 209 | expect(stdout, "to equal", expectedArray); |
| 210 | }); |
| 211 | }); |
| 212 | |
| 213 | describe("when 'tick' is true", function () { |
| 214 | it("should draw face with expected spaces, _ and ~", function () { |
| 215 | var runner = { on: noop, once: noop }; |
| 216 | var options = {}; |
| 217 | var nyanCat = new NyanCat(runner, options); |
| 218 | nyanCat.stats = { passes: 2, pending: 1, failures: 0 }; |
| 219 | var fakeThis = { |
| 220 | tick: true, |
| 221 | appendRainbow: noop, |
| 222 | rainbowify: noop, |
| 223 | drawScoreboard: noop, |
| 224 | drawRainbow: noop, |
| 225 | drawNyanCat: NyanCat.prototype.drawNyanCat, |
| 226 | scoreboardWidth: 0, |
| 227 | trajectories: [[]], |
| 228 | face: noop, |
| 229 | cursorUp: noop, |
| 230 | }; |
| 231 | |
| 232 | try { |
| 233 | nyanCat.draw.call(fakeThis); |
| 234 | } finally { |
| 235 | sinon.restore(); |
| 236 | } |
| 237 | |
| 238 | var expectedArray = [ |
| 239 | "\u001b[0C", |
| 240 | "_,------,", |
| 241 | "\n", |
| 242 | "\u001b[0C", |
| 243 | "_| /\\_/\\ ", |
| 244 | "\n", |
| 245 | "\u001b[0C", |
| 246 | "~|_undefined ", |
| 247 | "\n", |
| 248 | "\u001b[0C", |
| 249 | ' "" "" ', |
| 250 | "\n", |
| 251 | ]; |
| 252 | expect(stdout, "to equal", expectedArray); |
| 253 | }); |
| 254 | }); |
| 255 | }); |
| 256 | |
| 257 | describe("#cursorDown", function () { |
| 258 | var stdoutWriteStub; |
| 259 | var stdout; |
| 260 | |
| 261 | beforeEach(function () { |
| 262 | stdoutWriteStub = sinon.stub(process.stdout, "write"); |
| 263 | stdoutWriteStub.callsFake(function (chunk) { |
| 264 | stdout.push(chunk); |
| 265 | }); |
| 266 | stdout = []; |
| 267 | }); |
| 268 | |
| 269 | it("should write cursor down interaction with expected number", function () { |
| 270 | var runner = { on: noop, once: noop }; |
| 271 | var options = {}; |
| 272 | var nyanCat = new NyanCat(runner, options); |
| 273 | var expectedNumber = 25; |
| 274 | |
| 275 | try { |
| 276 | nyanCat.cursorDown(expectedNumber); |
| 277 | } finally { |
| 278 | sinon.restore(); |
| 279 | } |
| 280 | |
| 281 | var expectedArray = ["\u001b[" + expectedNumber + "B"]; |
| 282 | expect(stdout, "to equal", expectedArray); |
| 283 | }); |
| 284 | }); |
| 285 | |
| 286 | describe("#cursorUp", function () { |
| 287 | var stdoutWriteStub; |
| 288 | var stdout; |
| 289 | |
| 290 | beforeEach(function () { |
| 291 | stdoutWriteStub = sinon.stub(process.stdout, "write"); |
| 292 | stdoutWriteStub.callsFake(function (chunk) { |
| 293 | stdout.push(chunk); |
| 294 | }); |
| 295 | stdout = []; |
| 296 | }); |
| 297 | |
| 298 | it("should write cursor up interaction with expected number", function () { |
| 299 | var runner = { on: noop, once: noop }; |
| 300 | var options = {}; |
| 301 | var nyanCat = new NyanCat(runner, options); |
| 302 | var expectedNumber = 25; |
| 303 | |
| 304 | try { |
| 305 | nyanCat.cursorUp(expectedNumber); |
| 306 | } finally { |
| 307 | sinon.restore(); |
| 308 | } |
| 309 | |
| 310 | var expectedArray = ["\u001b[" + expectedNumber + "A"]; |
| 311 | expect(stdout, "to equal", expectedArray); |
| 312 | }); |
| 313 | }); |
| 314 | |
| 315 | describe("#rainbowify", function () { |
| 316 | var useColorsStub; |
| 317 | |
| 318 | beforeEach(function () { |
| 319 | useColorsStub = sinon.stub(Base, "useColors"); |
| 320 | }); |
| 321 | |
| 322 | afterEach(function () { |
| 323 | sinon.restore(); |
| 324 | }); |
| 325 | |
| 326 | describe("when 'useColors' is false", function () { |
| 327 | beforeEach(function () { |
| 328 | useColorsStub.value(false); |
| 329 | }); |
| 330 | |
| 331 | it("should return argument string", function () { |
| 332 | var runner = { on: noop, once: noop }; |
| 333 | var options = {}; |
| 334 | var nyanCat = new NyanCat(runner, options); |
| 335 | |
| 336 | var inputString = "hello"; |
| 337 | var outputString = nyanCat.rainbowify(inputString); |
| 338 | sinon.restore(); |
| 339 | |
| 340 | var expectedString = inputString; |
| 341 | expect(outputString, "to be", expectedString); |
| 342 | }); |
| 343 | }); |
| 344 | |
| 345 | describe("when 'useColors' is true", function () { |
| 346 | beforeEach(function () { |
| 347 | useColorsStub.value(true); |
| 348 | }); |
| 349 | |
| 350 | it("should return rainbowified string from the given string and predefined codes", function () { |
| 351 | var runner = { on: noop, once: noop }; |
| 352 | var options = {}; |
| 353 | var nyanCat = new NyanCat(runner, options); |
| 354 | |
| 355 | var inputString = "hello"; |
| 356 | var colorCode = "somecode"; |
| 357 | var fakeThis = { |
| 358 | rainbowColors: [colorCode], |
| 359 | colorIndex: 0, |
| 360 | }; |
| 361 | var outputString = nyanCat.rainbowify.call(fakeThis, inputString); |
| 362 | sinon.restore(); |
| 363 | |
| 364 | var startCode = "\u001b[38;5;"; |
| 365 | var endCode = "\u001b[0m"; |
| 366 | var expectedString = |
| 367 | startCode + colorCode + "m" + inputString + endCode; |
| 368 | expect(outputString, "to be", expectedString); |
| 369 | }); |
| 370 | }); |
| 371 | }); |
| 372 | |
| 373 | describe("#appendRainbow", function () { |
| 374 | describe("when 'tick' is true", function () { |
| 375 | it("should set an underscore segment", function () { |
| 376 | var runner = { on: noop, once: noop }; |
| 377 | var options = {}; |
| 378 | var nyanCat = new NyanCat(runner, options); |
| 379 | var expectedSegment; |
| 380 | var inputArray = []; |
| 381 | var trajectories = [inputArray, inputArray, inputArray, inputArray]; |
| 382 | nyanCat.appendRainbow.call({ |
| 383 | tick: true, |
| 384 | rainbowify: function (segment) { |
| 385 | expectedSegment = segment; |
| 386 | }, |
| 387 | numberOfLines: 4, |
| 388 | trajectoryWidthMax: 0, |
| 389 | trajectories, |
| 390 | }); |
| 391 | |
| 392 | expect(expectedSegment, "to be", "_"); |
| 393 | }); |
| 394 | |
| 395 | it("should shift each trajectory item if its length is greater than or equal to its max width", function () { |
| 396 | var runner = { on: noop, once: noop }; |
| 397 | var options = {}; |
| 398 | var nyanCat = new NyanCat(runner, options); |
| 399 | |
| 400 | var rainbowifyResult = "rainbowify"; |
| 401 | var inputArray = ["itemToShify"]; |
| 402 | var trajectories = [inputArray, inputArray, inputArray, inputArray]; |
| 403 | var expectedArray = [rainbowifyResult]; |
| 404 | var expectedTrajectories = [ |
| 405 | expectedArray, |
| 406 | expectedArray, |
| 407 | expectedArray, |
| 408 | expectedArray, |
| 409 | ]; |
| 410 | nyanCat.appendRainbow.call({ |
| 411 | tick: true, |
| 412 | rainbowify: function () { |
| 413 | return rainbowifyResult; |
| 414 | }, |
| 415 | numberOfLines: 4, |
| 416 | trajectoryWidthMax: 0, |
| 417 | trajectories, |
| 418 | }); |
| 419 | |
| 420 | expect(trajectories, "to equal", expectedTrajectories); |
| 421 | }); |
| 422 | }); |
| 423 | |
| 424 | describe("when 'tick' is false", function () { |
| 425 | it("should set a dash segment", function () { |
| 426 | var runner = { on: noop, once: noop }; |
| 427 | var options = {}; |
| 428 | var nyanCat = new NyanCat(runner, options); |
| 429 | var expectedSegment; |
| 430 | var inputArray = []; |
| 431 | var trajectories = [inputArray, inputArray, inputArray, inputArray]; |
| 432 | nyanCat.appendRainbow.call({ |
| 433 | tick: false, |
| 434 | rainbowify: function (segment) { |
| 435 | expectedSegment = segment; |
| 436 | }, |
| 437 | numberOfLines: 4, |
| 438 | trajectoryWidthMax: 5, |
| 439 | trajectories, |
| 440 | }); |
| 441 | |
| 442 | expect(expectedSegment, "to equal", "-"); |
| 443 | }); |
| 444 | }); |
| 445 | }); |
| 446 | |
| 447 | describe("#drawScoreboard", function () { |
| 448 | var stdout; |
| 449 | |
| 450 | beforeEach(function () { |
| 451 | sinon.stub(Base, "color").callsFake(function (type, n) { |
| 452 | return type + n; |
| 453 | }); |
| 454 | var stdoutWriteStub = sinon.stub(process.stdout, "write"); |
| 455 | stdoutWriteStub.callsFake(function (chunk) { |
| 456 | stdout.push(chunk); |
| 457 | }); |
| 458 | stdout = []; |
| 459 | }); |
| 460 | |
| 461 | it("should write scoreboard with color set with each stat", function () { |
| 462 | var runner = { on: noop, once: noop }; |
| 463 | var options = {}; |
| 464 | var nyanCat = new NyanCat(runner, options); |
| 465 | |
| 466 | var passes = 2; |
| 467 | var pending = 1; |
| 468 | var failures = 1; |
| 469 | |
| 470 | var fakeThis = { |
| 471 | cursorUp: noop, |
| 472 | stats: { passes, pending, failures }, |
| 473 | numberOfLines: 4, |
| 474 | }; |
| 475 | |
| 476 | try { |
| 477 | nyanCat.drawScoreboard.call(fakeThis); |
| 478 | } finally { |
| 479 | sinon.restore(); |
| 480 | } |
| 481 | |
| 482 | var expectedArray = [ |
| 483 | " ", |
| 484 | "green" + passes, |
| 485 | "\n", |
| 486 | " ", |
| 487 | "fail" + failures, |
| 488 | "\n", |
| 489 | " ", |
| 490 | "pending" + pending, |
| 491 | "\n", |
| 492 | "\n", |
| 493 | ]; |
| 494 | expect(stdout, "to equal", expectedArray); |
| 495 | }); |
| 496 | |
| 497 | it("should call cursorUp with given numberOfLines", function () { |
| 498 | var expectedNumberOfLines = 1000; |
| 499 | |
| 500 | var runner = { on: noop, once: noop }; |
| 501 | var options = {}; |
| 502 | var nyanCat = new NyanCat(runner, options); |
| 503 | var fakeThis = { |
| 504 | cursorUp: sinon.spy(), |
| 505 | stats: { passes: 0, pending: 0, failures: 0 }, |
| 506 | numberOfLines: expectedNumberOfLines, |
| 507 | }; |
| 508 | |
| 509 | try { |
| 510 | nyanCat.drawScoreboard.call(fakeThis); |
| 511 | } finally { |
| 512 | sinon.restore(); |
| 513 | } |
| 514 | |
| 515 | expect(fakeThis.cursorUp.calledWith(expectedNumberOfLines), "to be true"); |
| 516 | }); |
| 517 | }); |
| 518 | |
| 519 | describe("#drawRainbow", function () { |
| 520 | var stdout; |
| 521 | |
| 522 | beforeEach(function () { |
| 523 | var stdoutWriteStub = sinon.stub(process.stdout, "write"); |
| 524 | stdoutWriteStub.callsFake(function (chunk) { |
| 525 | stdout.push(chunk); |
| 526 | }); |
| 527 | stdout = []; |
| 528 | }); |
| 529 | |
| 530 | it("should write width, contents and newline for each trajectory", function () { |
| 531 | var expectedWidth = 444; |
| 532 | |
| 533 | var expectedContents = "input"; |
| 534 | var inputArray = [expectedContents]; |
| 535 | var trajectories = [inputArray]; |
| 536 | var runner = { on: noop, once: noop }; |
| 537 | var options = {}; |
| 538 | var nyanCat = new NyanCat(runner, options); |
| 539 | var fakeThis = { |
| 540 | cursorUp: noop, |
| 541 | trajectories, |
| 542 | scoreboardWidth: expectedWidth, |
| 543 | numberOfLines: 1, |
| 544 | }; |
| 545 | |
| 546 | try { |
| 547 | nyanCat.drawRainbow.call(fakeThis); |
| 548 | } finally { |
| 549 | sinon.restore(); |
| 550 | } |
| 551 | |
| 552 | var expectedArray = [ |
| 553 | "\u001b[" + expectedWidth + "C", |
| 554 | expectedContents, |
| 555 | "\n", |
| 556 | ]; |
| 557 | expect(stdout, "to equal", expectedArray); |
| 558 | }); |
| 559 | |
| 560 | it("should call cursorUp with given numberOfLines", function () { |
| 561 | var expectedCursorArgument = null; |
| 562 | var expectedNumberOfLines = 1000; |
| 563 | |
| 564 | var runner = { on: noop, once: noop }; |
| 565 | var options = {}; |
| 566 | var nyanCat = new NyanCat(runner, options); |
| 567 | var fakeThis = { |
| 568 | cursorUp: function (lines) { |
| 569 | expectedCursorArgument = lines; |
| 570 | }, |
| 571 | trajectories: [["input"]], |
| 572 | scoreboardWidth: 1, |
| 573 | numberOfLines: expectedNumberOfLines, |
| 574 | }; |
| 575 | |
| 576 | try { |
| 577 | nyanCat.drawRainbow.call(fakeThis); |
| 578 | } finally { |
| 579 | sinon.restore(); |
| 580 | } |
| 581 | |
| 582 | expect(expectedCursorArgument, "to be", expectedNumberOfLines); |
| 583 | }); |
| 584 | }); |
| 585 | |
| 586 | describe("#face", function () { |
| 587 | it('should expect "( x .x)" if any failures', function () { |
| 588 | var runner = { on: noop, once: noop }; |
| 589 | var options = {}; |
| 590 | var nyanCat = new NyanCat(runner, options); |
| 591 | nyanCat.stats = { passes: 2, pending: 1, failures: 1 }; |
| 592 | |
| 593 | expect(nyanCat.face(), "to be", "( x .x)"); |
| 594 | }); |
| 595 | |
| 596 | it('should expect "( o .o)" if any pending but none failing', function () { |
| 597 | var runner = { on: noop, once: noop }; |
| 598 | var options = {}; |
| 599 | var nyanCat = new NyanCat(runner, options); |
| 600 | nyanCat.stats = { passes: 2, pending: 1, failures: 0 }; |
| 601 | |
| 602 | expect(nyanCat.face(), "to be", "( o .o)"); |
| 603 | }); |
| 604 | |
| 605 | it('should expect "( ^ .^)" if all passing', function () { |
| 606 | var runner = { on: noop, once: noop }; |
| 607 | var options = {}; |
| 608 | var nyanCat = new NyanCat(runner, options); |
| 609 | nyanCat.stats = { passes: 1, pending: 0, failures: 0 }; |
| 610 | |
| 611 | expect(nyanCat.face(), "to be", "( ^ .^)"); |
| 612 | }); |
| 613 | |
| 614 | it('should expect "( - .-)" otherwise', function (done) { |
| 615 | var runner = { on: noop, once: noop }; |
| 616 | var options = {}; |
| 617 | var nyanCat = new NyanCat(runner, options); |
| 618 | nyanCat.stats = { passes: 0, pending: 0, failures: 0 }; |
| 619 | |
| 620 | expect(nyanCat.face(), "to be", "( - .-)"); |
| 621 | done(); |
| 622 | }); |
| 623 | }); |
| 624 | }); |