jeudi 28 mai 2020

How to test FileReader wrapped inside promise?

Current code:

export const readFilePromise = file => {
  return new Promise((resolve, reject) => {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => {
      resolve(reader.result);
    };
    reader.onerror = error => {
      reject(error);
    };
  });
};

I am able to test the onload method

it('readFilePromise method', async () => {
    const fileContentsEncodedInHex = [
      '\x45\x6e\x63\x6f\x64\x65\x49\x6e\x48\x65\x78\x42\x65\x63\x61\x75\x73\x65\x42\x69\x6e\x61\x72\x79\x46\x69\x6c\x65\x73\x43\x6f\x6e\x74\x61\x69\x6e\x55\x6e\x70\x72\x69\x6e\x74\x61\x62\x6c\x65\x43\x68\x61\x72\x61\x63\x74\x65\x72\x73'
    ];
    const blob = new Blob(fileContentsEncodedInHex);
    const result = readFilePromise(blob);
    const output = await result;
    const expectedOutput =
      'data:;base64,RW5jb2RlSW5IZXhCZWNhdXNlQmluYXJ5RmlsZXNDb250YWluVW5wcmludGFibGVDaGFyYWN0ZXJz';
    expect(output).toEqual(expectedOutput);
  });

I wonder how do I test the onerror?

Aucun commentaire:

Enregistrer un commentaire