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');
|
});
|
});
|