Skip to content
This repository was archived by the owner on Oct 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions files/framework/astro-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
},
"dependencies": {
"@astrojs/react": "^6",
"@dnd-kit/core": "^6",
"@dnd-kit/sortable": "^10",
"@dnd-kit/utilities": "^3",
"@nanostores/react": "^1",
"astro": "^7",
"mdast-util-gfm": "^3",
Expand Down
73 changes: 13 additions & 60 deletions files/framework/astro-react/src/components/tasks/categorize.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
}

.categorize-categories {
display: grid;
display: flex;
flex-direction: column;
gap: var(--space-md);
}

Expand All @@ -16,79 +17,31 @@
}

.zone {
position: relative;
border: 1px solid var(--quaternary-background);
border-radius: var(--radius-md);
padding: var(--space-md);
padding: var(--space-sm);
}

.zone-heading {
position: relative;
z-index: 1;
pointer-events: none;
margin-bottom: var(--space-sm);
margin-bottom: var(--space-xs);
font-size: var(--text-size-sm);
}

.zone-items {
position: relative;
z-index: 1;
pointer-events: none;
display: flex;
flex-direction: column;
gap: var(--space-xs);
min-height: 2.5rem;
margin-bottom: var(--space-sm);
}

.item-row {
pointer-events: auto;
display: block;
width: 100%;
text-align: left;
background: none;
border: 1px solid var(--quaternary-background);
border-radius: var(--radius-sm);
padding: var(--space-xs) var(--space-sm);
font: inherit;
color: inherit;
cursor: pointer;
}

.item-row:not(:disabled):hover {
background: var(--quaternary-background);
}

.item-row.selected {
background: var(--quaternary-background);
border-color: var(--highlight-color);
font-weight: 700;
}

.item-row:disabled {
cursor: not-allowed;
}

.unshuffled .item-row {
visibility: hidden;
}

.zone-drop {
position: absolute;
inset: 0;
z-index: 0;
width: 100%;
height: 100%;
background: none;
border: none;
border-radius: var(--radius-md);
cursor: pointer;
min-height: 2rem;
}

