| 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 |
|
| 15 |
goog.provide('goog.dom.datasetTest'); |
| 16 |
goog.setTestOnly('goog.dom.datasetTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.dataset'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
var $ = goog.dom.getElement; |
| 23 |
var dataset = goog.dom.dataset; |
| 24 |
|
| 25 |
|
| 26 |
function setUp() { |
| 27 |
var el = $('el2'); |
| 28 |
el.setAttribute('data-dynamic-key', 'dynamic'); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
function testHas() { |
| 33 |
var el = $('el1'); |
| 34 |
|
| 35 |
assertTrue( |
| 36 |
'Dataset should have an existing key', dataset.has(el, 'basicKey')); |
| 37 |
assertTrue( |
| 38 |
'Dataset should have an existing (unusual) key', |
| 39 |
dataset.has(el, 'UnusualKey1')); |
| 40 |
assertTrue( |
| 41 |
'Dataset should have an existing (unusual) key', |
| 42 |
dataset.has(el, 'unusual-Key2')); |
| 43 |
assertTrue( |
| 44 |
'Dataset should have an existing (bizarre) key', |
| 45 |
dataset.has(el, '-Bizarre--Key')); |
| 46 |
assertTrue( |
| 47 |
'Dataset should have an existing but empty-value attribute key', |
| 48 |
dataset.has(el, 'emptyString')); |
| 49 |
assertTrue( |
| 50 |
'Dataset should have a boolean attribute key', |
| 51 |
dataset.has(el, 'boolean')); |
| 52 |
assertFalse( |
| 53 |
'Dataset should not have a non-existent key', |
| 54 |
dataset.has(el, 'bogusKey')); |
| 55 |
assertFalse( |
| 56 |
'Dataset should not have invalid key', dataset.has(el, 'basic-key')); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
function testGet() { |
| 61 |
var el = $('el1'); |
| 62 |
|
| 63 |
assertEquals( |
| 64 |
'Dataset should return the proper value for an existing key', |
| 65 |
dataset.get(el, 'basicKey'), 'basic'); |
| 66 |
assertEquals( |
| 67 |
'Dataset should have an existing (unusual) key', |
| 68 |
dataset.get(el, 'UnusualKey1'), 'unusual1'); |
| 69 |
assertEquals( |
| 70 |
'Dataset should have an existing (unusual) key', |
| 71 |
dataset.get(el, 'unusual-Key2'), 'unusual2'); |
| 72 |
assertEquals( |
| 73 |
'Dataset should have an existing (bizarre) key', |
| 74 |
dataset.get(el, '-Bizarre--Key'), 'bizarre'); |
| 75 |
assertEquals( |
| 76 |
'Dataset should have an existing but empty-value attribute key', |
| 77 |
dataset.get(el, 'emptyString'), ''); |
| 78 |
assertEquals( |
| 79 |
'Dataset should have a boolean attribute key', dataset.get(el, 'boolean'), |
| 80 |
''); |
| 81 |
assertNull( |
| 82 |
'Dataset should return null for a non-existent key', |
| 83 |
dataset.get(el, 'bogusKey')); |
| 84 |
assertNull( |
| 85 |
'Dataset should return null for an invalid key', |
| 86 |
dataset.get(el, 'basic-key')); |
| 87 |
|
| 88 |
el = $('el2'); |
| 89 |
assertEquals( |
| 90 |
'Dataset should return the proper value for an existing key', |
| 91 |
dataset.get(el, 'dynamicKey'), 'dynamic'); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
function testSet() { |
| 96 |
var el = $('el2'); |
| 97 |
|
| 98 |
dataset.set(el, 'newKey', 'newValue'); |
| 99 |
assertTrue( |
| 100 |
'Dataset should have a newly created key', dataset.has(el, 'newKey')); |
| 101 |
assertEquals( |
| 102 |
'Dataset should return the proper value for a newly created key', |
| 103 |
dataset.get(el, 'newKey'), 'newValue'); |
| 104 |
|
| 105 |
dataset.set(el, 'dynamicKey', 'customValue'); |
| 106 |
assertTrue( |
| 107 |
'Dataset should have a modified, existing key', |
| 108 |
dataset.has(el, 'dynamicKey')); |
| 109 |
assertEquals( |
| 110 |
'Dataset should return the proper value for a modified key', |
| 111 |
dataset.get(el, 'dynamicKey'), 'customValue'); |
| 112 |
|
| 113 |
assertThrows('Invalid key should fail noticeably', function() { |
| 114 |
dataset.set(el, 'basic-key', ''); |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
function testRemove() { |
| 120 |
var el = $('el2'); |
| 121 |
|
| 122 |
assertTrue('Dataset starts with key', dataset.has(el, 'dynamicKey')); |
| 123 |
dataset.remove(el, 'dynamic-key'); |
| 124 |
assertTrue('Dataset should still have key', dataset.has(el, 'dynamicKey')); |
| 125 |
|
| 126 |
dataset.remove(el, 'dynamicKey'); |
| 127 |
assertFalse( |
| 128 |
'Dataset should not have a removed key', dataset.has(el, 'dynamicKey')); |
| 129 |
assertNull( |
| 130 |
'Dataset should return null for removed key', |
| 131 |
dataset.get(el, 'dynamicKey')); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
function testGetAll() { |
| 136 |
var el = $('el1'); |
| 137 |
var expectedDataset = { |
| 138 |
'basicKey': 'basic', |
| 139 |
'UnusualKey1': 'unusual1', |
| 140 |
'unusual-Key2': 'unusual2', |
| 141 |
'-Bizarre--Key': 'bizarre', |
| 142 |
'emptyString': '', |
| 143 |
'boolean': '' |
| 144 |
}; |
| 145 |
assertHashEquals( |
| 146 |
'Dataset should have basicKey, UnusualKey1, ' + |
| 147 |
'unusual-Key2, and -Bizarre--Key', |
| 148 |
expectedDataset, dataset.getAll(el)); |
| 149 |
} |