mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-09-30 09:13:11 +00:00
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
/**
|
|
* Mirrors WorkOrderSlideOver commit: exit edit mode only after a successful save
|
|
* (or when there is nothing to persist).
|
|
*/
|
|
function commitSlideOverEdit(input: {
|
|
patchKeys: number;
|
|
onSave: (options: { onSuccess?: () => void }) => void;
|
|
exitEdit: () => void;
|
|
clearUnsaved: () => void;
|
|
}) {
|
|
if (input.patchKeys === 0) {
|
|
input.exitEdit();
|
|
input.clearUnsaved();
|
|
return;
|
|
}
|
|
|
|
input.onSave({
|
|
onSuccess: () => {
|
|
input.exitEdit();
|
|
input.clearUnsaved();
|
|
},
|
|
});
|
|
}
|
|
|
|
describe("slide-over commit save contract", () => {
|
|
it("exits edit mode immediately when there is no patch", () => {
|
|
const exitEdit = vi.fn();
|
|
const clearUnsaved = vi.fn();
|
|
const onSave = vi.fn();
|
|
|
|
commitSlideOverEdit({ patchKeys: 0, onSave, exitEdit, clearUnsaved });
|
|
|
|
expect(onSave).not.toHaveBeenCalled();
|
|
expect(exitEdit).toHaveBeenCalledOnce();
|
|
expect(clearUnsaved).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("keeps edit mode when save fails (onSuccess never runs)", () => {
|
|
const exitEdit = vi.fn();
|
|
const clearUnsaved = vi.fn();
|
|
|
|
commitSlideOverEdit({
|
|
patchKeys: 1,
|
|
onSave: () => {
|
|
/* rejected — do not call onSuccess */
|
|
},
|
|
exitEdit,
|
|
clearUnsaved,
|
|
});
|
|
|
|
expect(exitEdit).not.toHaveBeenCalled();
|
|
expect(clearUnsaved).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("exits edit mode only from onSuccess after a rejected-then-retry path would not", () => {
|
|
const exitEdit = vi.fn();
|
|
const clearUnsaved = vi.fn();
|
|
let success: (() => void) | undefined;
|
|
|
|
commitSlideOverEdit({
|
|
patchKeys: 2,
|
|
onSave: (options) => {
|
|
success = options.onSuccess;
|
|
},
|
|
exitEdit,
|
|
clearUnsaved,
|
|
});
|
|
|
|
expect(exitEdit).not.toHaveBeenCalled();
|
|
success?.();
|
|
expect(exitEdit).toHaveBeenCalledOnce();
|
|
expect(clearUnsaved).toHaveBeenCalledOnce();
|
|
});
|
|
});
|