dataset_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.dom.datasetTest');
  15. goog.setTestOnly('goog.dom.datasetTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.dataset');
  18. goog.require('goog.testing.jsunit');
  19. var $ = goog.dom.getElement;
  20. var dataset = goog.dom.dataset;
  21. function setUp() {
  22. var el = $('el2');
  23. el.setAttribute('data-dynamic-key', 'dynamic');
  24. }
  25. function testHas() {
  26. var el = $('el1');
  27. assertTrue(
  28. 'Dataset should have an existing key', dataset.has(el, 'basicKey'));
  29. assertTrue(
  30. 'Dataset should have an existing (unusual) key',
  31. dataset.has(el, 'UnusualKey1'));
  32. assertTrue(
  33. 'Dataset should have an existing (unusual) key',
  34. dataset.has(el, 'unusual-Key2'));
  35. assertTrue(
  36. 'Dataset should have an existing (bizarre) key',
  37. dataset.has(el, '-Bizarre--Key'));
  38. assertFalse(
  39. 'Dataset should not have a non-existent key',
  40. dataset.has(el, 'bogusKey'));
  41. }
  42. function testGet() {
  43. var el = $('el1');
  44. assertEquals(
  45. 'Dataset should return the proper value for an existing key',
  46. dataset.get(el, 'basicKey'), 'basic');
  47. assertEquals(
  48. 'Dataset should have an existing (unusual) key',
  49. dataset.get(el, 'UnusualKey1'), 'unusual1');
  50. assertEquals(
  51. 'Dataset should have an existing (unusual) key',
  52. dataset.get(el, 'unusual-Key2'), 'unusual2');
  53. assertEquals(
  54. 'Dataset should have an existing (bizarre) key',
  55. dataset.get(el, '-Bizarre--Key'), 'bizarre');
  56. assertFalse(
  57. 'Dataset should return null or an empty string for a non-existent key',
  58. !!dataset.get(el, 'bogusKey'));
  59. el = $('el2');
  60. assertEquals(
  61. 'Dataset should return the proper value for an existing key',
  62. dataset.get(el, 'dynamicKey'), 'dynamic');
  63. }
  64. function testSet() {
  65. var el = $('el2');
  66. dataset.set(el, 'newKey', 'newValue');
  67. assertTrue(
  68. 'Dataset should have a newly created key', dataset.has(el, 'newKey'));
  69. assertEquals(
  70. 'Dataset should return the proper value for a newly created key',
  71. dataset.get(el, 'newKey'), 'newValue');
  72. dataset.set(el, 'dynamicKey', 'customValue');
  73. assertTrue(
  74. 'Dataset should have a modified, existing key',
  75. dataset.has(el, 'dynamicKey'));
  76. assertEquals(
  77. 'Dataset should return the proper value for a modified key',
  78. dataset.get(el, 'dynamicKey'), 'customValue');
  79. }
  80. function testRemove() {
  81. var el = $('el2');
  82. dataset.remove(el, 'dynamicKey');
  83. assertFalse(
  84. 'Dataset should not have a removed key', dataset.has(el, 'dynamicKey'));
  85. assertFalse(
  86. 'Dataset should return null or an empty string for removed key',
  87. !!dataset.get(el, 'dynamicKey'));
  88. }
  89. function testGetAll() {
  90. var el = $('el1');
  91. var expectedDataset = {
  92. 'basicKey': 'basic',
  93. 'UnusualKey1': 'unusual1',
  94. 'unusual-Key2': 'unusual2',
  95. '-Bizarre--Key': 'bizarre'
  96. };
  97. assertHashEquals(
  98. 'Dataset should have basicKey, UnusualKey1, ' +
  99. 'unusual-Key2, and -Bizarre--Key',
  100. expectedDataset, dataset.getAll(el));
  101. }