.zone-drop.active:not(:disabled):hover {
background: var(--quaternary-background);
.zone-empty {
border-style: dashed;
}

.zone-drop:disabled {
cursor: default;
.zone-placeholder {
margin: 0;
color: var(--quaternary-color);
font-size: var(--text-size-xs);
font-style: italic;
text-align: center;
}
196 changes: 88 additions & 108 deletions files/framework/astro-react/src/components/tasks/categorize.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Categorize } from "./categorize";
import { Categorize, findContainer, moveItem } from "./categorize";
import type { Task } from "../../lib/curriculum-tasks";
import { dragPointerTo, waitOutDragClickGuard } from "../../test-utils/dnd";

const task: Extract<Task, { type: "categorize" }> = {
type: "categorize",
Expand All @@ -20,153 +21,132 @@ beforeEach(() => {
function getZoneItems(zoneName: string): string[] {
const zone = screen.getByRole("group", { name: zoneName });
return within(zone)
.queryAllByRole("button", { pressed: undefined })
.filter((button) => button.getAttribute("aria-label") === null)
.map((button) => button.textContent ?? "");
.queryAllByText((_, element) => element?.className === "item-card")
.map((element) => element.textContent ?? "");
}

describe(Categorize, () => {
it("starts with all items in the unplaced zone", () => {
render(<Categorize task={task} onCorrect={() => {}} />);
// Each zone gets its own, non-overlapping vertical band so the pointer
// coordinates land unambiguously in the intended target zone.
const ZONE_TOP: Record<string, number> = { Items: 0, Fruit: 200, Vegetable: 400 };

expect(getZoneItems("Items")).toEqual(
expect.arrayContaining(["Apple", "Banana", "Carrot"]),
);
function dragItemToZone(itemText: string, zoneName: string) {
const item = screen.getByText(itemText);
const zone = screen.getByRole("group", { name: zoneName });
const top = ZONE_TOP[zoneName];

dragPointerTo(item, zone, { top, left: 0, bottom: top + 150, right: 300 });
}

describe("findContainer", () => {
it("finds the container an item currently belongs to", () => {
const containers = { unplaced: ["a"], Fruit: ["b"] };

expect(findContainer(containers, "b")).toBe("Fruit");
});

it("moves a selected item to a category zone", async () => {
const user = userEvent.setup();
it("treats a container id as its own container", () => {
const containers = { unplaced: [], Fruit: [] };

render(<Categorize task={task} onCorrect={() => {}} />);
expect(findContainer(containers, "Fruit")).toBe("Fruit");
});

await user.click(screen.getByRole("button", { name: "Apple" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to fruit/i,
}),
);
it("returns undefined for an id that isn't in any container", () => {
const containers = { unplaced: ["a"] };

expect(getZoneItems("Fruit")).toContain("Apple");
expect(getZoneItems("Items")).not.toContain("Apple");
expect(findContainer(containers, "z")).toBeUndefined();
});
});

it("moves an item back to unplaced when removed from a category", async () => {
const user = userEvent.setup();
describe("moveItem", () => {
it("moves an item from one container to another", () => {
const containers = { unplaced: ["a", "b"], Fruit: [] };

const result = moveItem(containers, "a", "unplaced", "Fruit");

expect(result.unplaced).toEqual(["b"]);
expect(result.Fruit).toEqual(["a"]);
});

it("appends the item, since order within a container is never scored", () => {
const containers = { unplaced: ["a"], Fruit: ["b", "c"] };

const result = moveItem(containers, "a", "unplaced", "Fruit");

expect(result.Fruit).toEqual(["b", "c", "a"]);
});
});

describe(Categorize, () => {
it("starts with all items in the unplaced zone and categories empty", () => {
render(<Categorize task={task} onCorrect={() => {}} />);

// Place Apple in Fruit
await user.click(screen.getByRole("button", { name: "Apple" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to fruit/i,
}),
expect(getZoneItems("Items")).toEqual(
expect.arrayContaining(["Apple", "Banana", "Carrot"]),
);
expect(getZoneItems("Fruit")).toHaveLength(0);
expect(getZoneItems("Vegetable")).toHaveLength(0);
});

// Move Apple back to unplaced
await user.click(screen.getByRole("button", { name: "Apple" }));
await user.click(
screen.getByRole("button", {
name: /move selected item back to the unplaced items/i,
}),
);
it("shows a drop-here placeholder for an empty zone", () => {
render(<Categorize task={task} onCorrect={() => {}} />);

expect(getZoneItems("Items")).toContain("Apple");
expect(getZoneItems("Fruit")).not.toContain("Apple");
expect(within(screen.getByRole("group", { name: "Fruit" })).getByText("Drop here")).toBeInTheDocument();
});

it("shows correct feedback and calls onCorrect when all items are placed correctly", async () => {
const onCorrect = vi.fn<() => void>();
it("shows unanswered feedback when not all items are placed", async () => {
const user = userEvent.setup();
render(<Categorize task={task} onCorrect={() => {}} />);

render(<Categorize task={task} onCorrect={onCorrect} />);
await user.click(screen.getByRole("button", { name: /check answer/i }));

await user.click(screen.getByRole("button", { name: "Apple" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to fruit/i,
}),
);
await user.click(screen.getByRole("button", { name: "Banana" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to fruit/i,
}),
);
await user.click(screen.getByRole("button", { name: "Carrot" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to vegetable/i,
}),
);
expect(screen.getByText("Place all items before checking.")).toBeInTheDocument();
});

await user.click(
screen.getByRole("button", { name: /check answer/i }),
);
it("moves an item into a category by dragging it", () => {
render(<Categorize task={task} onCorrect={() => {}} />);

expect(screen.getByRole("status")).toHaveTextContent("Correct!");
expect(onCorrect).toHaveBeenCalledOnce();
dragItemToZone("Apple", "Fruit");

expect(getZoneItems("Fruit")).toContain("Apple");
expect(getZoneItems("Items")).not.toContain("Apple");
});

it("shows incorrect feedback for wrong placements", async () => {
it("shows correct feedback and calls onCorrect once everything is placed correctly", async () => {
const onCorrect = vi.fn<() => void>();
const user = userEvent.setup();
render(<Categorize task={task} onCorrect={onCorrect} />);

render(<Categorize task={task} onCorrect={() => {}} />);

// Place all items, but Apple in wrong category
await user.click(screen.getByRole("button", { name: "Apple" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to vegetable/i,
}),
);
await user.click(screen.getByRole("button", { name: "Banana" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to fruit/i,
}),
);
await user.click(screen.getByRole("button", { name: "Carrot" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to vegetable/i,
}),
);
dragItemToZone("Apple", "Fruit");
dragItemToZone("Banana", "Fruit");
dragItemToZone("Carrot", "Vegetable");

await user.click(
screen.getByRole("button", { name: /check answer/i }),
);
await waitOutDragClickGuard();
await user.click(screen.getByRole("button", { name: /check answer/i }));

expect(screen.getByRole("status")).toHaveTextContent(
"Not quite. Try again.",
);
expect(screen.getByText("Correct!")).toBeInTheDocument();
expect(onCorrect).toHaveBeenCalledOnce();
});

it("shows unanswered feedback when not all items are placed", async () => {
it("shows incorrect feedback for a wrong placement", async () => {
const user = userEvent.setup();

render(<Categorize task={task} onCorrect={() => {}} />);

await user.click(
screen.getByRole("button", { name: /check answer/i }),
);
dragItemToZone("Apple", "Vegetable");
dragItemToZone("Banana", "Fruit");
dragItemToZone("Carrot", "Fruit");

expect(screen.getByRole("status")).toHaveTextContent(
"Place all items before checking.",
);
await waitOutDragClickGuard();
await user.click(screen.getByRole("button", { name: /check answer/i }));

expect(screen.getByText("Not quite. Try again.")).toBeInTheDocument();
});

it("resets all items to unplaced", async () => {
it("resets all items back to unplaced", async () => {
const user = userEvent.setup();

render(<Categorize task={task} onCorrect={() => {}} />);

await user.click(screen.getByRole("button", { name: "Apple" }));
await user.click(
screen.getByRole("button", {
name: /move selected item to fruit/i,
}),
);
dragItemToZone("Apple", "Fruit");
await waitOutDragClickGuard();
await user.click(screen.getByRole("button", { name: /reset/i }));

expect(getZoneItems("Items")).toHaveLength(3);
Expand Down
Loading