fix(demos): snapshot validated values before asynchronous submissions

This commit is contained in:
maoyingjie
2026-10-28 15:05:09 +08:00
parent 49dd67afac
commit b0f32796cb
6 changed files with 102 additions and 3 deletions
@@ -28,10 +28,11 @@ function submit() {
value.value > STOCK_AVAILABLE
)
return;
const submittedValue = value.value;
isSubmitting.value = true;
// Simulate API call
timer = setTimeout(() => {
console.log("Order submitted:", { quantity: value.value });
console.log("Order submitted:", { quantity: submittedValue });
value.value = null;
isSubmitting.value = false;
}, 1500);
@@ -22,10 +22,11 @@ let timer: ReturnType<typeof setTimeout> | undefined;
onBeforeUnmount(() => clearTimeout(timer));
function submit() {
if (value.value.length < 3 || isSubmitting.value) return;
const submittedValue = value.value;
isSubmitting.value = true;
// Simulate API call
timer = setTimeout(() => {
console.log("Search submitted:", { query: value.value });
console.log("Search submitted:", { query: submittedValue });
value.value = "";
isSubmitting.value = false;
}, 1500);
+2 -1
View File
@@ -33,10 +33,11 @@ const isSubmitting = shallowRef(false);
let timer: ReturnType<typeof setTimeout> | undefined;
function submit() {
if (!value.value || isInvalid.value || isSubmitting.value) return;
const submittedValue = value.value;
isSubmitting.value = true;
// Simulate API call
timer = setTimeout(() => {
console.log("Time submitted:", { time: value.value });
console.log("Time submitted:", { time: submittedValue });
value.value = null;
isSubmitting.value = false;
}, 1500);
+32
View File
@@ -477,3 +477,35 @@ it("keeps the baseline preview inventories and exact fieldset submission feedbac
alert.mockRestore();
w.unmount();
});
it.each(["changed-query", "x"])(
"keeps the submitted search-field snapshot when pending input changes to %s",
async (changed) => {
const { default: Demo } =
await import("../apps/docs/demos/search-field-form-example.vue");
const w = mount(Demo, { attachTo: document.body });
const log = vi.spyOn(console, "log").mockImplementation(() => {});
vi.useFakeTimers();
try {
const input = w.get("input[type=search]");
await input.setValue("original-query");
await input.trigger("blur");
await w.get("form").trigger("submit");
expect(w.get('button[type="submit"]').text()).toContain("…");
await input.setValue(changed);
await input.trigger("blur");
await w.get("form").trigger("submit");
await vi.advanceTimersByTimeAsync(1500);
expect(log).toHaveBeenCalledTimes(1);
expect(log.mock.calls[0][0]).toBe("Search submitted:");
expect(String((log.mock.calls[0][1] as { query: unknown }).query)).toBe(
"original-query",
);
expect((input.element as HTMLInputElement).value).toBe("");
} finally {
w.unmount();
log.mockRestore();
vi.useRealTimers();
}
},
);
+32
View File
@@ -332,3 +332,35 @@ describe("NumberField public contracts", () => {
);
});
});
it.each(["3", "5"])(
"keeps the submitted number-field snapshot when pending input changes to %s",
async (changed) => {
const { default: Demo } =
await import("../apps/docs/demos/number-field-form-example.vue");
const w = mount(Demo, { attachTo: document.body });
const log = vi.spyOn(console, "log").mockImplementation(() => {});
vi.useFakeTimers();
try {
const input = w.get("input[role=spinbutton]");
await input.setValue("2");
await input.trigger("blur");
await w.get("form").trigger("submit");
expect(w.get('button[type="submit"]').text()).toContain("…");
await input.setValue(changed);
await input.trigger("blur");
await w.get("form").trigger("submit");
await vi.advanceTimersByTimeAsync(1500);
expect(log).toHaveBeenCalledTimes(1);
expect(log.mock.calls[0][0]).toBe("Order submitted:");
expect(
String((log.mock.calls[0][1] as { quantity: unknown }).quantity),
).toBe("2");
expect((input.element as HTMLInputElement).value).toBe("");
} finally {
w.unmount();
log.mockRestore();
vi.useRealTimers();
}
},
);
+32
View File
@@ -113,3 +113,35 @@ describe("TimeField documentation", () => {
}
});
});
it.each(["15:30:00", "08:00:00"])(
"keeps the submitted time-field snapshot when pending input changes to %s",
async (changed) => {
const { default: Demo } =
await import("../apps/docs/demos/time-field-form-example.vue");
const w = mount(Demo, { attachTo: document.body });
const log = vi.spyOn(console, "log").mockImplementation(() => {});
vi.useFakeTimers();
try {
const input = w.get("[data-slot=time-field-value]");
await input.setValue("14:30:00");
await input.trigger("blur");
await w.get("form").trigger("submit");
expect(w.get('button[type="submit"]').text()).toContain("…");
await input.setValue(changed);
await input.trigger("blur");
await w.get("form").trigger("submit");
await vi.advanceTimersByTimeAsync(1500);
expect(log).toHaveBeenCalledTimes(1);
expect(log.mock.calls[0][0]).toBe("Time submitted:");
expect(String((log.mock.calls[0][1] as { time: unknown }).time)).toBe(
"14:30:00",
);
expect((input.element as HTMLInputElement).value).toBe("");
} finally {
w.unmount();
log.mockRestore();
vi.useRealTimers();
}
},
);