data-tests.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var $ = require('jquery');
  2. var Utils = require('select2/utils');
  3. module('Utils - GetUniqueElementId');
  4. test('Adds a prefix to the existing ID if one exists', function (assert) {
  5. var $element = $('<select id="existing-id"></select>');
  6. var id = Utils.GetUniqueElementId($element[0]);
  7. assert.notEqual(id, 'existing-id');
  8. assert.notEqual(id.indexOf('existing-id'), -1);
  9. });
  10. test('Generated random ID is not a number', function (assert) {
  11. var $element = $('<select></select>');
  12. var id = Utils.GetUniqueElementId($element[0]);
  13. assert.ok(isNaN(id));
  14. });
  15. module('Utils - RemoveData');
  16. test('The data-select2-id attribute is removed', function (assert) {
  17. var $element = $('<select data-select2-id="test"></select>');
  18. Utils.RemoveData($element[0]);
  19. assert.notEqual(
  20. $element.attr('data-select2-id'),
  21. 'test',
  22. 'The internal attribute was not removed when the data was cleared'
  23. );
  24. });
  25. test('The internal cache for the element is cleared', function (assert) {
  26. var $element = $('<select data-select2-id="test"></select>');
  27. Utils.__cache.test = {
  28. 'foo': 'bar'
  29. };
  30. Utils.RemoveData($element[0]);
  31. assert.equal(Utils.__cache.test, null, 'The cache should now be empty');
  32. });
  33. test('Calling it on an element without data works', function (assert) {
  34. assert.expect(0);
  35. var $element = $('<select></select>');
  36. Utils.RemoveData($element[0]);
  37. });