@samitouri / QOS-React / commits / 7bdf4dba75

getEventCharCode unit tests

Adds unit tests to getEventCharCode.

cbrwizard committed Mar 21, 2016 at 12:13 UTC 7bdf4dba75aa69a4b41df33083004ef607e2e559
1 file changed +89
src/renderers/dom/client/utils/__tests__/getEventCharCode-test.js new
+89
@@ -0,0 +1,89 @@
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 getEventCharCode = require('getEventCharCode');
15 +
16 +describe('getEventCharCode', function() {
17 + describe('when charCode is present in nativeEvent', function() {
18 + describe('when charCode is 0 and keyCode is 13', function() {
19 + it('returns 13', function() {
20 + var nativeEvent = new KeyboardEvent(
21 + 'keypress', {charCode: 0, keyCode: 13}
22 + );
23 +
24 + expect(getEventCharCode(nativeEvent)).toBe(13);
25 + });
26 + });
27 +
28 + describe('when charCode is not 0 and/or keyCode is not 13', function() {
29 + describe('when charCode is 32 or bigger', function() {
30 + it('returns charCode', function() {
31 + var nativeEvent = new KeyboardEvent('keypress', {charCode: 32});
32 +
33 + expect(getEventCharCode(nativeEvent)).toBe(32);
34 + });
35 + });
36 +
37 + describe('when charCode is smaller than 32', function() {
38 + describe('when charCode is 13', function() {
39 + it('returns 13', function() {
40 + var nativeEvent = new KeyboardEvent('keypress', {charCode: 13});
41 +
42 + expect(getEventCharCode(nativeEvent)).toBe(13);
43 + });
44 + });
45 +
46 + describe('when charCode is not 13', function() {
47 + it('returns 0', function() {
48 + var nativeEvent = new KeyboardEvent('keypress', {charCode: 31});
49 +
50 + expect(getEventCharCode(nativeEvent)).toBe(0);
51 + });
52 + });
53 + });
54 + });
55 + });
56 +
57 + /**
58 + nativeEvent is represented as a plain object here to ease testing, because
59 + KeyboardEvent's 'charCode' event key cannot be deleted to simulate a missing
60 + charCode key.
61 + */
62 + describe('when charCode is not present in nativeEvent', function() {
63 + describe('when keyCode is 32 or bigger', function() {
64 + it('returns keyCode', function() {
65 + var nativeEvent = {'keyCode': 32};
66 +
67 + expect(getEventCharCode(nativeEvent)).toBe(32);
68 + });
69 + });
70 +
71 + describe('when keyCode is smaller than 32', function() {
72 + describe('when keyCode is 13', function() {
73 + it('returns 13', function() {
74 + var nativeEvent = {'keyCode': 13};
75 +
76 + expect(getEventCharCode(nativeEvent)).toBe(13);
77 + });
78 + });
79 +
80 + describe('when keyCode is not 13', function() {
81 + it('returns 0', function() {
82 + var nativeEvent = {'keyCode': 31};
83 +
84 + expect(getEventCharCode(nativeEvent)).toBe(0);
85 + });
86 + });
87 + });
88 + });
89 +});