第5项:按页面管理导航、历史草稿与操作回执
This commit is contained in:
1 parent
f570929cfe
commit
ff8b05973a
48 files changed
+1577
-574
No files matched your search
@@ -0,0 +1,187 @@
|
||||
import { afterEach, expect, it } from 'vitest';
|
||||
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
|
||||
import { LedgerClient } from '../src/lib/api';
|
||||
import { loadDraft } from '../src/pages/CreateOrder/drafts';
|
||||
import { MaterialPoolsPage } from '../src/pages/MaterialPools/MaterialPoolsPage';
|
||||
import type { LedgerState } from '../src/shared';
|
||||
const today = '2026-11-20';
|
||||
const state: LedgerState = { today, mode: 'demo', user: { id: 'owner', role: 'owner', member: 'owner', name: '测试' } };
|
||||
const ok = (body: unknown) => new Response(JSON.stringify(body), { status: 200 });
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
localStorage.clear();
|
||||
});
|
||||
it('在本账号尚无新版草稿时继续旧开单内容,已有新版或另一账号内容分别保留', () => {
|
||||
const old = { customer: '旧草稿', lines: [{ quantity: '1.250' }] };
|
||||
localStorage.setItem('mm-draft-owner', JSON.stringify(old));
|
||||
expect(loadDraft(localStorage, 'owner')).toEqual(old);
|
||||
expect(loadDraft(localStorage, 'staff')).toBeNull();
|
||||
const next = { ...old, customer: '新版草稿' };
|
||||
localStorage.setItem('mm-next-draft-owner', JSON.stringify(next));
|
||||
expect(loadDraft(localStorage, 'owner')).toEqual(next);
|
||||
});
|
||||
it('购料发出后另一标签已写下一笔,迟到的成功回执保留新草稿', async () => {
|
||||
let reply!: (value: Response) => void;
|
||||
const client = new LedgerClient({
|
||||
storage: localStorage,
|
||||
randomUUID: () => 'test-operation-key-0001',
|
||||
setTimeout: () => 0,
|
||||
clearTimeout: () => {},
|
||||
fetch: async (url, init) => {
|
||||
if (init.method === 'POST')
|
||||
return new Promise(resolve => {
|
||||
reply = resolve;
|
||||
});
|
||||
return ok(url.startsWith('/api/material-pools') ? { rows: [] } : state);
|
||||
},
|
||||
});
|
||||
client.setUser('owner');
|
||||
history.replaceState({}, '', '/next/material-pools');
|
||||
render(<MaterialPoolsPage state={state} client={client} />);
|
||||
fireEvent.change(await screen.findByLabelText('购买克重(g)'), { target: { value: '1.250' } });
|
||||
fireEvent.change(screen.getByLabelText('购买总价(元)'), { target: { value: '60.50' } });
|
||||
fireEvent.click(screen.getByRole('button', { name: '保存购料' }));
|
||||
expect(client.pending()?.url).toBe('/api/material-purchases');
|
||||
const key = 'mm-material-purchase-owner';
|
||||
const newer = JSON.stringify({ date: today, grams: '9.250', total: '90.50', payer: 'owner', note: '下一笔' });
|
||||
localStorage.setItem(key, newer);
|
||||
await act(async () => reply(ok({ poolId: today })));
|
||||
expect(localStorage.getItem(key)).toBe(newer);
|
||||
});
|
||||
|
||||
import { recoverPending } from '../src/pages/recoverPending';
|
||||
import { saveWithDraft } from '../src/lib/receipt';
|
||||
const storageKey = 'mm-pending-owner';
|
||||
function clientFor(fetcher: (url: string, init: RequestInit) => Promise<Response>) {
|
||||
const client = new LedgerClient({
|
||||
storage: localStorage,
|
||||
randomUUID: () => 'test-operation-key-0001',
|
||||
fetch: fetcher,
|
||||
setTimeout: (fn, delay) => (delay === 25000 ? 0 : setTimeout(fn, 0)),
|
||||
clearTimeout: () => {},
|
||||
});
|
||||
client.setUser('owner');
|
||||
return client;
|
||||
}
|
||||
it('发出后换账号再切回,已确认的旧回执不能进入当前会话', async () => {
|
||||
let deliver!: (r: Response) => void;
|
||||
const client = clientFor(
|
||||
() =>
|
||||
new Promise(resolve => {
|
||||
deliver = resolve;
|
||||
}),
|
||||
);
|
||||
const key = 'mm-price-lock-draft-owner',
|
||||
raw = JSON.stringify({ customer: '甲' });
|
||||
localStorage.setItem(key, raw);
|
||||
const sent = saveWithDraft(client, '/api/price-locks', { customer: '甲' }, { key, value: raw });
|
||||
client.setUser('staff');
|
||||
client.setUser('owner');
|
||||
deliver(ok({ id: 'old-lock' }));
|
||||
await expect(sent).rejects.toMatchObject({ code: 'ACCOUNT_CHANGED' });
|
||||
expect(localStorage.getItem(key)).toBe(raw);
|
||||
});
|
||||
it.each([false, true])('丢回执后在其他页恢复原锁价,完全相同的请求体和操作号,更改草稿=%s', async changed => {
|
||||
const data = { customer: '甲', date: today, lines: [{ quantity: '1.250' }] },
|
||||
raw = JSON.stringify({ ...data, manual: { rate: '6.00' } }),
|
||||
key = 'mm-price-lock-draft-owner';
|
||||
const calls: { body: string; operation: string }[] = [];
|
||||
let online = false;
|
||||
const fetcher = async (_url: string, init: RequestInit) => {
|
||||
calls.push({ body: String(init.body), operation: new Headers(init.headers).get('Idempotency-Key')! });
|
||||
if (!online) throw new TypeError('network');
|
||||
return ok({ id: 'lock-1' });
|
||||
};
|
||||
const first = clientFor(fetcher);
|
||||
localStorage.setItem(key, raw);
|
||||
await expect(saveWithDraft(first, '/api/price-locks', data, { key, value: raw })).rejects.toThrow(/待确认/);
|
||||
expect(first.pending()?.draft).toBe(raw);
|
||||
online = true;
|
||||
const next = clientFor(fetcher);
|
||||
if (changed) localStorage.setItem(key, '{"customer":"下一笔"}');
|
||||
localStorage.setItem('mm-price-lock-draft-staff', 'staff');
|
||||
expect(await recoverPending(next, state, localStorage)).toMatchObject({
|
||||
route: 'price-locks',
|
||||
search: '?lock=lock-1',
|
||||
});
|
||||
expect(calls).toHaveLength(4);
|
||||
expect(new Set(calls.map(x => x.body))).toEqual(new Set([JSON.stringify(data)]));
|
||||
expect(new Set(calls.map(x => x.operation)).size).toBe(1);
|
||||
expect(localStorage.getItem(storageKey)).toBeNull();
|
||||
expect(localStorage.getItem(key)).toBe(changed ? '{"customer":"下一笔"}' : null);
|
||||
expect(localStorage.getItem('mm-price-lock-draft-staff')).toBe('staff');
|
||||
});
|
||||
it('升级前没有 userId 或快照的待确认按本账号恢复,清除对应的订单更正草稿并保留后来打开的另一单', async () => {
|
||||
const data = { date: today, customer: '甲', revision: 7, reason: '更正', verified: true };
|
||||
const client = clientFor(async () => ok({ id: 'original' }));
|
||||
localStorage.setItem(
|
||||
storageKey,
|
||||
JSON.stringify({ url: '/api/orders/original/change', key: 'legacy-operation-key', data }),
|
||||
);
|
||||
localStorage.setItem(
|
||||
'mm-order-edit-owner-original',
|
||||
JSON.stringify({
|
||||
id: 'original',
|
||||
revision: 7,
|
||||
reason: ' 更正 ',
|
||||
verified: true,
|
||||
inputs: { date: today, customer: '甲' },
|
||||
}),
|
||||
);
|
||||
localStorage.setItem('mm-order-edit-active-owner', 'other');
|
||||
const target = await recoverPending(client, state, localStorage);
|
||||
expect(target).toEqual({ route: 'orders', search: '?order=original' });
|
||||
expect(localStorage.getItem('mm-order-edit-owner-original')).toBeNull();
|
||||
expect(localStorage.getItem('mm-order-edit-active-owner')).toBe('other');
|
||||
});
|
||||
it('批准一份历史转账与确认其单数旧地址保持不同的结果', async () => {
|
||||
for (const suffix of ['transfer', 'transfers']) {
|
||||
const client = clientFor(async () => ok({ id: 'transfer-identifier' }));
|
||||
localStorage.setItem(
|
||||
storageKey,
|
||||
JSON.stringify({ url: '/api/team-funds/' + suffix, key: 'legacy-operation-key', data: {} }),
|
||||
);
|
||||
expect(await recoverPending(client, state, localStorage)).toEqual(
|
||||
suffix === 'transfer' ? {} : { route: 'team-funds', message: '实际转账已保存,待转金额已重新核对' },
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
import { draftsFor, receiptFor } from '../src/pages/Report/drafts';
|
||||
import { clearIssuedDraft } from '../src/pages/CreateOrder/drafts';
|
||||
it('旧开单导入一次,同一回执清理已采用的草稿后不会重新导入或改写旧版', () => {
|
||||
const raw = JSON.stringify({ customer: ' 甲 ', lines: [{ quantity: '1.250' }] });
|
||||
localStorage.setItem('mm-draft-owner', raw);
|
||||
expect(loadDraft(localStorage, 'owner')?.customer).toBe(' 甲 ');
|
||||
clearIssuedDraft(localStorage, 'owner', { data: { customer: '甲', lines: [{ quantity: '1.250' }] } });
|
||||
expect(loadDraft(localStorage, 'owner')).toBeNull();
|
||||
expect(localStorage.getItem('mm-draft-owner')).toBe(raw);
|
||||
localStorage.setItem('mm-draft-owner', JSON.stringify({ customer: '乙', lines: [] }));
|
||||
expect(loadDraft(localStorage, 'owner')?.customer).toBe('乙');
|
||||
});
|
||||
it.each(['更新原草稿', '另开一张'])('跨页恢复报销保留后来填写的内容:%s', async action => {
|
||||
const client = clientFor(async () => ok({ date: today }));
|
||||
const cache = draftsFor(client, 'owner'),
|
||||
key = 'owner|' + today + '|' + today + '|false';
|
||||
cache.forms.set('first', { id: '', date: today, items: [], note: '原备注' });
|
||||
cache.editors.set(key, 'first');
|
||||
const snapshot = receiptFor(cache, key, 'draft');
|
||||
localStorage.setItem(
|
||||
storageKey,
|
||||
JSON.stringify({
|
||||
url: '/api/reimbursements',
|
||||
key: 'legacy-operation-key',
|
||||
data: { date: today, note: '原备注' },
|
||||
draft: snapshot,
|
||||
userId: 'owner',
|
||||
}),
|
||||
);
|
||||
if (action === '更新原草稿') cache.forms.get('first')!.note = '新备注';
|
||||
else {
|
||||
cache.editors.set(key, 'second');
|
||||
cache.forms.set('second', { id: '', date: today, items: [], note: '新备注' });
|
||||
}
|
||||
expect(await recoverPending(client, state, localStorage)).toMatchObject({ message: '报销单已提交,等待店主复核' });
|
||||
expect(cache.forms.get(cache.editors.get(key)!)?.note).toBe('新备注');
|
||||
expect(cache.forms.has('first')).toBe(action === '更新原草稿');
|
||||
});
|
||||
Reference in new issue
Block a user