Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
"use strict";
13
14
var assign = require('Object.assign');
15
16
describe('EventPluginRegistry', function() {
17
var EventPluginRegistry;
18
var createPlugin;
19
20
beforeEach(function() {
21
EventPluginRegistry = require('EventPluginRegistry');
22
EventPluginRegistry._resetEventPlugins();
23
24
createPlugin = function(properties) {
25
return assign({extractEvents: function() {}}, properties);
26
};
27
});
28
29
it('should be able to inject ordering before plugins', function() {
30
var OnePlugin = createPlugin();
31
var TwoPlugin = createPlugin();
32
var ThreePlugin = createPlugin();
33
34
EventPluginRegistry.injectEventPluginOrder(['one', 'two', 'three']);
35
EventPluginRegistry.injectEventPluginsByName({
36
one: OnePlugin,
37
two: TwoPlugin
38
});
39
EventPluginRegistry.injectEventPluginsByName({
40
three: ThreePlugin
41
});
42
43
expect(EventPluginRegistry.plugins.length).toBe(3);
44
expect(EventPluginRegistry.plugins[0]).toBe(OnePlugin);
45
expect(EventPluginRegistry.plugins[1]).toBe(TwoPlugin);
46
expect(EventPluginRegistry.plugins[2]).toBe(ThreePlugin);
47
});
48
49
it('should be able to inject plugins before and after ordering', function() {
50
var OnePlugin = createPlugin();
51
var TwoPlugin = createPlugin();
52
var ThreePlugin = createPlugin();
53
54
EventPluginRegistry.injectEventPluginsByName({
55
one: OnePlugin,
56
two: TwoPlugin
57
});
58
EventPluginRegistry.injectEventPluginOrder(['one', 'two', 'three']);
59
EventPluginRegistry.injectEventPluginsByName({
60
three: ThreePlugin
61
});
62
63
expect(EventPluginRegistry.plugins.length).toBe(3);
64
expect(EventPluginRegistry.plugins[0]).toBe(OnePlugin);
65
expect(EventPluginRegistry.plugins[1]).toBe(TwoPlugin);
66
expect(EventPluginRegistry.plugins[2]).toBe(ThreePlugin);
67
});
68
69
it('should be able to inject repeated plugins and out-of-order', function() {
70
var OnePlugin = createPlugin();
71
var TwoPlugin = createPlugin();
72
var ThreePlugin = createPlugin();
73
74
EventPluginRegistry.injectEventPluginsByName({
75
one: OnePlugin,
76
three: ThreePlugin
77
});
78
EventPluginRegistry.injectEventPluginOrder(['one', 'two', 'three']);
79
EventPluginRegistry.injectEventPluginsByName({
80
two: TwoPlugin,
81
three: ThreePlugin
82
});
83
84
expect(EventPluginRegistry.plugins.length).toBe(3);
85
expect(EventPluginRegistry.plugins[0]).toBe(OnePlugin);
86
expect(EventPluginRegistry.plugins[1]).toBe(TwoPlugin);
87
expect(EventPluginRegistry.plugins[2]).toBe(ThreePlugin);
88
});
89
90
it('should throw if plugin does not implement `extractEvents`', function() {
91
var BadPlugin = {};
92
93
EventPluginRegistry.injectEventPluginOrder(['bad']);
94
95
expect(function() {
96
EventPluginRegistry.injectEventPluginsByName({
97
bad: BadPlugin
98
});
99
}).toThrow(
100
'Invariant Violation: EventPluginRegistry: Event plugins must ' +
101
'implement an `extractEvents` method, but `bad` does not.'
102
);
103
});
104
105
it('should throw if plugin does not exist in ordering', function() {
106
var OnePlugin = createPlugin();
107
var RandomPlugin = createPlugin();
108
109
EventPluginRegistry.injectEventPluginOrder(['one']);
110
111
expect(function() {
112
EventPluginRegistry.injectEventPluginsByName({
113
one: OnePlugin,
114
random: RandomPlugin
115
});
116
}).toThrow(
117
'Invariant Violation: EventPluginRegistry: Cannot inject event plugins ' +
118
'that do not exist in the plugin ordering, `random`.'
119
);
120
});
121
122
it('should throw if ordering is injected more than once', function() {
123
var pluginOrdering = [];
124
125
EventPluginRegistry.injectEventPluginOrder(pluginOrdering);
126
127
expect(function() {
128
EventPluginRegistry.injectEventPluginOrder(pluginOrdering);
129
}).toThrow(
130
'Invariant Violation: EventPluginRegistry: Cannot inject event plugin ' +
131
'ordering more than once. You are likely trying to load more than one ' +
132
'copy of React.'
133
);
134
});
135
136
it('should throw if different plugins injected using same name', function() {
137
var OnePlugin = createPlugin();
138
var TwoPlugin = createPlugin();
139
140
EventPluginRegistry.injectEventPluginsByName({same: OnePlugin});
141
142
expect(function() {
143
EventPluginRegistry.injectEventPluginsByName({same: TwoPlugin});
144
}).toThrow(
145
'Invariant Violation: EventPluginRegistry: Cannot inject two different ' +
146
'event plugins using the same name, `same`.'
147
);
148
});
149
150
it('should publish registration names of injected plugins', function() {
151
var OnePlugin = createPlugin({
152
eventTypes: {
153
click: {registrationName: 'onClick'},
154
focus: {registrationName: 'onFocus'}
155
}
156
});
157
var TwoPlugin = createPlugin({
158
eventTypes: {
159
magic: {
160
phasedRegistrationNames: {
161
bubbled: 'onMagicBubble',
162
captured: 'onMagicCapture'
163
}
164
}
165
}
166
});
167
168
EventPluginRegistry.injectEventPluginsByName({one: OnePlugin});
169
EventPluginRegistry.injectEventPluginOrder(['one', 'two']);
170
171
expect(Object.keys(EventPluginRegistry.registrationNameModules).length).toBe(2);
172
expect(EventPluginRegistry.registrationNameModules.onClick).toBe(OnePlugin);
173
expect(EventPluginRegistry.registrationNameModules.onFocus).toBe(OnePlugin);
174
175
EventPluginRegistry.injectEventPluginsByName({two: TwoPlugin});
176
177
expect(Object.keys(EventPluginRegistry.registrationNameModules).length).toBe(4);
178
expect(EventPluginRegistry.registrationNameModules.onMagicBubble).toBe(TwoPlugin);
179
expect(
180
EventPluginRegistry.registrationNameModules.onMagicCapture
181
).toBe(TwoPlugin);
182
});
183
184
it('should throw if multiple registration names collide', function() {
185
var OnePlugin = createPlugin({
186
eventTypes: {
187
photoCapture: {registrationName: 'onPhotoCapture'}
188
}
189
});
190
var TwoPlugin = createPlugin({
191
eventTypes: {
192
photo: {
193
phasedRegistrationNames: {
194
bubbled: 'onPhotoBubble',
195
captured: 'onPhotoCapture'
196
}
197
}
198
}
199
});
200
201
EventPluginRegistry.injectEventPluginsByName({
202
one: OnePlugin,
203
two: TwoPlugin
204
});
205
206
expect(function() {
207
EventPluginRegistry.injectEventPluginOrder(['one', 'two']);
208
}).toThrow(
209
'Invariant Violation: EventPluginHub: More than one plugin attempted ' +
210
'to publish the same registration name, `onPhotoCapture`.'
211
);
212
});
213
214
it('should throw if an invalid event is published', function() {
215
var OnePlugin = createPlugin({
216
eventTypes: {
217
badEvent: {/* missing configuration */}
218
}
219
});
220
221
EventPluginRegistry.injectEventPluginsByName({one: OnePlugin});
222
223
expect(function() {
224
EventPluginRegistry.injectEventPluginOrder(['one']);
225
}).toThrow(
226
'Invariant Violation: EventPluginRegistry: Failed to publish event ' +
227
'`badEvent` for plugin `one`.'
228
);
229
});
230
231
it('should be able to get the plugin from synthetic events', function() {
232
var clickDispatchConfig = {
233
registrationName: 'onClick'
234
};
235
var magicDispatchConfig = {
236
phasedRegistrationNames: {
237
bubbled: 'onMagicBubble',
238
captured: 'onMagicCapture'
239
}
240
};
241
242
var OnePlugin = createPlugin({
243
eventTypes: {
244
click: clickDispatchConfig,
245
magic: magicDispatchConfig
246
}
247
});
248
249
var clickEvent = {dispatchConfig: clickDispatchConfig};
250
var magicEvent = {dispatchConfig: magicDispatchConfig};
251
252
expect(EventPluginRegistry.getPluginModuleForEvent(clickEvent)).toBe(null);
253
expect(EventPluginRegistry.getPluginModuleForEvent(magicEvent)).toBe(null);
254
255
EventPluginRegistry.injectEventPluginsByName({one: OnePlugin});
256
EventPluginRegistry.injectEventPluginOrder(['one']);
257
258
expect(
259
EventPluginRegistry.getPluginModuleForEvent(clickEvent)
260
).toBe(OnePlugin);
261
expect(
262
EventPluginRegistry.getPluginModuleForEvent(magicEvent)
263
).toBe(OnePlugin);
264
});
265
266
});
267
268