@salesforce/design-system-react
Version:
Salesforce Lightning Design System for React
279 lines (261 loc) • 8.47 kB
JavaScript
;
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _lodash = _interopRequireDefault(require("lodash.assign"));
var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));
var _chai = require("chai");
var _menuPicklist = _interopRequireDefault(require("../../menu-picklist"));
var _iconSettings = _interopRequireDefault(require("../../icon-settings"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint-disable react/no-render-return-value */
var Simulate = _testUtils.default.Simulate,
scryRenderedDOMComponentsWithTag = _testUtils.default.scryRenderedDOMComponentsWithTag,
findRenderedDOMComponentWithClass = _testUtils.default.findRenderedDOMComponentWithClass;
describe('SLDSMenuPicklist: ', function () {
var body;
var options = [{
label: 'A Option Option Super Super Long',
value: 'A0',
title: 'Greg'
}, {
label: 'B Option',
value: 'B0'
}];
var renderPicklist = function renderPicklist(inst) {
body = document.createElement('div');
document.body.appendChild(body);
return _reactDom.default.render(_react.default.createElement(_iconSettings.default, {
iconPath: "/assets/icons"
}, inst), body);
};
function removePicklist() {
_reactDom.default.unmountComponentAtNode(body);
document.body.removeChild(body);
}
var defaultProps = {
modal: false,
options: options,
placeholder: 'Select a contact',
value: 'C0'
};
var createPicklist = function createPicklist(props) {
return _react.default.createElement(_menuPicklist.default, (0, _lodash.default)({}, defaultProps, props));
};
var getPicklist = function getPicklist(props) {
return renderPicklist(createPicklist(props));
};
var getMenu = function getMenu(dom) {
return dom.querySelector('.slds-dropdown');
};
var clickOnItem = function clickOnItem(cmp, index) {
var items = scryRenderedDOMComponentsWithTag(cmp, 'a');
Simulate.click(items[index]);
};
describe('in modal mode', function () {
var cmp;
var btn;
beforeEach(function () {
cmp = getPicklist({
modal: true
});
btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
});
afterEach(function () {
removePicklist();
});
it('expands/contracts the dropdown on click', function () {
(0, _chai.expect)(getMenu(document.body)).to.equal(null);
Simulate.click(btn, {});
(0, _chai.expect)(getMenu(document.body).className).to.include('slds-dropdown_left');
Simulate.click(btn, {});
(0, _chai.expect)(getMenu(document.body)).to.equal(null);
});
});
describe('with click handler', function () {
var cmp;
var btn;
var clicked;
beforeEach(function () {
clicked = false;
cmp = getPicklist({
onClick: function onClick() {
clicked = true;
}
});
btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
});
afterEach(function () {
removePicklist();
});
it('gives the button correct aria properties', function () {
(0, _chai.expect)(btn.getAttribute('aria-haspopup')).to.equal('true');
});
it('sets the placeholder', function () {
(0, _chai.expect)(btn.textContent).to.equal('Select a contact');
});
it('expands/contracts the dropdown on click', function () {
(0, _chai.expect)(getMenu(body)).to.equal(null);
Simulate.click(btn, {});
(0, _chai.expect)(getMenu(body).className).to.include('slds-dropdown');
Simulate.click(btn, {});
(0, _chai.expect)(getMenu(body)).to.equal(null);
});
it('preserves click behavior', function () {
(0, _chai.expect)(clicked).to.be.false;
Simulate.click(btn, {});
(0, _chai.expect)(clicked).to.be.true;
});
});
describe('expanded with onSelect', function () {
var cmp;
var btn;
var clicked;
var selected;
beforeEach(function () {
selected = false;
cmp = getPicklist({
onSelect: function onSelect(i) {
selected = i;
}
});
btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
Simulate.click(btn, {});
});
afterEach(function () {
removePicklist();
});
it('selects an item', function () {
(0, _chai.expect)(selected).to.be.false;
var items = getMenu(body).querySelectorAll('.slds-dropdown__item');
Simulate.click(items[1].querySelector('a'), {});
(0, _chai.expect)(selected.value).to.equal('B0');
});
});
describe('disabled', function () {
var cmp;
var btn;
var clicked;
beforeEach(function () {
clicked = false;
cmp = getPicklist({
disabled: true,
onClick: function onClick() {
clicked = true;
}
});
btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
});
afterEach(function () {
removePicklist();
});
it("doesn't open", function () {
Simulate.click(btn, {});
(0, _chai.expect)(getMenu(body)).to.equal(null);
});
it('prevents click behavior', function () {
(0, _chai.expect)(clicked).to.be.false;
Simulate.click(btn, {});
(0, _chai.expect)(clicked).to.be.false;
});
});
describe('accessible markup', function () {
var cmp;
var btn;
var clicked;
var selected;
beforeEach(function () {
selected = false;
cmp = getPicklist({
onSelect: function onSelect(i) {
selected = i;
}
});
btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
});
afterEach(function () {
removePicklist();
});
it('<ul> has role menu & aria-labelledby', function () {
Simulate.click(btn, {});
var ulRole = getMenu(body).querySelector('ul').getAttribute('role');
var ulAria = getMenu(body).querySelector('ul').getAttribute('aria-labelledby');
(0, _chai.expect)(ulRole).to.equal('menu');
(0, _chai.expect)(ulAria).to.equal(btn.getAttribute('id'));
});
it('<a> inside <li> has role menuitem', function () {
Simulate.click(btn, {});
var items = getMenu(body).querySelectorAll('.slds-dropdown__item a');
var anchorRole = items[1].getAttribute('role');
var match = anchorRole === 'menuitem' || anchorRole === 'menuitemradio' || anchorRole === 'menuitemcheckbox';
(0, _chai.expect)(match).to.be.true;
});
});
describe('Keyboard behavior', function () {
var cmp;
var btn;
var selected;
beforeEach(function () {
selected = false;
cmp = getPicklist({
onSelect: function onSelect(i) {
selected = i;
}
});
btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
});
afterEach(function () {
removePicklist();
});
it('opens menu with enter', function () {
(0, _chai.expect)(getMenu(body)).to.equal(null);
Simulate.keyDown(btn, {
key: 'Enter',
keyCode: 13,
which: 13
});
(0, _chai.expect)(getMenu(body)).to.not.equal(null);
});
it('opens menu with down arrow key', function () {
(0, _chai.expect)(getMenu(body)).to.equal(null);
Simulate.keyDown(btn, {
key: 'Down',
keyCode: 40,
which: 40
});
(0, _chai.expect)(getMenu(body)).to.not.equal(null);
});
it('selects an item with keyboard', function () {
Simulate.click(btn, {});
(0, _chai.expect)(selected).to.be.false;
var menu = getMenu(body);
Simulate.keyDown(menu, {
key: 'Down',
keyCode: 40,
which: 40
});
Simulate.keyDown(menu, {
key: 'Down',
keyCode: 40,
which: 40
});
Simulate.keyDown(menu, {
key: 'Enter',
keyCode: 13,
which: 13
});
(0, _chai.expect)(selected.value).to.equal('B0');
});
it('closes Menu on esc', function () {
(0, _chai.expect)(getMenu(body)).to.equal(null);
Simulate.click(btn, {});
(0, _chai.expect)(getMenu(body)).to.not.equal(null);
var menuItems = getMenu(body).querySelectorAll('.slds-dropdown__item');
Simulate.keyDown(menuItems[1].querySelector('a'), {
key: 'Esc',
keyCode: 27,
which: 27
});
(0, _chai.expect)(getMenu(body)).to.equal(null);
});
});
});