dragUpload.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Upload, Icon } from '../../index';
  2. import { noop } from 'lodash';
  3. import { cssClasses } from '@douyinfe/semi-foundation/icons/constants';
  4. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  5. import { IconBolt } from '@douyinfe/semi-icons';
  6. const prefixCls = cssClasses.PREFIX;
  7. let action = 'https://semi.bytendance.com';
  8. function getUpload(props, formProps = {}) {
  9. if (!props.action) {
  10. props.action = action;
  11. }
  12. return mount(<Upload {...props} draggable></Upload>);
  13. }
  14. function trigger(upload, event) {
  15. const dragArea = upload.find(`.${BASE_CLASS_PREFIX}-upload-drag-area`);
  16. dragArea.simulate('drop', event);
  17. }
  18. const createFile = (size = 1024, name = 'semi-logo.png', type = 'image/png') => {
  19. return new File([new ArrayBuffer(size)], name, {
  20. type: type,
  21. });
  22. };
  23. const createEvent = files => {
  24. let event = {
  25. dataTransfer: {
  26. files,
  27. effectAllowed: 'none',
  28. },
  29. };
  30. return event;
  31. };
  32. describe('Drag Upload', () => {
  33. let requests;
  34. let xhr;
  35. window.URL.createObjectURL = jest.fn();
  36. beforeEach(() => {
  37. xhr = sinon.useFakeXMLHttpRequest();
  38. requests = [];
  39. xhr.onCreate = req => requests.push(req);
  40. window.URL.createObjectURL.mockReset();
  41. });
  42. afterEach(() => {
  43. xhr.restore();
  44. });
  45. // 1、Should start upload after drag
  46. it('draggable', () => {
  47. let spyOnSuccess = sinon.spy((res, file) => { });
  48. let props = {
  49. onSuccess: spyOnSuccess,
  50. };
  51. const upload = getUpload(props);
  52. const file = createFile(200, 'drag.jpg');
  53. const event = createEvent([file]);
  54. trigger(upload, event);
  55. let response = { code: 0, message: 'success' };
  56. requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(response));
  57. expect(spyOnSuccess.calledWith(response, file)).toEqual(true);
  58. });
  59. it('shouldn not trigger upload when upload is disabled', () => {
  60. let spyFileChange = sinon.spy(file => { });
  61. let props = {
  62. disabled: true,
  63. onFileChange: spyFileChange,
  64. };
  65. const upload = getUpload(props);
  66. const file = createFile(200, 'drag.jpg');
  67. const event = createEvent([file]);
  68. trigger(upload, event);
  69. expect(spyFileChange.callCount).toEqual(0);
  70. });
  71. it('dragMainText / dragIcon / dragSubText', () => {
  72. let dragMainText = <span>Drop files here to upload or Click to browse files</span>;
  73. let dragSubText = <span>Only support jpeg / pdf</span>;
  74. let dragIcon = <IconBolt />;
  75. let props = {
  76. dragIcon,
  77. dragMainText,
  78. dragSubText,
  79. };
  80. const upload = getUpload(props);
  81. expect(upload.exists(`.${BASE_CLASS_PREFIX}-upload-drag-area-icon .${BASE_CLASS_PREFIX}-icon-bolt`)).toEqual(true);
  82. expect(upload.find(`.${BASE_CLASS_PREFIX}-upload-drag-area-main-text`).contains(dragMainText)).toEqual(true);
  83. expect(upload.find(`.${BASE_CLASS_PREFIX}-upload-drag-area-sub-text`).contains(dragSubText)).toEqual(true);
  84. });
  85. it('draggable & accept', () => {
  86. let accept = 'application/pdf';
  87. let spyFileChange = sinon.spy(file => { });
  88. let props = {
  89. accept,
  90. onFileChange: spyFileChange,
  91. };
  92. const upload = getUpload(props);
  93. const invalidFile = createFile(200, 'drag.jpg');
  94. const validFile = createFile(200, 'drag.pdf', 'application/pdf');
  95. trigger(upload, createEvent([invalidFile]));
  96. expect(spyFileChange.callCount).toEqual(0);
  97. trigger(upload, createEvent([validFile]));
  98. expect(spyFileChange.callCount).toEqual(1);
  99. });
  100. it('draggable custom children', () => {
  101. let props = {
  102. children: <div className="test"></div>,
  103. };
  104. const upload = getUpload(props);
  105. expect(upload.find(`.${BASE_CLASS_PREFIX}-upload-drag-area-custom`).children().length).toEqual(1);
  106. expect(upload.exists(`.${BASE_CLASS_PREFIX}-upload-drag-area-custom .test`)).toEqual(true);
  107. });
  108. });