SyntheticKeyboardEvent unit tests
Adds unit tests for SyntheticKeyboardEvent.
cbrwizard committed
Mar 23, 2016 at 11:59 UTC
d743a8dd551b885389ba35da4bd928f12d1e99ce
1 file changed
+123
src/renderers/dom/client/syntheticEvents/__tests__/SyntheticKeyboardEvent-test.js
new
+123
@@ -0,0 +1,123 @@
1
+/**
2
+ * Copyright 2016-present, 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 SyntheticKeyboardEvent;
15
+var getEventCharCode;
16
+
17
+describe('SyntheticKeyboardEvent', function() {
18
+ var createEvent;
19
+
20
+ beforeEach(function() {
21
+ // Mock getEventCharCode for proper unit testing
22
+ jest.mock('getEventCharCode');
23
+ getEventCharCode = require('getEventCharCode');
24
+
25
+ SyntheticKeyboardEvent = require('SyntheticKeyboardEvent');
26
+ createEvent = function(nativeEvent) {
27
+ var target = require('getEventTarget')(nativeEvent);
28
+ return SyntheticKeyboardEvent.getPooled({}, '', nativeEvent, target);
29
+ };
30
+ });
31
+
32
+ describe('KeyboardEvent interface', function() {
33
+ describe('charCode', function() {
34
+ describe('when event is `keypress`', function() {
35
+ it('returns whatever getEventCharCode returns', function() {
36
+ getEventCharCode.mockReturnValue(100500);
37
+ var keyboardEvent = createEvent({type: 'keypress', charCode: 50});
38
+
39
+ expect(keyboardEvent.charCode).toBe(100500);
40
+ });
41
+ });
42
+
43
+ describe('when event is not `keypress`', function() {
44
+ it('returns 0', function() {
45
+ var keyboardEvent = createEvent({type: 'keyup', charCode: 50});
46
+ expect(keyboardEvent.charCode).toBe(0);
47
+ });
48
+ });
49
+ });
50
+
51
+ describe('keyCode', function() {
52
+ describe('when event is `keydown` or `keyup`', function() {
53
+ it('returns a passed keyCode', function() {
54
+ var keyboardEvent = createEvent({type: 'keyup', keyCode: 40});
55
+ expect(keyboardEvent.keyCode).toBe(40);
56
+ });
57
+ });
58
+
59
+ describe('when event is `keypress`', function() {
60
+ it('returns 0', function() {
61
+ var keyboardEvent = createEvent({type: 'keypress', charCode: 40});
62
+ expect(keyboardEvent.keyCode).toBe(0);
63
+ });
64
+ });
65
+ });
66
+
67
+ describe('which', function() {
68
+ describe('when event is `keypress`', function() {
69
+ it('returns whatever getEventCharCode returns', function() {
70
+ getEventCharCode.mockReturnValue(9001);
71
+ var keyboardEvent = createEvent({type: 'keypress', charCode: 50});
72
+
73
+ expect(keyboardEvent.which).toBe(9001);
74
+ });
75
+ });
76
+
77
+ describe('when event is `keydown` or `keyup`', function() {
78
+ it('returns a passed keyCode', function() {
79
+ var keyboardEvent = createEvent({type: 'keyup', keyCode: 40});
80
+ expect(keyboardEvent.which).toBe(40);
81
+ });
82
+ });
83
+
84
+ describe('when event type is unknown', function() {
85
+ it('returns 0', function() {
86
+ var keyboardEvent = createEvent({type: 'keysmack', keyCode: 40});
87
+ expect(keyboardEvent.which).toBe(0);
88
+ });
89
+ });
90
+ });
91
+ });
92
+
93
+ describe('EventInterface', function() {
94
+ it('normalizes properties from the Event interface', function() {
95
+ var target = document.createElement('div');
96
+ var syntheticEvent = createEvent({srcElement: target});
97
+
98
+ expect(syntheticEvent.target).toBe(target);
99
+ expect(syntheticEvent.type).toBe(undefined);
100
+ });
101
+
102
+ it('is able to `preventDefault` and `stopPropagation`', function() {
103
+ var nativeEvent = {};
104
+ var syntheticEvent = createEvent(nativeEvent);
105
+
106
+ expect(syntheticEvent.isDefaultPrevented()).toBe(false);
107
+ syntheticEvent.preventDefault();
108
+ expect(syntheticEvent.isDefaultPrevented()).toBe(true);
109
+
110
+ expect(syntheticEvent.isPropagationStopped()).toBe(false);
111
+ syntheticEvent.stopPropagation();
112
+ expect(syntheticEvent.isPropagationStopped()).toBe(true);
113
+ });
114
+
115
+ it('is able to `persist`', function() {
116
+ var syntheticEvent = createEvent({});
117
+
118
+ expect(syntheticEvent.isPersistent()).toBe(false);
119
+ syntheticEvent.persist();
120
+ expect(syntheticEvent.isPersistent()).toBe(true);
121
+ });
122
+ });
123
+});