import BmsInputFile from '@/components/form/BmsInputFile.vue';
import { mount } from '@vue/test-utils';
import { field } from '@/plugins/field';
import { vi } from 'vitest';
import { nextTick } from 'vue';
window['alert'] = () => {};
vi.spyOn(window, 'alert').mockImplementation(() => {});

const fakeFile = {
  lastModified: 1576593160479,
  lastModifiedDate: {},
  name: 'nirzar-pangarkar-5APj-fzKE-k-unsplash.jpg',
  size: 665780,
  type: 'image/jpeg',
  webkitRelativePath: '',
};

const factory = (props: any) => {
  return mount(BmsInputFile, {
    global: {
      plugins: [field],
    },
    props: {
      ...props,
      label: 'inputFile',
    },
  });
};

describe('BmsInputFile', () => {
  describe('drop behavior', () => {
    it('should update on drop file', async () => {
      const wrapper = factory({ modelValue: [], limit: 5 });

      const dropArea = wrapper.get('[data-testid="file-upload-drop-area"]');

      await dropArea.trigger('drop', {
        dataTransfer: {
          files: [fakeFile],
        },
      });

      await nextTick();

      expect(wrapper.emitted()['update:modelValue'][0]).toEqual([[fakeFile]]);
    });

    it('should not update when file too big', async () => {
      const wrapper = factory({
        modelValue: [],
        limit: 1,
      });

      const dropArea = wrapper.get('[data-testid="file-upload-drop-area"]');

      await dropArea.trigger('drop', {
        dataTransfer: {
          files: [{ ...fakeFile, size: 6665780 }],
        },
      });

      await nextTick();

      expect(wrapper.emitted()['update:modelValue']).not.toBeTruthy();
    });
  });

  describe('select behavior', () => {
    it('should update on drop file', async () => {
      const wrapper = factory({ modelValue: [], limit: 5 });

      (wrapper.vm as any).onSelectFile({ target: { files: { 0: fakeFile } } });

      await nextTick();

      expect(wrapper.emitted()['update:modelValue'][0]).toEqual([[fakeFile]]);
    });

    it('should not update when file too big', async () => {
      const wrapper = factory({ modelValue: [], limit: 1 });

      (wrapper.vm as any).onSelectFile({
        target: { files: { 0: { ...fakeFile, size: 6665780 } } },
      });
      await nextTick();

      expect(wrapper.emitted()['update:modelValue']).not.toBeTruthy();
    });
  });
});
