刘光辉
12 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { afterEach, describe, expect, it, vi } from 'vitest';
 
import { fetchFillData, FillDataError, parseFillData } from '../../src/api/template-fill';
 
describe('template fill api', () => {
  afterEach(() => vi.unstubAllGlobals());
 
  it('sends the opaque ticket in the Authorization header', async () => {
    const fetchMock = vi.fn().mockResolvedValue(new Response('{"fields":{}}', { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);
    await fetchFillData({ apiBaseUrl: 'https://api.example.com', ticket: 'secret' });
    expect(fetchMock).toHaveBeenCalledWith('https://api.example.com/api/eln/onlyoffice/template-fill/data', {
      headers: { Authorization: 'Ticket secret' },
    });
  });
 
  it.each([
    [401, '填写凭证已过期,请重新打开文档'],
    [403, '无权读取填写数据'],
    [404, '填写数据不存在'],
    [500, '填写数据加载失败'],
  ])('maps HTTP %s', async (status, message) => {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response('', { status })));
    await expect(fetchFillData({ apiBaseUrl: 'https://api.example.com', ticket: 'secret' })).rejects.toEqual(new FillDataError(message, status));
  });
 
  it('normalizes null checkbox to false and validates field values', () => {
    expect(parseFillData({ fields: { 'eln.field.ok': { type: 'checkbox', value: null } } }).fields['eln.field.ok']?.value).toBe(false);
    expect(() => parseFillData({ fields: { bad: { type: 'unknown', value: '' } } })).toThrow('填写数据格式无效');
  });
});