This repository was archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathjsaction_test.js
More file actions
102 lines (84 loc) · 3.42 KB
/
jsaction_test.js
File metadata and controls
102 lines (84 loc) · 3.42 KB
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
goog.provide('jsaction.jsactionTest');
goog.setTestOnly('jsaction.jsactionTest');
goog.require('goog.events.EventType');
goog.require('goog.testing.jsunit');
goog.require('jsaction');
var eventsToTargets = {};
jsaction.fireCustomEvent = function(target, type, opt_data) {
if (eventsToTargets[type]) {
eventsToTargets[type].push(target);
} else {
eventsToTargets[type] = [target];
}
};
// Fix Object.keys for IE8
if (!Object.keys) {
Object.keys = function(obj) {
var keys = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
keys.push(i);
}
}
return keys;
};
}
function setUp() {
eventsToTargets = {};
}
function tearDown() {
eventsToTargets = {};
}
function testBroadcastCustomEventNoMatches() {
jsaction.broadcastCustomEvent(document.getElementById('no-matches'),
'customEvent');
assertEquals(0, Object.keys(eventsToTargets).length);
}
function testBroadcastCustomEventMatches() {
jsaction.broadcastCustomEvent(document.getElementById('matches'),
'customEvent');
assertEquals(1, Object.keys(eventsToTargets).length);
assertTrue('customEvent' in eventsToTargets);
jsaction.broadcastCustomEvent(document.getElementById('matches'),
'custom2');
assertEquals(2, Object.keys(eventsToTargets).length);
assertTrue('custom2' in eventsToTargets);
assertEquals(2, eventsToTargets['customEvent'].length);
assertEquals('matches-foo', eventsToTargets['customEvent'][0].id);
assertEquals('matches-bar', eventsToTargets['customEvent'][1].id);
assertEquals(1, eventsToTargets['custom2'].length);
assertEquals('matches2', eventsToTargets['custom2'][0].id);
}
function testBroadcastCustomEventNestedMatches() {
jsaction.broadcastCustomEvent(document.getElementById('nested'),
'customEvent');
assertEquals(1, Object.keys(eventsToTargets).length);
assertTrue('customEvent' in eventsToTargets);
jsaction.broadcastCustomEvent(document.getElementById('nested'),
'custom2');
assertEquals(2, Object.keys(eventsToTargets).length);
assertTrue('custom2' in eventsToTargets);
assertEquals(3, eventsToTargets['customEvent'].length);
assertEquals('nested-foo', eventsToTargets['customEvent'][0].id);
assertEquals('nested-bar', eventsToTargets['customEvent'][1].id);
assertEquals('nested-qux', eventsToTargets['customEvent'][2].id);
assertEquals(1, eventsToTargets['custom2'].length);
assertEquals('nested2', eventsToTargets['custom2'][0].id);
}
function testBroadcastCustomEventPartialMatchDoesNotTriggerEvent() {
jsaction.broadcastCustomEvent(document.getElementById('partial_match_test'),
'partial_');
assertFalse('partial_' in eventsToTargets);
jsaction.broadcastCustomEvent(document.getElementById('partial_match_test'),
'_match');
assertFalse('_match' in eventsToTargets);
}
function testBroadcastCustomEventExactMatchTriggersEvent() {
// Does not trigger inexact_match
jsaction.broadcastCustomEvent(document.getElementById('exact_match_test'),
'exact_match');
assertEquals(3, eventsToTargets['exact_match'].length);
assertEquals('exact-match-a', eventsToTargets['exact_match'][0].id);
assertEquals('exact-match-b', eventsToTargets['exact_match'][1].id);
assertEquals('exact-match-c', eventsToTargets['exact_match'][2].id);
}