Skip to content

Commit

Permalink
Refactor CheckpointMenu tests and update UI text
Browse files Browse the repository at this point in the history
  • Loading branch information
coolcline committed Mar 8, 2025
1 parent 497dc20 commit 0c5e515
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,81 +19,86 @@ describe("CheckpointMenu", () => {
jest.clearAllMocks()
})

it("renders checkpoint diff button", () => {
it("renders compare button", () => {
render(<CheckpointMenu {...props} />)
expect(screen.getByRole("button", { name: "diff" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Compare" })).toBeInTheDocument()
})

it("renders restore button", () => {
render(<CheckpointMenu {...props} />)
const restoreButton = screen.getByRole("button", { name: "history" })
const restoreButton = screen.getByRole("button", { name: "Restore" })
expect(restoreButton).toBeInTheDocument()
})

it("shows popover content when clicking restore button", () => {
it("shows restore options when clicking restore button", () => {
render(<CheckpointMenu {...props} />)
const restoreButton = screen.getByRole("button", { name: "history" })
const restoreButton = screen.getByRole("button", { name: "Restore" })
fireEvent.click(restoreButton)

expect(screen.getByRole("button", { name: "Restore Files" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Restore Files & Messages" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Restore Messages Only" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Restore Files Only" })).toBeInTheDocument()
})

it("calls checkpoint diff when clicking diff button", () => {
it("calls checkpoint diff when clicking compare button", () => {
render(<CheckpointMenu {...props} />)
const diffButton = screen.getByRole("button", { name: "diff" })
fireEvent.click(diffButton)
const compareButton = screen.getByRole("button", { name: "Compare" })
fireEvent.click(compareButton)

expect(vscode.postMessage).toHaveBeenCalledWith({
type: "checkpointDiff",
payload: { ts: props.ts, commitHash: props.commitHash, mode: "checkpoint" },
payload: {
ts: props.ts,
commitHash: props.commitHash,
mode: "checkpoint",
},
})
})

it("shows confirmation dialog when clicking Restore Files", async () => {
it("shows confirmation dialog when clicking restore options", async () => {
render(<CheckpointMenu {...props} />)

// Open history popover
const historyButton = screen.getByRole("button", { name: "history" })
fireEvent.click(historyButton)

// Wait for the popover to be open
const dialog = await screen.findByRole("dialog")

// Find and click restore files button (exact match)
const restoreButton = await within(dialog).findByRole("button", { name: "Restore Files & Task" })
// Open restore menu
const restoreButton = screen.getByRole("button", { name: "Restore" })
fireEvent.click(restoreButton)

// Use button role and name to find confirmation buttons
const confirmButton = await within(dialog).findByRole("button", { name: /confirm/i })
const cancelButton = await within(dialog).findByRole("button", { name: /cancel/i })
const warningText = await within(dialog).findByText(/this action cannot be undone/i)

expect(confirmButton).toBeInTheDocument()
expect(cancelButton).toBeInTheDocument()
expect(warningText).toBeInTheDocument()
// Click Restore Files & Messages
const restoreFilesAndMessagesButton = screen.getByRole("button", { name: "Restore Files & Messages" })
fireEvent.click(restoreFilesAndMessagesButton)

// Check confirmation dialog
expect(screen.getByText("Warning")).toBeInTheDocument()
expect(
screen.getByText(
/This action will restore project files to the state at this checkpoint and delete all messages after this point. This cannot be undone. Are you sure you want to continue?/,
),
).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Confirm" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Cancel" })).toBeInTheDocument()
})

it("calls restore with correct mode when confirming restore", async () => {
render(<CheckpointMenu {...props} />)

// Open history popover
const historyButton = screen.getByRole("button", { name: "history" })
fireEvent.click(historyButton)

// Wait for the popover to be open
const dialog = await screen.findByRole("dialog")

// Find and click restore files button (exact match)
const restoreButton = await within(dialog).findByRole("button", { name: "Restore Files & Task" })
// Open restore menu
const restoreButton = screen.getByRole("button", { name: "Restore" })
fireEvent.click(restoreButton)

// Find and click confirm button using button role and name
const confirmButton = await within(dialog).findByRole("button", { name: /confirm/i })
// Click Restore Files & Messages
const restoreFilesAndMessagesButton = screen.getByRole("button", { name: "Restore Files & Messages" })
fireEvent.click(restoreFilesAndMessagesButton)

// Click confirm
const confirmButton = screen.getByRole("button", { name: "Confirm" })
fireEvent.click(confirmButton)

expect(vscode.postMessage).toHaveBeenCalledWith({
type: "checkpointRestore",
payload: { ts: props.ts, commitHash: props.commitHash, mode: "files_and_messages" },
payload: {
ts: props.ts,
commitHash: props.commitHash,
mode: "files_and_messages",
},
})
})

Expand All @@ -107,41 +112,49 @@ describe("CheckpointMenu", () => {
jest.clearAllMocks()
})

it("should always show history button", () => {
it("should show bookmark icon", () => {
render(<CheckpointMenu {...defaultProps} />)
expect(screen.getByText("", { selector: "span.codicon-history" })).toBeInTheDocument()
expect(screen.getByText("", { selector: "i.codicon-bookmark" })).toBeInTheDocument()
})

it('should show "Restore Files" option only when not current checkpoint', () => {
const { rerender } = render(<CheckpointMenu {...defaultProps} currentCheckpointHash="xyz789" />)

// Open popover
fireEvent.click(screen.getByText("", { selector: "span.codicon-history" }))
expect(screen.getByText("Restore Files")).toBeInTheDocument()
it("should show restore options for non-current checkpoint", () => {
render(<CheckpointMenu {...defaultProps} currentCheckpointHash="xyz789" />)

// Test with current checkpoint
rerender(<CheckpointMenu {...defaultProps} currentCheckpointHash="abc123" />)
expect(screen.queryByText("Restore Files")).not.toBeInTheDocument()
})
// Open restore menu
const restoreButton = screen.getByRole("button", { name: "Restore" })
fireEvent.click(restoreButton)

it('should always show "Restore Files & Task" option', () => {
render(<CheckpointMenu {...defaultProps} />)
fireEvent.click(screen.getByText("", { selector: "span.codicon-history" }))
expect(screen.getByText("Restore Files & Task")).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Restore Files Only" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Restore Messages Only" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Restore Files & Messages" })).toBeInTheDocument()
})

it("should handle confirmation flow correctly", () => {
render(<CheckpointMenu {...defaultProps} />)
fireEvent.click(screen.getByText("", { selector: "span.codicon-history" }))

// Click Restore Files & Task
fireEvent.click(screen.getByText("Restore Files & Task"))
expect(screen.getByText("Confirm")).toBeInTheDocument()
expect(screen.getByText("Cancel")).toBeInTheDocument()
expect(screen.getByText("This action cannot be undone.")).toBeInTheDocument()
// Open restore menu
const restoreButton = screen.getByRole("button", { name: "Restore" })
fireEvent.click(restoreButton)

// Click Restore Files & Messages
const restoreFilesAndMessagesButton = screen.getByRole("button", { name: "Restore Files & Messages" })
fireEvent.click(restoreFilesAndMessagesButton)

// Check confirmation dialog
expect(screen.getByText("Warning")).toBeInTheDocument()
expect(
screen.getByText(
/This action will restore project files to the state at this checkpoint and delete all messages after this point. This cannot be undone. Are you sure you want to continue?/,
),
).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Confirm" })).toBeInTheDocument()
expect(screen.getByRole("button", { name: "Cancel" })).toBeInTheDocument()

// Click Cancel
fireEvent.click(screen.getByText("Cancel"))
expect(screen.queryByText("Confirm")).not.toBeInTheDocument()
const cancelButton = screen.getByRole("button", { name: "Cancel" })
fireEvent.click(cancelButton)

// Dialog should be closed
expect(screen.queryByText("Warning")).not.toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ describe("CheckpointSaved", () => {

it("should render checkpoint layout correctly", () => {
render(<CheckpointSaved {...defaultProps} />)

expect(screen.getByTestId("git-commit-icon")).toBeInTheDocument()
expect(screen.getByText("Checkpoint")).toBeInTheDocument()
expect(screen.getByTestId("checkpoint-menu")).toBeInTheDocument()
})

it("should show Current label when checkpoint is current", () => {
it("should pass current checkpoint hash correctly", () => {
const { rerender } = render(<CheckpointSaved {...defaultProps} currentCheckpointHash="xyz789" />)
expect(screen.queryByText("Current")).not.toBeInTheDocument()
expect(screen.getByTestId("checkpoint-menu")).toBeInTheDocument()

rerender(<CheckpointSaved {...defaultProps} currentCheckpointHash="abc123" />)
expect(screen.getByText("Current")).toBeInTheDocument()
expect(screen.getByTestId("checkpoint-menu")).toBeInTheDocument()
})
})

0 comments on commit 0c5e515

Please sign in to comment.