import { isTypeValid } from '.';

const pdfFile = new Blob(undefined, {
  type: 'application/pdf',
});

const pngFile = new Blob(undefined, {
  type: 'image/png',
});

const data64Img = 'data:image/png;something';

describe('isTypeValid', () => {
  describe('when type is provided', () => {
    const files = [pdfFile, pngFile] as const;

    it.each(files)('returns true for wildcard rule %s', (file: (typeof files)[number]) => {
      expect(isTypeValid(file, '*')).toBe(true);
    });

    it('returns true for matching type', () => {
      expect(isTypeValid(pdfFile, 'application/*')).toBe(true);
    });

    it('returns true for matching type when multiple rules provided', () => {
      expect(isTypeValid(pdfFile, 'application/*, image/*')).toBe(true);
    });

    it('returns true for matching subtype', () => {
      expect(isTypeValid(pdfFile, 'application/pdf')).toBe(true);
    });

    it('returns true for matching subtype when multiple rules provided', () => {
      expect(isTypeValid(pngFile, 'application/xls, image/png')).toBe(true);
    });

    it('can parse multiple types with extra whitespace', () => {
      expect(isTypeValid(pngFile, ' application/xls,  image/png ')).toBe(true);
    });

    it('returns false for unsupported type', () => {
      expect(isTypeValid(pngFile, 'application/*')).toBe(false);
    });

    it('returns false for unsupported subtype', () => {
      expect(isTypeValid(pdfFile, 'application/xls')).toBe(false);
    });

    it('returns false for unsupported subtype when multiple rules provided', () => {
      expect(isTypeValid(pngFile, 'application/xls, image/jpeg')).toBe(false);
    });
  });
  describe('when type is not provided', () => {
    it('returns true for supported file', () => {
      expect(isTypeValid(new Blob(), 'image/png', data64Img)).toBe(true);
      expect(isTypeValid(new Blob(), 'image/*', data64Img)).toBe(true);
    });

    it('returns false for unsupported file', () => {
      expect(isTypeValid(new Blob(), 'image/jpeg', data64Img)).toBe(false);
      expect(isTypeValid(new Blob(), 'application/*', data64Img)).toBe(false);
    });
  });
});
