tobowers / screw-unit forked from nathansobo/screw-unit

A Javascript BDD Framework with nested describes, a convenient assertion syntax, and an intuitive test browser. - this fork is an attempt to add mocking (Ajax mocking is for prototype)

This URL has Read+Write access

screw-unit / lib / screw.events.js
100644 71 lines (69 sloc) 2.483 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function($) {
  $(Screw)
    .bind('loaded', function() {
      $('.describe, .it')
        .click(function() {
          document.location = location.href.split('?')[0] + '?' + $(this).fn('selector');
          return false;
        })
        .focus(function() {
          return $(this).addClass('focused');
        })
        .bind('scroll', function() {
          var sel = $(this).fn('selector');
          var use_browser_top = $.inArray(sel, [ Screw.Defaults.to_run + ':eq(0)', 'body > .describe']) >= 0;
          document.body.scrollTop = use_browser_top ? 0 : $(this).offset().top;
        });
 
      $('.it')
        .bind('enqueued', function() {
          $(this).addClass('enqueued');
        })
        .bind('running', function() {
          $(this)
            .addClass('running')
            .removeClass('failed')
            .removeClass('passed');
        })
        .bind('passed', function() {
          if ($(this).hasClass('skipped')){return}
          $(this)
            .addClass('passed')
            .removeClass('failed');
          $('.status').fn('display');
        })
        .bind('failed', function(e, reason) {
          if ($(this).hasClass('skipped')){return}
          reason = reason || '';
          $(this)
            .addClass('failed')
            .removeClass('passed')
            .append($('<p class="error"></p>').text(reason.toString()));
          $('.status').fn('display');
          if (reason.fileName || reason.lineNumber) {
            $(this)
              .append($('<p class="error"></p>').text(reason.fileName + " : " + reason.lineNumber));
          }
        })
        .bind('skipped', function(e, reason) {
          $(this)
            .removeClass('failed')
            .removeClass('passed')
            .addClass('skipped')
            .append($('<p class="skip_reason"></p>').text("Skipped because: " + reason.toString()));
          $('.status').fn('display');
        });
        
      $('.before')
        .bind('skipped', function(e, reason){
          if ($(this).hasClass('skipped')){ return }
          $(this)
            .addClass('skipped')
            .append($('<p class="skip_reason"></p>').text("Group Skipped because: " + reason.toString()));
          $('.it', $(this).parent().parent().get(0))
            .addClass('skipped');
          $('.status').fn('display');
        });
    })
    .bind('before', function() {
      $('.status').text('Starting...');
    })
})(jQuery);