import { handleResponseMimeTypes } from './handle-response-mime-types';

describe('handleResponseMimeTypes', () => {
  test('gracefully handles empty values', async () => {
    const resp = await handleResponseMimeTypes(new Response());
    expect(Object.values(resp)).toHaveLength(0);
  });

  test('handles text', async () => {
    const TEXT = 'This is text';
    const resp = await handleResponseMimeTypes(
      new Response(TEXT, { headers: new Headers([['Content-Type', 'text/plain']]) }),
    );
    expect(resp.text).toBe(TEXT);
  });
  test('handles images', async () => {
    const DATA = new Uint8Array([0, 1, 2, 3]).buffer;
    const resp = await handleResponseMimeTypes(
      new Response(DATA, { headers: new Headers([['Content-Type', 'image/jpeg']]) }),
    );
    expect(resp.blobUrl).toBeDefined();
  });
  test('handles videos', async () => {
    const DATA = new Uint8Array([0, 1, 2, 3]).buffer;
    const resp = await handleResponseMimeTypes(
      new Response(DATA, { headers: new Headers([['Content-Type', 'video/mpeg4']]) }),
    );
    expect(resp.blobUrl).toBeDefined();
  });
  test('handles form data when URL encoded', async () => {
    const formData = 'foo=bar&baz=42';
    const resp = await handleResponseMimeTypes(
      new Response(formData, { headers: new Headers([['Content-Type', 'application/x-www-form-urlencoded']]) }),
    );
    expect(resp.formData.foo).toBe('bar');
    expect(resp.formData.baz).toBe('42');
  });
  test('handles JSON data', async () => {
    const json = '{ "foo": "bar", "baz": 42 }';
    const resp = await handleResponseMimeTypes(
      new Response(json, { headers: new Headers([['Content-Type', 'application/json']]) }),
    );
    expect(resp.text).toBe(json);
  });
});
