@samitouri / QOS-React / commits / 07824258c6

getEventKey unit tests

Adds unit tests to getEventKey function.

cbrwizard committed Mar 16, 2016 at 22:07 UTC 07824258c633467ade69341da68d305bc3e3f2a6
1 file changed +80
src/renderers/dom/client/utils/__tests__/getEventKey-test.js new
+80
@@ -0,0 +1,80 @@
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 getEventKey = require('getEventKey');
15 +
16 +describe('getEventKey', function() {
17 + describe('when key is implemented in a browser', function() {
18 + describe('when key is not normalized', function() {
19 + it('returns a normalized value', function() {
20 + var nativeEvent = new KeyboardEvent('keypress', {key: 'Del'});
21 +
22 + expect(getEventKey(nativeEvent)).toBe('Delete');
23 + });
24 + });
25 +
26 + describe('when key is normalized', function() {
27 + it('returns a key', function() {
28 + var nativeEvent = new KeyboardEvent('keypress', {key: 'f'});
29 +
30 + expect(getEventKey(nativeEvent)).toBe('f');
31 + });
32 + });
33 + });
34 +
35 + describe('when key is not implemented in a browser', function() {
36 + describe('when event type is keypress', function() {
37 + describe('when charCode is 13', function() {
38 + it("returns 'Enter'", function() {
39 + var nativeEvent = new KeyboardEvent('keypress', {charCode: 13});
40 +
41 + expect(getEventKey(nativeEvent)).toBe('Enter');
42 + });
43 + });
44 +
45 + describe('when charCode is not 13', function() {
46 + it('returns a string from a charCode', function() {
47 + var nativeEvent = new KeyboardEvent('keypress', {charCode: 65});
48 +
49 + expect(getEventKey(nativeEvent)).toBe('A');
50 + });
51 + });
52 + });
53 +
54 + describe('when event type is keydown or keyup', function() {
55 + describe('when keyCode is recognized', function() {
56 + it('returns a translated key', function() {
57 + var nativeEvent = new KeyboardEvent('keydown', {keyCode: 45});
58 +
59 + expect(getEventKey(nativeEvent)).toBe('Insert');
60 + });
61 + });
62 +
63 + describe('when keyCode is not recognized', function() {
64 + it('returns Unidentified', function() {
65 + var nativeEvent = new KeyboardEvent('keydown', {keyCode: 1337});
66 +
67 + expect(getEventKey(nativeEvent)).toBe('Unidentified');
68 + });
69 + });
70 + });
71 +
72 + describe('when event type is unknown', function() {
73 + it('returns an empty string', function() {
74 + var nativeEvent = new KeyboardEvent('keysmack');
75 +
76 + expect(getEventKey(nativeEvent)).toBe('');
77 + });
78 + });
79 + });
80 +});