@isi-ui7/bos7-shared 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/form-types.d.ts +23 -0
- package/dist/{index-1k-PSj-b.js → index-DV8lJjii.js} +71 -71
- package/dist/{index.es-zuTIhIvp.js → index.es-DqL2wDqB.js} +2 -2
- package/dist/index.js +1 -1
- package/dist/{jspdf.es.min-CnDMxfNp.js → jspdf.es.min-iZWDIC-k.js} +2 -2
- package/dist/{jspdf.plugin.autotable-DcntYaes.js → jspdf.plugin.autotable-DFaknRns.js} +19 -22
- package/dist/purify.es-Cm3utOpm.js +560 -0
- package/package.json +2 -1
- package/src/form-types.ts +25 -1
- package/src/wizard-step-gate.test.ts +65 -0
- package/src/workflow/use-crud-form-gate.test.tsx +138 -0
- package/src/workflow/use-crud-form.ts +13 -0
- package/dist/purify.es-CiEWEeUM.js +0 -605
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import type { CrudForm } from './form-types';
|
|
3
|
+
|
|
4
|
+
type D = Record<string, unknown>;
|
|
5
|
+
|
|
6
|
+
// Gerbang antar-langkah wizard. Diuji sebagai KONTRAK, bukan lewat render:
|
|
7
|
+
// yang penting bentuk & urutan pemanggilannya, dan itu bisa dipastikan tanpa
|
|
8
|
+
// memasang seluruh form.
|
|
9
|
+
//
|
|
10
|
+
// Kenapa diuji sama sekali: gerbang yang gagal-terbuka tidak terlihat sebagai
|
|
11
|
+
// galat — operator sekadar melewati langkah yang seharusnya mengunci, dan
|
|
12
|
+
// akibatnya (dua CIF untuk satu orang) baru muncul jauh kemudian.
|
|
13
|
+
|
|
14
|
+
function makeSchema(gate: NonNullable<Extract<CrudForm<D>['layout'], { type: 'wizard' }>['canLeavePage']>): CrudForm<D> {
|
|
15
|
+
return {
|
|
16
|
+
title: { create: 'c', edit: 'e', view: 'v' },
|
|
17
|
+
emptyData: {},
|
|
18
|
+
layout: {
|
|
19
|
+
type: 'wizard',
|
|
20
|
+
validateOnNext: true,
|
|
21
|
+
canLeavePage: gate,
|
|
22
|
+
pages: [
|
|
23
|
+
{ label: 'Verifikasi', sections: [] },
|
|
24
|
+
{ label: 'Data', sections: [] },
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
} as CrudForm<D>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('wizard canLeavePage', () => {
|
|
31
|
+
it('menolak pindah ketika gerbang mengembalikan ok:false, dan membawa pesannya', () => {
|
|
32
|
+
const gate = vi.fn().mockReturnValue({ ok: false, message: 'Verifikasi identitas dulu.' });
|
|
33
|
+
const schema = makeSchema(gate);
|
|
34
|
+
const layout = schema.layout as Extract<typeof schema.layout, { type: 'wizard' }>;
|
|
35
|
+
|
|
36
|
+
const res = layout.canLeavePage!(0, { verified: false });
|
|
37
|
+
expect(res.ok).toBe(false);
|
|
38
|
+
expect(res.message).toBe('Verifikasi identitas dulu.');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('mengizinkan pindah ketika gerbang ok', () => {
|
|
42
|
+
const layout = makeSchema(() => ({ ok: true })).layout as Extract<
|
|
43
|
+
CrudForm<D>['layout'],
|
|
44
|
+
{ type: 'wizard' }
|
|
45
|
+
>;
|
|
46
|
+
expect(layout.canLeavePage!(0, { verified: true }).ok).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('menerima nomor halaman ASAL, bukan tujuan — gerbang milik langkah yang ditinggalkan', () => {
|
|
50
|
+
const gate = vi.fn().mockReturnValue({ ok: true });
|
|
51
|
+
const layout = makeSchema(gate).layout as Extract<CrudForm<D>['layout'], { type: 'wizard' }>;
|
|
52
|
+
layout.canLeavePage!(0, {});
|
|
53
|
+
expect(gate).toHaveBeenCalledWith(0, {});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('opsional — wizard tanpa gerbang tetap sah secara tipe', () => {
|
|
57
|
+
const schema: CrudForm<D> = {
|
|
58
|
+
title: { create: 'c', edit: 'e', view: 'v' },
|
|
59
|
+
emptyData: {},
|
|
60
|
+
layout: { type: 'wizard', pages: [{ label: 'A', sections: [] }] },
|
|
61
|
+
} as CrudForm<D>;
|
|
62
|
+
const layout = schema.layout as Extract<typeof schema.layout, { type: 'wizard' }>;
|
|
63
|
+
expect(layout.canLeavePage).toBeUndefined();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { act, renderHook } from "@testing-library/react";
|
|
3
|
+
import { useCrudForm } from "./use-crud-form";
|
|
4
|
+
import type { CrudForm } from "../form-types";
|
|
5
|
+
|
|
6
|
+
type D = Record<string, unknown>;
|
|
7
|
+
|
|
8
|
+
// Gerbang antar-langkah wizard, diuji lewat PERILAKU goNext.
|
|
9
|
+
//
|
|
10
|
+
// Kenapa ada tes kedua padahal sudah ada wizard-step-gate.test.ts: tes itu
|
|
11
|
+
// hanya memastikan BENTUK tipenya — ia akan tetap hijau meski goNext tidak
|
|
12
|
+
// pernah memanggil canLeavePage sama sekali. Persis kegagalan yang gerbang ini
|
|
13
|
+
// ada untuk mencegahnya: gagal-terbuka tidak berbunyi sebagai galat, operator
|
|
14
|
+
// sekadar melewati langkah yang seharusnya mengunci, dan akibatnya (dua CIF
|
|
15
|
+
// untuk satu orang) baru muncul jauh kemudian.
|
|
16
|
+
//
|
|
17
|
+
// Tes di bawah memeriksa `page` — halaman yang BENAR-BENAR ditempati — bukan
|
|
18
|
+
// nilai balik gerbangnya. Itu satu-satunya yang membedakan "terpasang" dari
|
|
19
|
+
// "sekadar ada".
|
|
20
|
+
|
|
21
|
+
function schemaWith(
|
|
22
|
+
gate?: (from: number, d: D) => { ok: boolean; message?: string },
|
|
23
|
+
opts: { validateOnNext?: boolean } = {},
|
|
24
|
+
): CrudForm<D> {
|
|
25
|
+
return {
|
|
26
|
+
title: { create: "c", edit: "e", view: "v" },
|
|
27
|
+
emptyData: { verified: false } as D,
|
|
28
|
+
layout: {
|
|
29
|
+
type: "wizard",
|
|
30
|
+
validateOnNext: opts.validateOnNext ?? false,
|
|
31
|
+
canLeavePage: gate,
|
|
32
|
+
pages: [
|
|
33
|
+
{
|
|
34
|
+
label: "Verifikasi",
|
|
35
|
+
sections: [
|
|
36
|
+
{
|
|
37
|
+
title: "s",
|
|
38
|
+
fields: [
|
|
39
|
+
{
|
|
40
|
+
key: "nama",
|
|
41
|
+
label: "Nama",
|
|
42
|
+
type: "text",
|
|
43
|
+
span: 4,
|
|
44
|
+
validation: { required: true },
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
{ label: "Data", sections: [] },
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
} as unknown as CrudForm<D>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe("useCrudForm — gerbang canLeavePage", () => {
|
|
57
|
+
it("gerbang ok:false MENAHAN halaman di 0 dan menampilkan pesannya", async () => {
|
|
58
|
+
const { result } = renderHook(() =>
|
|
59
|
+
useCrudForm<D>({
|
|
60
|
+
schema: schemaWith(() => ({ ok: false, message: "Cek identitas dulu." })),
|
|
61
|
+
mode: "create",
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(result.current.page).toBe(0);
|
|
66
|
+
await act(async () => {
|
|
67
|
+
await result.current.goNext();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(result.current.page).toBe(0);
|
|
71
|
+
expect(result.current.error).toBe("Cek identitas dulu.");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("gerbang ok:true MEMBIARKAN pindah ke halaman 1", async () => {
|
|
75
|
+
const { result } = renderHook(() =>
|
|
76
|
+
useCrudForm<D>({ schema: schemaWith(() => ({ ok: true })), mode: "create" }),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
await act(async () => {
|
|
80
|
+
await result.current.goNext();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(result.current.page).toBe(1);
|
|
84
|
+
expect(result.current.error).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("gerbang menerima nomor halaman ASAL beserta isi form saat itu", async () => {
|
|
88
|
+
const gate = vi.fn().mockReturnValue({ ok: true });
|
|
89
|
+
const { result } = renderHook(() =>
|
|
90
|
+
useCrudForm<D>({ schema: schemaWith(gate), mode: "create" }),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
await act(async () => {
|
|
94
|
+
result.current.setForm({ verified: true });
|
|
95
|
+
});
|
|
96
|
+
await act(async () => {
|
|
97
|
+
await result.current.goNext();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(gate).toHaveBeenCalledWith(0, { verified: true });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("gerbang berjalan SEBELUM validasi field — pesan yang muncul alasan sebenarnya, bukan galat field", async () => {
|
|
104
|
+
// `nama` wajib dan kosong, jadi validasi PASTI gagal juga. Kalau urutannya
|
|
105
|
+
// terbalik, yang tampil adalah galat validasi generik dan operator tidak
|
|
106
|
+
// pernah tahu bahwa yang sesungguhnya menahan adalah verifikasi identitas.
|
|
107
|
+
const { result } = renderHook(() =>
|
|
108
|
+
useCrudForm<D>({
|
|
109
|
+
schema: schemaWith(() => ({ ok: false, message: "Cek identitas dulu." }), {
|
|
110
|
+
validateOnNext: true,
|
|
111
|
+
}),
|
|
112
|
+
mode: "create",
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
await act(async () => {
|
|
117
|
+
await result.current.goNext();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
expect(result.current.page).toBe(0);
|
|
121
|
+
expect(result.current.error).toBe("Cek identitas dulu.");
|
|
122
|
+
expect(result.current.fieldErrors).toEqual({});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("KONTROL negatif: wizard tanpa gerbang tetap bisa pindah halaman", async () => {
|
|
126
|
+
// Kalau tes ini ikut gagal ketika gerbang dicabut, berarti yang diukur
|
|
127
|
+
// bukan gerbangnya melainkan navigasinya secara umum.
|
|
128
|
+
const { result } = renderHook(() =>
|
|
129
|
+
useCrudForm<D>({ schema: schemaWith(undefined), mode: "create" }),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
await act(async () => {
|
|
133
|
+
await result.current.goNext();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
expect(result.current.page).toBe(1);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -100,6 +100,19 @@ export function useCrudForm<TData extends Record<string, unknown>>({
|
|
|
100
100
|
setServerError(null);
|
|
101
101
|
setHasValidationError(false);
|
|
102
102
|
const layout = schema.layout;
|
|
103
|
+
|
|
104
|
+
// Gerbang antar-langkah — diperiksa SEBELUM validasi field, karena gerbang
|
|
105
|
+
// yang gagal berarti langkah ini belum boleh ditinggalkan sama sekali;
|
|
106
|
+
// menjalankan validasi lebih dulu akan menampilkan galat field yang tidak
|
|
107
|
+
// relevan dan menutupi alasan sebenarnya.
|
|
108
|
+
if (layout.type === "wizard" && layout.canLeavePage) {
|
|
109
|
+
const gate = layout.canLeavePage(page, form);
|
|
110
|
+
if (!gate.ok) {
|
|
111
|
+
setServerError(gate.message ?? "Langkah ini belum bisa dilanjutkan.");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
103
116
|
if (layout.type === "wizard" && layout.validateOnNext) {
|
|
104
117
|
const errs = validatePage(page);
|
|
105
118
|
setFieldErrors(errs);
|