docbrown/app/javascript/article-form/components/__tests__/dragAndDropHelpers.test.js
ludwiczakpawel 6c3f5d51fe
Drop containers styling (#10203)
* Refactored drag n drop functionality to a hook and component.

* Add the snackbar to the article editor.

* Now you can drag and drop images in the post body content.

* Cleaning up a few things.

* Now post cover images can be dragged and dropped.

* Removed unused empty function.

* Added @testing-library/preact-hooks so that we can test hooks.

* Some renaming, small refactor.

* Added tests

* Added more tests.

* Added more tests.

* Added message about dropping only one image in post body.

* Added message about dropping only one image for cover image.

* put onDrop into it's own meethod.

* More tests.

* Added some API documentation.

* Updated editor help with drag and drop image help.

* drag and drop styling

* .

* revert

* Fixed broken test.

Co-authored-by: Nick Taylor <nick@dev.to>
2020-09-09 09:37:16 +02:00

127 lines
3.6 KiB
JavaScript

import {
handleImageDrop,
handleImageFailure,
matchesDataTransferType,
} from '../dragAndDropHelpers';
import { addSnackbarItem } from '../../../Snackbar';
import { processImageUpload } from '../../actions';
jest.mock('../../../Snackbar');
jest.mock('../../actions');
function getDropZoneElement() {
const textArea = document.createElement('textarea');
const dropZoneElement = document.createElement('div');
dropZoneElement.setAttribute('class', 'drop-area drop-area--active');
dropZoneElement.appendChild(textArea);
return textArea;
}
describe('Article drag and drop helpers', () => {
beforeEach(() => {
addSnackbarItem.mockReset();
});
describe('matchesDataTransferType', () => {
it('should match data transfer type Files when no data transfer type to match is set', () => {
expect(matchesDataTransferType(['Files'])).toEqual(true);
});
it('should match data transfer type when the type to match is in the list of types', () => {
expect(
matchesDataTransferType(
['SomeType', 'OtherType', 'BestType'],
'SomeType',
),
).toEqual(true);
});
it('should not match data transfer type when the type to match is not in the list of types', () => {
expect(
matchesDataTransferType(
['SomeType', 'OtherType', 'BestType'],
'NotInTheListType',
),
).toEqual(false);
});
});
describe('handleImageFailure', () => {
it('should handle image failure', () => {
handleImageFailure(new Error('oh no'));
expect(addSnackbarItem).toBeCalledTimes(1);
});
});
describe('handleImageDrop', () => {
it('should abort if data dropped is not files', () => {
const successHandler = jest.fn();
const failureHandler = jest.fn();
const imageHandler = handleImageDrop(successHandler, failureHandler);
const dropEvent = {
preventDefault: jest.fn(),
currentTarget: document.createElement('textarea'),
dataTransfer: { types: ['NotFiles'] },
};
imageHandler(dropEvent);
expect(processImageUpload).not.toHaveBeenCalled();
});
it('should abort if data dropped is multiple files', () => {
const successHandler = jest.fn();
const failureHandler = jest.fn();
const imageHandler = handleImageDrop(successHandler, failureHandler);
const dropEvent = {
preventDefault: jest.fn(),
currentTarget: getDropZoneElement(),
dataTransfer: {
types: ['Files'],
files: [
new File(['(⌐□_□)'], 'chucknorris.png', {
type: 'image/png',
}),
new File(['ʕʘ̅͜ʘ̅ʔ'], 'vandamme.png', {
type: 'image/png',
}),
],
},
};
imageHandler(dropEvent);
expect(addSnackbarItem).toHaveBeenCalledTimes(1);
expect(processImageUpload).not.toHaveBeenCalled();
});
it('should process image upload if data dropped is one image file', () => {
const successHandler = jest.fn();
const failureHandler = jest.fn();
const imageHandler = handleImageDrop(successHandler, failureHandler);
const dropEvent = {
preventDefault: jest.fn(),
currentTarget: getDropZoneElement(),
dataTransfer: {
types: ['Files'],
files: [
new File(['(⌐□_□)'], 'chucknorris.png', {
type: 'image/png',
}),
],
},
};
imageHandler(dropEvent);
expect(addSnackbarItem).not.toHaveBeenCalled();
expect(processImageUpload).toHaveBeenCalledTimes(1);
});
});
});