Debian Codegraph @

closure-library/closure/goog/html/sanitizer/elementweakmap_test.js

raw
1 // Copyright 2017 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 /** @fileoverview Tests for {@link goog.html.sanitizer.ElementWeakMap} */
16
17 goog.module('goog.html.sanitizer.ElementWeakMapTest');
18 goog.setTestOnly();
19
20 var ElementWeakMap = goog.require('goog.html.sanitizer.ElementWeakMap');
21 var testSuite = goog.require('goog.testing.testSuite');
22 var userAgent = goog.require('goog.userAgent');
23
24 /** @const {boolean} */
25 var ELEMENTWEAKMAP_SUPPORTED = !userAgent.IE || document.documentMode >= 10;
26
27 testSuite({
28 testBasic() {
29 if (!ELEMENTWEAKMAP_SUPPORTED) {
30 return;
31 }
32 var el1 = document.createElement('a');
33 var el2 = document.createElement('b');
34 var el3 = document.createElement('a');
35 var weakMap = ElementWeakMap.newWeakMap();
36 weakMap.set(el1, 1);
37 weakMap.set(el2, 2);
38
39 assertEquals(1, weakMap.get(el1));
40 assertEquals(2, weakMap.get(el2));
41 assertUndefined(weakMap.get(el3));
42 },
43
44 testDuplicates() {
45 if (!ELEMENTWEAKMAP_SUPPORTED) {
46 return;
47 }
48 var el1 = document.createElement('a');
49 var el2 = document.createElement('a');
50 var weakMap = ElementWeakMap.newWeakMap();
51 weakMap.set(el1, 1);
52 weakMap.set(el1, 2);
53
54 assertEquals(2, weakMap.get(el1));
55 assertUndefined(weakMap.get(el2));
56 },
57
58 testClear() {
59 if (!ELEMENTWEAKMAP_SUPPORTED) {
60 return;
61 }
62 var el = document.createElement('a');
63 var weakMap = ElementWeakMap.newWeakMap();
64 weakMap.set(el, 1);
65 weakMap.set(el, 2);
66
67 if (weakMap.clear) {
68 weakMap.clear();
69 }
70 }
71 });