import { isFileImage, getImageFromFile } from '@/helpers/file.helper';
import { base64StringToBlob } from 'blob-util';

const file = new File(
  [
    base64StringToBlob(
      'iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==',
      'image/png',
    ),
  ],
  'image.png',
  { type: 'image/png' },
);

describe('File Helper', () => {
  describe('isFileImage', () => {
    it('should return false if file is not image', () => {
      expect(isFileImage({ ...file, type: 'toto' } as File)).toEqual(false);
    });
    it('should return true if file is png', () => {
      expect(isFileImage({ ...file, type: 'image/png' } as File)).toEqual(true);
    });
    it('should return false if file is gif', () => {
      expect(isFileImage({ ...file, type: 'image/gif' } as File)).toEqual(true);
    });
    it('should return false if file is jpeg', () => {
      expect(isFileImage({ ...file, type: 'image/jpeg' } as File)).toEqual(
        true,
      );
    });
  });
  describe('getImageFromFile', () => {
    it('should return promise with formatted answer for image', async () => {
      const result = await getImageFromFile(file);
      expect(result).toEqual({
        img: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==',
        name: 'image.png',
      });
    });
    it('should reject promise if file is not correct', async () => {
      expect.assertions(1);
      try {
        await getImageFromFile(
          new File(
            [
              base64StringToBlob(
                'iVBORw0KGgoAAAANSUh2P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==',
                'toto',
              ),
            ],
            'image.png',
            { type: 'titi' },
          ),
        );
      } catch (error) {
        expect(error).toBeTruthy();
      }
    });
  });
});
