optimizely-oui
Version:
Optimizely's Component Library.
89 lines (83 loc) • 4.01 kB
JavaScript
;
var _react = _interopRequireDefault(require("react"));
var _index = _interopRequireDefault(require("../index"));
var _enzyme = require("enzyme");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
describe("components/ButterBar", function () {
it("should render text passed in as children", function () {
var message = "Hello! This is a short butterbar.";
var component = (0, _enzyme.shallow)(_react["default"].createElement(_index["default"], null, message));
expect(component.text()).toBe(message);
});
it("should render dismiss button when prop is provided", function () {
var component = (0, _enzyme.mount)(_react["default"].createElement(_index["default"], {
isDismissible: true,
testSection: "foo"
}, "'Hello! This is a short butterbar.'"));
expect(component.find('[data-test-section="foo-dismiss"]').length).toBe(1);
});
it("should call dismiss button click handler when prop is provided", function () {
var onDismiss = jest.fn();
var component = (0, _enzyme.mount)(_react["default"].createElement(_index["default"], {
isDismissible: true,
testSection: "foo",
onDismiss: onDismiss
}, "'Hello! This is a short butterbar.'"));
expect(component.find('[data-test-section="foo-dismiss"]').length).toBe(1);
expect(onDismiss).toHaveBeenCalledTimes(0);
component.find('[data-test-section="foo-dismiss"]').simulate("click");
expect(onDismiss).toHaveBeenCalledTimes(1);
});
it("should not render dismiss button by default", function () {
var component = (0, _enzyme.mount)(_react["default"].createElement(_index["default"], {
testSection: "butterbar"
}, "'Hello! This is a short butterbar.'"));
expect(component.find('[data-test-section="foo-dismiss"]').length).toBe(0);
});
it("should have a properly set role attribute", function () {
var component = (0, _enzyme.shallow)(_react["default"].createElement(_index["default"], null, "'Hello! This is a short butterbar.'"));
expect(component.is('[role="alert"]')).toBe(true);
});
it("should have aria-label if type is provided", function () {
var component = (0, _enzyme.shallow)(_react["default"].createElement(_index["default"], {
type: "brand"
}, "'Hello! This is a short butterbar.'"));
expect(component.is("[aria-label]")).toBe(true);
});
/**
* Dismissible ButterBar components should use button element since it
* triggers an action instead of navigating elsewhere.
*/
it("should use an HTML button element for a close button", function () {
var component = (0, _enzyme.mount)(_react["default"].createElement(_index["default"], {
isDismissible: true,
testSection: "foo"
}, "'Hello! This is a short butterbar.'"));
expect(component.find('button[data-test-section="foo-dismiss"]').length).toBe(1);
});
it("should have a properly set test section", function () {
var component = (0, _enzyme.shallow)(_react["default"].createElement(_index["default"], {
testSection: "foo"
}, "'Hello! This is a short butterbar.'"));
expect(component.is('[data-test-section="foo"]')).toBe(true);
});
describe("when given a className", function () {
it("appends it to the root element", function () {
var component = (0, _enzyme.shallow)(_react["default"].createElement(_index["default"], {
className: "extra-class"
}, "Text"));
expect(component.prop("className").includes("extra-class")).toBe(true);
});
});
describe("when given an unknown prop", function () {
it("passes it to the root element", function () {
// @ts-ignore: `unknownProps` is not part of any typed interface and therefore a good test value.
var component = (0, _enzyme.shallow)(_react["default"].createElement(_index["default"], {
unknownProp: "some value"
}, "Text"));
expect(component).toHaveProp({
unknownProp: "some value"
});
});
});
});