Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - handleImageUpload #883

Open
tomsmith8 opened this issue Jan 7, 2025 · 0 comments
Open

[Unit Tests] - handleImageUpload #883

tomsmith8 opened this issue Jan 7, 2025 · 0 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for "handleImageUpload"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes-frontend/src/components/common/TicketEditor/TicketTextArea.tsx


// handleImageUpload.test.js

import { handleImageUpload } from './path/to/your/module';
import { v4 as uuidv4 } from 'uuid';
import { uploadImage } from './path/to/uploadImage';
import { textareaValue, onChange } from './path/to/textareaHelpers';

jest.mock('uuid', () => ({
v4: jest.fn(),
}));

jest.mock('./path/to/uploadImage', () => ({
uploadImage: jest.fn(),
}));

jest.mock('./path/to/textareaHelpers', () => ({
textareaValue: jest.fn(),
onChange: jest.fn(),
}));

describe('handleImageUpload', () => {
let file;
let textArea;

beforeEach(() => {
  file = new File(['dummy content'], 'example.png', { type: 'image/png' });
  textArea = document.createElement('textarea');
  document.body.appendChild(textArea);
  jest.clearAllMocks();
});

afterEach(() => {
  document.body.removeChild(textArea);
});

test('Standard Image Upload', async () => {
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('existing text');
  uploadImage.mockResolvedValueOnce();

  await handleImageUpload(file);

  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id...]()'));
  expect(uploadImage).toHaveBeenCalledWith(file, expect.stringContaining('![Uploading unique-id...]()'));
});

test('Text Area with Existing Text', async () => {
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('existing text');
  textArea.value = 'existing text';
  textArea.selectionStart = 8; // Cursor position

  await handleImageUpload(file);

  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id...]()'));
});

test('Empty Text Area', async () => {
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('');

  await handleImageUpload(file);

  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id...]()'));
});

test('Cursor at End of Text', async () => {
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('some text');
  textArea.value = 'some text';
  textArea.selectionStart = textArea.value.length; // Cursor at end

  await handleImageUpload(file);

  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id...]()'));
});

test('Cursor at Start of Text', async () => {
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('some text');
  textArea.value = 'some text';
  textArea.selectionStart = 0; // Cursor at start

  await handleImageUpload(file);

  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id...]()'));
});

test('Invalid File Type', async () => {
  const nonImageFile = new File(['dummy content'], 'example.txt', { type: 'text/plain' });

  await handleImageUpload(nonImageFile);

  expect(onChange).not.toHaveBeenCalled();
  expect(uploadImage).not.toHaveBeenCalled();
});

test('Null File Input', async () => {
  await handleImageUpload(null);

  expect(onChange).not.toHaveBeenCalled();
  expect(uploadImage).not.toHaveBeenCalled();
});

test('Upload Failure', async () => {
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('existing text');
  uploadImage.mockRejectedValueOnce(new Error('Upload failed'));

  await handleImageUpload(file);

  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Failed to upload unique-id...]()'));
});

test('Large Image File', async () => {
  const largeFile = new File([new ArrayBuffer(10 * 1024 * 1024)], 'large.png', { type: 'image/png' });
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('existing text');
  uploadImage.mockResolvedValueOnce();

  await handleImageUpload(largeFile);

  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id...]()'));
});

test('Multiple Consecutive Uploads', async () => {
  uuidv4.mockReturnValue('unique-id-1');
  textareaValue.mockReturnValue('existing text');
  uploadImage.mockResolvedValueOnce();

  await handleImageUpload(file);

  uuidv4.mockReturnValue('unique-id-2');
  await handleImageUpload(file);

  expect(onChange).toHaveBeenCalledTimes(2);
  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id-1...]()'));
  expect(onChange).toHaveBeenCalledWith(expect.stringContaining('![Uploading unique-id-2...]()'));
});

test('Textarea Not Found', async () => {
  document.body.removeChild(textArea); // Simulate textarea not found

  await handleImageUpload(file);

  expect(onChange).not.toHaveBeenCalled();
});

test('Simultaneous Uploads', async () => {
  uuidv4.mockReturnValue('unique-id');
  textareaValue.mockReturnValue('existing text');
  uploadImage.mockResolvedValueOnce();

  await Promise.all([handleImageUpload(file), handleImageUpload(file)]);

  expect(onChange).toHaveBeenCalledTimes(2);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant