'use strict'; describe('angular.scenario.ObjectModel', function() { var model; var runner; var spec, step; function buildSpec(id, name, definitions) { var spec = { id: id, name: name, definition: { name: definitions.shift() } }; var currentDef = spec.definition; forEach(definitions, function(defName) { currentDef.parent = { name: defName }; currentDef = currentDef.parent; }); return spec; } function buildStep(name, line) { return { name: name || 'test step', line: function() { return line || ''; } }; } beforeEach(function() { spec = buildSpec(1, 'test spec', ['describe 1']); step = buildStep(); runner = new angular.scenario.testing.MockRunner(); model = new angular.scenario.ObjectModel(runner); }); it('should value default empty value', function() { expect(model.value).toEqual({ name: '', children: [] }); }); it('should add spec and create describe blocks on SpecBegin event', function() { runner.emit('SpecBegin', buildSpec(1, 'test spec', ['describe 2', 'describe 1'])); expect(model.value.children['describe 1']).toBeDefined(); expect(model.value.children['describe 1'].children['describe 2']).toBeDefined(); expect(model.value.children['describe 1'].children['describe 2'].specs['test spec']).toBeDefined(); }); it('should set fullDefinitionName on SpecBegin event', function() { runner.emit('SpecBegin', buildSpec(1, 'fake spec', ['describe 2'])); var spec = model.getSpec(1); expect(spec.fullDefinitionName).toBeDefined(); expect(spec.fullDefinitionName).toEqual('describe 2'); }); it('should set fullDefinitionName on SpecBegin event (join more names by space)', function() { runner.emit('SpecBegin', buildSpec(1, 'fake spec', ['describe 2', 'describe 1'])); var spec = model.getSpec(1); expect(spec.fullDefinitionName).toEqual('describe 1 describe 2'); }); it('should add step to spec on StepBegin', function() { runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepEnd', spec, step); runner.emit('SpecEnd', spec); expect(model.value.children['describe 1'].specs['test spec'].steps.length).toEqual(1); }); it('should update spec timer duration on SpecEnd event', function() { runner.emit('SpecBegin', spec); runner.emit('SpecEnd', spec); expect(model.value.children['describe 1'].specs['test spec'].duration).toBeDefined(); }); it('should update step timer duration on StepEnd event', function() { runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepEnd', spec, step); runner.emit('SpecEnd', spec); expect(model.value.children['describe 1'].specs['test spec'].steps[0].duration).toBeDefined(); }); it('should set spec status on SpecEnd to success if no status set', function() { runner.emit('SpecBegin', spec); runner.emit('SpecEnd', spec); expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('success'); }); it('should set status to error after SpecError', function() { runner.emit('SpecBegin', spec); runner.emit('SpecError', spec, 'error'); expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('error'); }); it('should set spec status to failure if step fails', function() { runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepEnd', spec, step); runner.emit('StepBegin', spec, step); runner.emit('StepFailure', spec, step, 'error'); runner.emit('StepEnd', spec, step); runner.emit('StepBegin', spec, step); runner.emit('StepEnd', spec, step); runner.emit('SpecEnd', spec); expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('failure'); }); it('should set spec status to error if step errors', function() { runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepError', spec, step, 'error'); runner.emit('StepEnd', spec, step); runner.emit('StepBegin', spec, step); runner.emit('StepFailure', spec, step, 'error'); runner.emit('StepEnd', spec, step); runner.emit('SpecEnd', spec); expect(model.value.children['describe 1'].specs['test spec'].status).toEqual('error'); }); describe('events', function() { var Spec = angular.scenario.ObjectModel.Spec, Step = angular.scenario.ObjectModel.Step, callback; beforeEach(function() { callback = jasmine.createSpy('listener'); }); it('should provide method for registering a listener', function() { expect(model.on).toBeDefined(); expect(model.on instanceof Function).toBe(true); }); it('should forward SpecBegin event', function() { model.on('SpecBegin', callback); runner.emit('SpecBegin', spec); expect(callback).toHaveBeenCalled(); }); it('should forward SpecBegin event with ObjectModel.Spec as a param', function() { model.on('SpecBegin', callback); runner.emit('SpecBegin', spec); expect(callback.mostRecentCall.args[0] instanceof Spec).toBe(true); expect(callback.mostRecentCall.args[0].name).toEqual(spec.name); }); it('should forward SpecError event', function() { model.on('SpecError', callback); runner.emit('SpecBegin', spec); runner.emit('SpecError', spec, {}); expect(callback).toHaveBeenCalled(); }); it('should forward SpecError event with ObjectModel.Spec and error as a params', function() { var error = {}; model.on('SpecError', callback); runner.emit('SpecBegin', spec); runner.emit('SpecError', spec, error); var param = callback.mostRecentCall.args[0]; expect(param instanceof Spec).toBe(true); expect(param.name).toEqual(spec.name); expect(param.status).toEqual('error'); expect(param.error).toBe(error); }); it('should forward SpecEnd event', function() { model.on('SpecEnd', callback); runner.emit('SpecBegin', spec); runner.emit('SpecEnd', spec); expect(callback).toHaveBeenCalled(); }); it('should forward SpecEnd event with ObjectModel.Spec as a param', function() { model.on('SpecEnd', callback); runner.emit('SpecBegin', spec); runner.emit('SpecEnd', spec); expect(callback.mostRecentCall.args[0] instanceof Spec).toBe(true); expect(callback.mostRecentCall.args[0].name).toEqual(spec.name); }); it('should forward StepBegin event', function() { model.on('StepBegin', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); expect(callback).toHaveBeenCalled(); }); it('should forward StepBegin event with Spec and Step as params', function() { model.on('StepBegin', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); var params = callback.mostRecentCall.args; expect(params[0] instanceof Spec).toBe(true); expect(params[0].name).toEqual(spec.name); expect(params[1] instanceof Step).toBe(true); }); it('should forward StepError event', function() { model.on('StepError', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepError', spec, step, {}); expect(callback).toHaveBeenCalled(); }); it('should forward StepError event with Spec, Step and error as params', function() { var error = {}; model.on('StepError', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepError', spec, step, error); var params = callback.mostRecentCall.args; expect(params[0] instanceof Spec).toBe(true); expect(params[0].name).toEqual(spec.name); expect(params[1] instanceof Step).toBe(true); expect(params[1].status).toEqual('error'); expect(params[2]).toBe(error); }); it('should forward StepFailure event', function() { model.on('StepFailure', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepFailure', spec, step, {}); expect(callback).toHaveBeenCalled(); }); it('should forward StepFailure event with Spec, Step and error as params', function() { var error = {}; model.on('StepFailure', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepFailure', spec, step, error); var params = callback.mostRecentCall.args; expect(params[0] instanceof Spec).toBe(true); expect(params[0].name).toEqual(spec.name); expect(params[1] instanceof Step).toBe(true); expect(params[1].status).toEqual('failure'); expect(params[2]).toBe(error); }); it('should forward StepEnd event', function() { model.on('StepEnd', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepEnd', spec, step); expect(callback).toHaveBeenCalled(); }); it('should forward StepEnd event with Spec and Step as params', function() { model.on('StepEnd', callback); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepEnd', spec, step); var params = callback.mostRecentCall.args; expect(params[0] instanceof Spec).toBe(true); expect(params[0].name).toEqual(spec.name); expect(params[1] instanceof Step).toBe(true); }); it('should forward RunnerEnd event', function() { model.on('RunnerEnd', callback); runner.emit('RunnerEnd'); expect(callback).toHaveBeenCalled(); }); it('should set error of first failure', function() { var error = 'first-error', step2 = buildStep(); model.on('SpecEnd', function(spec) { expect(spec.error).toBeDefined(); expect(spec.error).toBe(error); }); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepFailure', spec, step, error); runner.emit('StepBegin', spec, step2); runner.emit('StepFailure', spec, step2, 'second-error'); runner.emit('SpecEnd', spec); }); it('should set line number of first failure', function() { var step = buildStep('fake', 'first-line'), step2 = buildStep('fake2', 'second-line'); model.on('SpecEnd', function(spec) { expect(spec.line).toBeDefined(); expect(spec.line).toBe('first-line'); }); runner.emit('SpecBegin', spec); runner.emit('StepBegin', spec, step); runner.emit('StepFailure', spec, step, null); runner.emit('StepBegin', spec, step2); runner.emit('StepFailure', spec, step2, null); runner.emit('SpecEnd', spec); }); }); }); href='#n258'>258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772