summaryrefslogtreecommitdiffstats
path: root/test/test.js
blob: bd02f7d2c8bb96d4f89be1f56dd619ee4cbb174a (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var assert = require('assert');
var jsdom = require('mocha-jsdom');

/**
 * Automated tests for jQuery Once.
 */
/* globals describe, it, before, beforeEach */
describe('jQuery Once', function () {
  /**
   * The global instance of jQuery.
   */
  var $;

  /**
   * Turn the Mocha test environment into a DOM environment with JSDom.
   */
  jsdom();

  /**
   * Before the tests initiate, load jQuery and jQuery Once.
   */
  before(function () {
    $ = require('jquery');
    $.once = require('../jquery.once.js');
  });

  /**
   * Before each test, reset the document body so that there is fresh data.
   */
  beforeEach(function () {
    // Build the body HTML.
    /* globals document */
    document.body.innerHTML = '<p>This is the <span>Test</span>.</p>';
  });

  it('should require ID to be a string', function () {
    // Expect it to throw an error.
    assert.throws(function () {
      $('span').once(function () {
        // Nothing.
      });
    });
  });

  it('properly executes .once("test2")', function () {
    // Create one once('test2') call.
    $('span').once('test2').data('test2', 'foo');

    // Create another once('test2') call.
    $('span').once('test2').data('test2', 'bar');

    // The data should result to the first once() call.
    var data = $('span').data('test2');
    assert.equal(data, 'foo');
  });

  it('is called only once with an ID', function () {
    // Count the number of times once() was called.
    $('span').data('count', 0);

    // Create the once() callback.
    var callback = function () {
      // Increment the count variable stored in the data.
      $('span').data('count', $('span').data('count') + 1);
    };

    // Call once() a bunch of times.
    for (var i = 0; i < 10; i++) {
      $('span').once('count').each(callback);
    }

    // Verify that it was only called once.
    var count = $('span').data('count');
    assert.equal(count, 1, 'It was called ' + count + ' times.');
  });

  it('is called only once without an ID', function () {
    // Count the number of times once() was called.
    $('span').data('once', 0);

    // Create the once() callback.
    var callback = function () {
      $('span').data('once', $('span').data('once') + 1);
    };

    // Call once() a bunch of times.
    for (var i = 0; i < 10; i++) {
      $('span').once().each(callback);
    }

    // Verify that it was only called once.
    var count = $('span').data('once');
    assert.equal(count, 1, 'It was called ' + count + ' times.');
  });

  it('retrieves empty once data correctly', function () {
    // Verify that the element starts without the class.
    var hasData = $('span').data('jquery-once-test3');
    assert(!hasData, 'Value not applied in the beginning.');

    // Create one once() call.
    $('span').once('test3');

    // Verify the data is applied.
    hasData = $('span').data('jquery-once-test3');
    assert(hasData, 'The value is properly applied after once().');
  });

  it('calls removeOnce() correctly', function () {
    // Create one once() call.
    $('span').once('test4');

    // Verify the data is applied.
    var hasData = $('span').data('jquery-once-test4');
    assert(hasData, 'The value is properly applied after once().');

    // Remove the once property.
    $('span').removeOnce('test4');
    hasData = $('span').data('jquery-once-test4');
    assert(!hasData, 'The value is properly removed when called removeOnce().');
  });

  it('calls findOnce() correctly', function () {
    // Append an additional span to the end.
    document.body.innerHTML += '<p>This is the <span>Test 2</span>.</p>';

    // Create one once() call.
    $('span').once('test5').data('foo', 'bar');

    // Find the once'd elements.
    $('span').findOnce('test5').each(function () {
      assert.equal($(this).data('foo'), 'bar', 'Found correct span data.');
    });
  });
});