import * as React from "react";
import { render } from "reakit-test-utils";
import {
unstable_IdProps as IdProps,
unstable_IdProvider as IdProvider,
unstable_useIdState as useIdState,
unstable_useId as useId,
unstable_Id as Id,
} from "..";
// Basically puts the id prop into the aria-label prop
function TestId(props: IdProps) {
return (
{(htmlProps) => (
{props.children}
)}
);
}
test("Id", () => {
const Test = () => {
return (
<>
>
);
};
const { getAllByLabelText, rerender } = render( );
const ids = getAllByLabelText(/id-[a-z\d]{2,}$/).map((el) => el.id);
expect(ids).toHaveLength(2);
// shouldn't change ids
rerender( );
const nextIds = getAllByLabelText(/id-[a-z\d]{2,}$/).map((el) => el.id);
expect(ids).toEqual(nextIds);
});
test("Id with baseId", () => {
const Test = () => {
return (
<>
>
);
};
const { getAllByLabelText, rerender } = render( );
const ids = getAllByLabelText(/a-[a-z\d]{2,}$/).map((el) => el.id);
expect(ids).toHaveLength(2);
// shouldn't change ids
rerender( );
const nextIds = getAllByLabelText(/a-[a-z\d]{2,}$/).map((el) => el.id);
expect(ids).toEqual(nextIds);
});
test("Id within IdProvider", () => {
const Test = () => {
return (
);
};
const { container } = render( );
// IDs will be sequential because it's handled by the Provider
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id with baseId within IdProvider", () => {
const Test = () => {
return (
);
};
const { container } = render( );
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id within IdProvider with prefix", () => {
const Test = () => {
return (
);
};
const { container } = render( );
// IDs will be sequential because it's handled by the Provider
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id with useIdState", () => {
const Test = () => {
const id = useIdState();
return (
<>
>
);
};
const { getByLabelText, rerender } = render( );
const { id: id1 } = getByLabelText(/id-[a-z\d]{2,}-1$/);
const { id: id2 } = getByLabelText(/id-[a-z\d]{2,}-2$/);
// shouldn't change ids
rerender( );
getByLabelText(id1);
getByLabelText(id2);
});
test("Id with useIdState within IdProvider", () => {
const Test = () => {
const id = useIdState();
return (
<>
>
);
};
const { container } = render(
);
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id with useIdState within IdProvider with prefix", () => {
const Test = () => {
const id = useIdState();
return (
<>
>
);
};
const { container } = render(
);
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id with useIdState with baseId", () => {
const Test = () => {
const id = useIdState({ baseId: "a" });
return (
<>
>
);
};
const { container } = render( );
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id with useIdState with baseId within IdProvider", () => {
const Test = () => {
const id = useIdState({ baseId: "a" });
return (
<>
>
);
};
const { container } = render(
);
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id with useIdState with baseId within IdProvider with prefix", () => {
const Test = () => {
const id = useIdState({ baseId: "b" });
return (
<>
>
);
};
const { container } = render(
);
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id within IdProvider with useIdState with baseId", () => {
const Test = () => {
const id = useIdState({ baseId: "b" });
return (
);
};
const { container } = render( );
expect(container).toMatchInlineSnapshot(`
`);
});
test("Id within IdProvider with prefix with useIdState with baseId", () => {
const Test = () => {
const id = useIdState({ baseId: "b" });
return (
);
};
const { container } = render( );
expect(container).toMatchInlineSnapshot(`
`);
});
test("useId", () => {
const Test = () => {
const { id } = useId();
return ;
};
const { getByLabelText, rerender } = render( );
const { id } = getByLabelText(/id-[a-z\d]{2,}$/);
// shouldn't change ids
rerender( );
const { id: nextId } = getByLabelText(/id-[a-z\d]{2,}$/);
expect(id).toBe(nextId);
});
test("useId with id option", () => {
const Test = (props: React.HTMLAttributes) => {
const { id } = useId(props);
return ;
};
const { getByLabelText } = render( );
getByLabelText("a");
});
test("useId with id prop", () => {
const Test = (props: React.HTMLAttributes) => {
const { id } = useId({}, props);
return ;
};
const { getByLabelText } = render( );
getByLabelText("a");
});
test("useId with id prop and option", () => {
const Test = (props: React.HTMLAttributes) => {
const { id } = useId({ id: "b" }, props);
return ;
};
const { getByLabelText } = render( );
getByLabelText("a");
});