fix(sidebar): keep destructive table operations cancellable
This commit is contained in:
1 parent
dd77817c7e
commit
89e0f1734b
20 files changed
+627
-25
No files matched your search
@@ -33,6 +33,8 @@ const props = withDefaults(
|
||||
suppressToggleLabel?: string;
|
||||
loading?: boolean;
|
||||
closeOnConfirm?: boolean;
|
||||
cancelable?: boolean;
|
||||
cancelRunningLoading?: boolean;
|
||||
}>(),
|
||||
{
|
||||
sql: "",
|
||||
@@ -45,11 +47,14 @@ const props = withDefaults(
|
||||
suppressToggleLabel: "",
|
||||
loading: false,
|
||||
closeOnConfirm: true,
|
||||
cancelable: false,
|
||||
cancelRunningLoading: false,
|
||||
},
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
confirm: [];
|
||||
"cancel-running": [];
|
||||
}>();
|
||||
|
||||
const code = computed(() => props.details || props.sql);
|
||||
@@ -119,7 +124,11 @@ async function copyFullCode() {
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" :disabled="loading" @click="open = false">{{ t("dangerDialog.cancel") }}</Button>
|
||||
<Button v-if="loading && cancelable" variant="outline" :disabled="cancelRunningLoading" @click="$emit('cancel-running')">
|
||||
<Loader2 v-if="cancelRunningLoading" class="h-3.5 w-3.5 animate-spin" />
|
||||
{{ t("dangerDialog.cancelRunning") }}
|
||||
</Button>
|
||||
<Button v-else variant="outline" :disabled="loading" @click="open = false">{{ t("dangerDialog.cancel") }}</Button>
|
||||
<Button variant="destructive" class="gap-1.5" :disabled="loading" @click="onConfirm">
|
||||
<Loader2 v-if="loading" class="h-3.5 w-3.5 animate-spin" />
|
||||
{{ confirmLabel || t("dangerDialog.confirm") }}
|
||||
|
||||
@@ -20,7 +20,7 @@ vi.mock("@/lib/common/clipboard", () => ({
|
||||
|
||||
const mountedApps: App[] = [];
|
||||
|
||||
async function mountDialog(sql: string) {
|
||||
async function mountDialog(sql: string, extraProps: Record<string, unknown> = {}, listeners: Record<string, (...args: any[]) => void> = {}) {
|
||||
const state = reactive({ open: true });
|
||||
const container = document.createElement("div");
|
||||
document.body.append(container);
|
||||
@@ -30,6 +30,8 @@ async function mountDialog(sql: string) {
|
||||
h(DangerConfirmDialog, {
|
||||
open: state.open,
|
||||
sql,
|
||||
...extraProps,
|
||||
...listeners,
|
||||
"onUpdate:open": (value: boolean) => {
|
||||
state.open = value;
|
||||
},
|
||||
@@ -106,3 +108,51 @@ describe("DangerConfirmDialog SQL preview", () => {
|
||||
expect(copyToClipboard).toHaveBeenCalledWith(sql);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DangerConfirmDialog running/cancel footer state", () => {
|
||||
function footerButtons() {
|
||||
const footer = document.body.querySelectorAll("button");
|
||||
return Array.from(footer);
|
||||
}
|
||||
|
||||
it("keeps the default Cancel button unchanged when cancelable is not set (existing callers)", async () => {
|
||||
await mountDialog("DROP TABLE users;", { loading: true });
|
||||
|
||||
const buttons = footerButtons();
|
||||
const cancelButton = buttons.find((button) => button.textContent?.trim() === "Cancel");
|
||||
expect(cancelButton).toBeDefined();
|
||||
expect(cancelButton?.disabled).toBe(true);
|
||||
expect(buttons.some((button) => button.textContent?.trim() === "Cancel Query")).toBe(false);
|
||||
});
|
||||
|
||||
it("shows an active Cancel Query button while loading when cancelable is true", async () => {
|
||||
const onCancelRunning = vi.fn();
|
||||
await mountDialog("DELETE FROM big_orders;", { loading: true, cancelable: true }, { onCancelRunning });
|
||||
|
||||
const buttons = footerButtons();
|
||||
const cancelRunningButton = buttons.find((button) => button.textContent?.trim() === "Cancel Query");
|
||||
expect(cancelRunningButton).toBeDefined();
|
||||
expect(cancelRunningButton?.disabled).toBe(false);
|
||||
|
||||
cancelRunningButton?.click();
|
||||
await nextTick();
|
||||
|
||||
expect(onCancelRunning).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("disables the Cancel Query button while the cancel itself is in flight", async () => {
|
||||
await mountDialog("DELETE FROM big_orders;", { loading: true, cancelable: true, cancelRunningLoading: true });
|
||||
|
||||
const buttons = footerButtons();
|
||||
const cancelRunningButton = buttons.find((button) => button.textContent?.trim() === "Cancel Query");
|
||||
expect(cancelRunningButton?.disabled).toBe(true);
|
||||
});
|
||||
|
||||
it("does not show the Cancel Query button when cancelable is true but not loading", async () => {
|
||||
await mountDialog("DELETE FROM big_orders;", { loading: false, cancelable: true });
|
||||
|
||||
const buttons = footerButtons();
|
||||
expect(buttons.some((button) => button.textContent?.trim() === "Cancel Query")).toBe(false);
|
||||
expect(buttons.some((button) => button.textContent?.trim() === "Cancel")).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user