Here's an interesting thing about
@ariakitjs test files: there's no reference to React or JSX. Rather, they focus solely on testing DOM functionality (1st image).
To accomplish this, the tested component is imported and rendered "before each" test in a separate setup file (2nd image).
Do you know what this means?
Yes, we can potentially reuse this file to test the same component written with any framework, not only React.
ALT The following code:
test("show on click", async () => {
expect(getPopover()).not.toBeVisible();
await click(getCombobox());
expect(getPopover()).toBeVisible();
expect(getOption("🍎 Apple")).not.toHaveFocus();
});
test("label click", async () => {
expect(getPopover()).not.toBeVisible();
await click(getByText("Your favorite fruit"));
expect(getPopover()).not.toBeVisible();
});
test("show on arrow down key", async () => {
await press.Tab();
expect(getPopover()).not.toBeVisible();
await press.ArrowDown();
expect(getPopover()).toBeVisible();
expect(getOption("🍎 Apple")).not.toHaveFocus();
});
test("show on arrow up key", async () => {
await press.Tab();
expect(getPopover()).not.toBeVisible();
await press.ArrowUp();
expect(getPopover()).toBeVisible();
expect(getOption("🍉 Watermelon")).not.toHaveFocus();
});
ALT The following code:
beforeEach(async ({ meta }) => {
const filename = meta.file?.name;
if (!filename) return;
const match = filename.match(/examples\/(.*)\/test.ts$/);
if (!match) return;
const [, example] = match;
const { default: comp } = await import(`./examples/${example}/index.tsx`);
const { unmount } = render(createElement(comp));
return unmount;
});