@instructure/ui-react-utils
Version:
A React utility library made by Instructure Inc.
89 lines (87 loc) • 3.2 kB
JavaScript
;
var _vitest = require("vitest");
var _createChainedFunction = require("@instructure/ui-utils/lib/createChainedFunction.js");
var _react = require("@testing-library/react");
require("@testing-library/jest-dom");
var _safeCloneElement = require("../safeCloneElement");
var _jsxRuntime = require("@emotion/react/jsx-runtime");
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
describe('safeCloneElement', () => {
const SafeClone = function ({
element,
props,
children
}) {
return (0, _safeCloneElement.safeCloneElement)(element, props, children);
};
it('should preserve refs', () => {
const origRef = _vitest.vi.fn();
const cloneRef = _vitest.vi.fn();
(0, _react.render)((0, _jsxRuntime.jsx)(SafeClone, {
element: (0, _jsxRuntime.jsx)("div", {
ref: origRef
}),
props: {
ref: cloneRef
}
}));
expect(origRef).toHaveBeenCalled();
expect(cloneRef).toHaveBeenCalled();
});
it('should preserve event handlers', () => {
const onClickA = _vitest.vi.fn();
const onClickB = _vitest.vi.fn();
(0, _react.render)((0, _jsxRuntime.jsx)(SafeClone, {
element: (0, _jsxRuntime.jsx)("button", {
onClick: onClickA
}),
props: {
onClick: onClickB
}
}));
const button = _react.screen.getByRole('button');
button.click();
expect(onClickA).toHaveBeenCalled();
expect(onClickB).toHaveBeenCalled();
});
it('should preserve already chained functions', () => {
const onClickA = _vitest.vi.fn();
const onClickB = _vitest.vi.fn();
const onClickC = _vitest.vi.fn();
(0, _react.render)((0, _jsxRuntime.jsx)(SafeClone, {
element: (0, _jsxRuntime.jsx)("button", {
onClick: onClickA
}),
props: {
onClick: (0, _createChainedFunction.createChainedFunction)(onClickB, onClickC)
}
}));
const button = _react.screen.getByRole('button');
button.click();
expect(onClickA).toHaveBeenCalled();
expect(onClickB).toHaveBeenCalled();
expect(onClickC).toHaveBeenCalled();
});
});