@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
91 lines (82 loc) • 3.81 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _A11yHelper = require("./A11yHelper");
var _sinon = _interopRequireDefault(require("sinon"));
//
// Copyright IBM Corp. 2020, 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//
var sinonSandbox = _sinon.default.createSandbox();
var EVENTS = {
CLICK: {
type: _A11yHelper.keys.CLICK
},
SPACE: {
key: _A11yHelper.keys.SPACE
},
ENTER: {
key: _A11yHelper.keys.ENTER
},
TAB: {
key: _A11yHelper.keys.TAB
}
};
describe('A11yHelper - functions', function () {
afterEach(function () {
sinonSandbox.reset();
});
it('keyAndClickHandler only triggers callback with correct events', function () {
var mockCallback = sinonSandbox.stub();
var handleFunction = (0, _A11yHelper.keyAndClickHandler)(mockCallback);
handleFunction();
expect(mockCallback.callCount).toBe(0);
handleFunction(EVENTS.CLICK);
expect(mockCallback.callCount).toBe(1);
handleFunction(EVENTS.SPACE);
expect(mockCallback.callCount).toBe(2);
handleFunction(EVENTS.ENTER);
expect(mockCallback.callCount).toBe(3);
});
it('keyAndClickHandler triggers correctly with custom valid keys', function () {
var mockCallback = sinonSandbox.stub();
var validKeys = [_A11yHelper.keys.TAB, _A11yHelper.keys.ENTER];
var handleFunction = (0, _A11yHelper.keyAndClickHandler)(mockCallback, validKeys);
handleFunction();
expect(mockCallback.callCount).toBe(0); // doesn't trigger rom blank event
handleFunction(EVENTS.CLICK);
expect(mockCallback.callCount).toBe(0); // doesn't trigger from default click event
handleFunction(EVENTS.TAB);
expect(mockCallback.callCount).toBe(1); // triggers from custom tab event
handleFunction(EVENTS.ENTER);
expect(mockCallback.callCount).toBe(2); // triggers form custom enter event
});
it('createInteractionHandler calls a callback for the correct default events', function () {
var mockCallback = sinonSandbox.stub();
var interactionHandler = (0, _A11yHelper.createInteractionHandler)(mockCallback);
expect(interactionHandler.onKeyPress).toBeUndefined(); // onKeyPress is not included in default set of handlers
interactionHandler.onKeyDown(EVENTS.TAB);
expect(mockCallback.callCount).toBe(0); // tab is not included in default set of valid keys, don't expect callback to be called
interactionHandler.onClick(EVENTS.CLICK);
expect(mockCallback.callCount).toBe(1);
interactionHandler.onKeyDown(EVENTS.ENTER);
expect(mockCallback.callCount).toBe(2);
interactionHandler.onKeyDown(EVENTS.SPACE);
expect(mockCallback.callCount).toBe(3);
});
it('createInteractionHandler calls a callback for the correct custom events', function () {
var mockCallback = sinonSandbox.stub();
var validHandlers = [_A11yHelper.handlers.onKeyPress];
var validKeys = [_A11yHelper.keys.TAB, _A11yHelper.keys.ENTER];
var interactionHandler = (0, _A11yHelper.createInteractionHandler)(mockCallback, validHandlers, validKeys);
expect(interactionHandler.onKeyDown).toBeUndefined(); // onKeyDown is not included in custom set of handlers
expect(interactionHandler.onClick).toBeUndefined(); // onClick is not included in custom set of handlers
interactionHandler.onKeyPress(EVENTS.ENTER);
expect(mockCallback.callCount).toBe(1); // onKeyPress is not included in default set of handlers, don't expect callback to be called
interactionHandler.onKeyPress(EVENTS.TAB);
expect(mockCallback.callCount).toBe(2);
interactionHandler.onKeyPress(EVENTS.SPACE);
expect(mockCallback.callCount).toBe(2); // space is not part of custom set of keys
});
});