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 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright 2011 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.dom.datasetTest');
goog.setTestOnly('goog.dom.datasetTest');
goog.require('goog.dom');
goog.require('goog.dom.dataset');
goog.require('goog.testing.jsunit');
var $ = goog.dom.getElement;
var dataset = goog.dom.dataset;
function setUp() {
var el = $('el2');
el.setAttribute('data-dynamic-key', 'dynamic');
}
function testHas() {
var el = $('el1');
assertTrue(
'Dataset should have an existing key', dataset.has(el, 'basicKey'));
assertTrue(
'Dataset should have an existing (unusual) key',
dataset.has(el, 'UnusualKey1'));
assertTrue(
'Dataset should have an existing (unusual) key',
dataset.has(el, 'unusual-Key2'));
assertTrue(
'Dataset should have an existing (bizarre) key',
dataset.has(el, '-Bizarre--Key'));
assertTrue(
'Dataset should have an existing but empty-value attribute key',
dataset.has(el, 'emptyString'));
assertTrue(
'Dataset should have a boolean attribute key',
dataset.has(el, 'boolean'));
assertFalse(
'Dataset should not have a non-existent key',
dataset.has(el, 'bogusKey'));
assertFalse(
'Dataset should not have invalid key', dataset.has(el, 'basic-key'));
}
function testGet() {
var el = $('el1');
assertEquals(
'Dataset should return the proper value for an existing key',
dataset.get(el, 'basicKey'), 'basic');
assertEquals(
'Dataset should have an existing (unusual) key',
dataset.get(el, 'UnusualKey1'), 'unusual1');
assertEquals(
'Dataset should have an existing (unusual) key',
dataset.get(el, 'unusual-Key2'), 'unusual2');
assertEquals(
'Dataset should have an existing (bizarre) key',
dataset.get(el, '-Bizarre--Key'), 'bizarre');
assertEquals(
'Dataset should have an existing but empty-value attribute key',
dataset.get(el, 'emptyString'), '');
assertEquals(
'Dataset should have a boolean attribute key', dataset.get(el, 'boolean'),
'');
assertNull(
'Dataset should return null for a non-existent key',
dataset.get(el, 'bogusKey'));
assertNull(
'Dataset should return null for an invalid key',
dataset.get(el, 'basic-key'));
el = $('el2');
assertEquals(
'Dataset should return the proper value for an existing key',
dataset.get(el, 'dynamicKey'), 'dynamic');
}
function testSet() {
var el = $('el2');
dataset.set(el, 'newKey', 'newValue');
assertTrue(
'Dataset should have a newly created key', dataset.has(el, 'newKey'));
assertEquals(
'Dataset should return the proper value for a newly created key',
dataset.get(el, 'newKey'), 'newValue');
dataset.set(el, 'dynamicKey', 'customValue');
assertTrue(
'Dataset should have a modified, existing key',
dataset.has(el, 'dynamicKey'));
assertEquals(
'Dataset should return the proper value for a modified key',
dataset.get(el, 'dynamicKey'), 'customValue');
assertThrows('Invalid key should fail noticeably', function() {
dataset.set(el, 'basic-key', '');
});
}
function testRemove() {
var el = $('el2');
assertTrue('Dataset starts with key', dataset.has(el, 'dynamicKey'));
dataset.remove(el, 'dynamic-key');
assertTrue('Dataset should still have key', dataset.has(el, 'dynamicKey'));
dataset.remove(el, 'dynamicKey');
assertFalse(
'Dataset should not have a removed key', dataset.has(el, 'dynamicKey'));
assertNull(
'Dataset should return null for removed key',
dataset.get(el, 'dynamicKey'));
}
function testGetAll() {
var el = $('el1');
var expectedDataset = {
'basicKey': 'basic',
'UnusualKey1': 'unusual1',
'unusual-Key2': 'unusual2',
'-Bizarre--Key': 'bizarre',
'emptyString': '',
'boolean': ''
};
assertHashEquals(
'Dataset should have basicKey, UnusualKey1, ' +
'unusual-Key2, and -Bizarre--Key',
expectedDataset, dataset.getAll(el));
}
|