刘光辉
3 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
import { describe, expect, it } from 'vitest';
import { canReplaceWithTemplate, templateFileError } from './templateSelection';
const file = { fileId: 'source', name: '模板.docx', fileSize: 1024, url: '/source', thumbUrl: '' };
const props = { accept: '*', fileSize: 10, sizeUnit: 'MB' };
describe('template selection constraints', () => {
  it('requires add for an empty field, replace for an existing file, and remove when shrinking multiple files', () => {
    const access = { status: 'ready' as const, actions: { ADD: true, REPLACE: false, REMOVE: false } };
    expect(canReplaceWithTemplate(access, 0)).toBe(true);
    expect(canReplaceWithTemplate(access, 1)).toBe(false);
    access.actions.REPLACE = true;
    expect(canReplaceWithTemplate(access, 1)).toBe(true);
    expect(canReplaceWithTemplate(access, 2)).toBe(false);
    access.actions.REMOVE = true;
    expect(canReplaceWithTemplate(access, 2)).toBe(true);
    expect(canReplaceWithTemplate({ status: 'loading' }, 0)).toBe(false);
  });
  it('applies the upload type and strict size limits', () => {
    expect(templateFileError(file, props)).toBe('');
    expect(templateFileError(file, { ...props, accept: '.pdf' })).toContain('类型');
    expect(templateFileError(file, { ...props, accept: '.DOCX' })).toBe('');
    expect(templateFileError(file, { ...props, sizeUnit: 'KB', fileSize: 1 })).toContain('小于');
    expect(templateFileError({ ...file, fileSize: 'broken' }, props)).toContain('无效');
    expect(templateFileError({ ...file, name: 'a.pdf' }, props)).toContain('DOCX');
  });
});