1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Angular unit test file upload input - DataTransfer is undefined

Discussão em 'Angular' iniciado por Cody Pritchard, Outubro 3, 2024 às 10:22.

  1. Trying to write a Jest unit test for a file upload input element.

    Based on what i've found online, I'm trying to mock selecting a file by programmatically setting the files attribute on the input element like so:

    function selectFile(inputElement: HTMLInputElement) {
    const dataTransfer = new DataTransfer();
    const mockFile = new File(['Hello World'], 'test-file.txt');
    dataTransfer.items.add(mockFile);
    inputElement.files = dataTransfer.files;
    inputElement.dispatchEvent(new InputEvent('change'));
    return fixture.whenStable();
    }


    However I just receive the error that "DataTransfer is not defined".

    Is there something I'm missing here? Why is DataTransfer undefined? Or how else am I supposed to mock selecting a file for a unit test with Jest?

    This is the post I found for reference. Here is another post of someone else using Jest and having the same issue. But the incident was closed without any resolution.

    Continue reading...

Compartilhe esta Página