@crossed/primitive
Version:
A universal & performant styling library for React Native, Next.js & React
103 lines (102 loc) • 5.33 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var import_jsx_runtime = require("react/jsx-runtime");
var import_jest_dom = require("@testing-library/jest-dom");
var import_test = require("@crossed/test");
var import_Button = require("../Button");
var import_react = __toESM(require("react"));
var import_context = require("../context");
var import_contextCollection = require("../contextCollection");
var import_RovingFocus = require("../../utils/RovingFocus");
jest.mock("../contextCollection");
jest.mock("../context");
jest.mock("../../utils/RovingFocus");
const ProviderMocked = import_context.Provider;
const ItemSlotMocked = import_contextCollection.ButtonGroupCollectionItemSlot.render;
const RovingItemMocked = import_RovingFocus.Item.render;
const Comp = (0, import_react.forwardRef)((p, ref) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...p, ref }));
const NewComp = (0, import_Button.createButtonMain)(Comp);
describe("createButtonMain", () => {
const oldUseId = import_react.default.useId;
beforeEach(() => {
import_react.default.useId = jest.fn(() => "id");
ProviderMocked.mockImplementation(({ children }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children }));
ItemSlotMocked.mockImplementation(({ children }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children }));
RovingItemMocked.mockImplementation(({ children }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children }));
});
afterEach(() => {
import_react.default.useId = oldUseId;
ProviderMocked.mockReset();
ItemSlotMocked.mockReset();
RovingItemMocked.mockReset();
});
afterAll(() => {
jest.restoreAllMocks();
});
test("not disabled", async () => {
const child = "Pass child";
(0, import_test.render)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(NewComp, { children: child }));
expect(import_react.default.useId).toHaveBeenCalled();
expect(ProviderMocked).toHaveBeenCalled();
expect(ProviderMocked.mock.calls[0][0]).toHaveProperty("id", "id");
expect(ProviderMocked.mock.calls[0][0]).toHaveProperty("children");
expect(ItemSlotMocked).toHaveBeenCalled();
expect(ItemSlotMocked.mock.calls[0][0]).toHaveProperty("id", "id");
expect(ItemSlotMocked.mock.calls[0][0]).toHaveProperty("children");
expect(RovingItemMocked).toHaveBeenCalled();
expect(RovingItemMocked.mock.calls[0][0]).toHaveProperty("focusable", true);
expect(RovingItemMocked.mock.calls[0][0]).toHaveProperty("children");
const button = await import_test.screen.getByRole("button");
expect(button).toHaveTextContent(child);
expect(button).toHaveAttribute("aria-labelledby", "id");
expect(button).toHaveAttribute("aria-disabled", "false");
expect(button).toHaveAttribute("id", "id");
expect(button).toHaveAttribute("tabIndex", "0");
});
test("disabled", async () => {
const child = "Pass child";
(0, import_test.render)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(NewComp, { disabled: true, children: child }));
expect(import_react.default.useId).toHaveBeenCalled();
expect(ProviderMocked).toHaveBeenCalled();
expect(ProviderMocked.mock.calls[0][0]).toHaveProperty("id", "id");
expect(ProviderMocked.mock.calls[0][0]).toHaveProperty("children");
expect(ItemSlotMocked).toHaveBeenCalled();
expect(ItemSlotMocked.mock.calls[0][0]).toHaveProperty("id", "id");
expect(ItemSlotMocked.mock.calls[0][0]).toHaveProperty("children");
expect(RovingItemMocked).toHaveBeenCalled();
expect(RovingItemMocked.mock.calls[0][0]).toHaveProperty(
"focusable",
false
);
expect(RovingItemMocked.mock.calls[0][0]).toHaveProperty("children");
const button = await import_test.screen.getByRole("button");
expect(button).toHaveTextContent(child);
expect(button).toHaveAttribute("aria-labelledby", "id");
expect(button).toHaveAttribute("aria-disabled", "true");
expect(button).toHaveAttribute("disabled", "");
expect(button).toHaveAttribute("id", "id");
expect(button).toHaveAttribute("tabIndex", "-1");
});
});
//# sourceMappingURL=Button.spec.js.map