Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanksdesign committed Nov 26, 2024
1 parent 8f6e7f1 commit 22e0451
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
convertBufferToBase64,
formatDate,
getImageTypeFromMimeType,
attachmentsValidator,
} from '../utils';

describe('convertBufferToBase64', () => {
Expand Down Expand Up @@ -57,3 +58,91 @@ describe('getImageTypeFromMimeType', () => {
expect(getImageTypeFromMimeType('image/webp')).toBe('webp');
});
});

describe('attachmentsValidator', () => {
// Helper function to create mock files
const createMockFile = (size: number, name = 'test.txt'): File => {
const buffer = new ArrayBuffer(size);
File.prototype.arrayBuffer = jest.fn().mockResolvedValueOnce(buffer);
return new File([buffer], name, { type: 'text/plain' });
};

it('should accept files within size limit', async () => {
const files = [createMockFile(100)];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(1);
expect(result.rejectedFiles).toHaveLength(0);
expect(result.hasMaxSizeError).toBeFalsy();
expect(result.hasMaxError).toBeUndefined();
});

it('should reject files exceeding size limit', async () => {
const files = [createMockFile(2000)];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(0);
expect(result.rejectedFiles).toHaveLength(1);
expect(result.hasMaxSizeError).toBeTruthy();
expect(result.hasMaxError).toBeUndefined();
});

it('should handle mixed valid and invalid file sizes', async () => {
const files = [
createMockFile(500),
createMockFile(2000),
createMockFile(800),
];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(2);
expect(result.rejectedFiles).toHaveLength(1);
expect(result.hasMaxSizeError).toBeTruthy();
expect(result.hasMaxError).toBeUndefined();
});

it('should enforce maximum number of attachments', async () => {
const files = [
createMockFile(100),
createMockFile(200),
createMockFile(300),
createMockFile(400),
];
const result = await attachmentsValidator({
files,
maxAttachments: 2,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(2);
expect(result.rejectedFiles).toHaveLength(2);
expect(result.hasMaxError).toBeTruthy();
expect(result.hasMaxSizeError).toBeFalsy();
});

it('should handle empty file list', async () => {
const files: File[] = [];
const result = await attachmentsValidator({
files,
maxAttachments: 3,
maxAttachmentSize: 1000,
});

expect(result.acceptedFiles).toHaveLength(0);
expect(result.rejectedFiles).toHaveLength(0);
expect(result.hasMaxSizeError).toBeFalsy();
expect(result.hasMaxError).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export const defaultAIConversationDisplayTextEn: Required<AIConversationDisplayT
getMaxAttachmentErrorText(count: number): string {
return `Cannot choose more than ${count} ${
count === 1 ? 'file' : 'files'
}`;
}. `;
},
getAttachmentSizeErrorText(sizeText: string): string {
return `File size must be below ${sizeText}`;
return `File size must be below ${sizeText}.`;
},
};

Expand Down

0 comments on commit 22e0451

Please sign in to comment.