Skip to main content
added 841 characters in body
Source Link
Dave C
  • 467
  • 1
  • 6
  • 29

I have an LWC that updloads a file:

<lightning-input type="file" label={label.AttachInvoice} accept="image/png, image/jpg, image/gif, application/pdf" onchange={invoiceUploadHandler}></lightning-input>

Question: How do you set up a JEST test for this onchange handler? Where do you inject a test file?

Adding my handler:

    invoiceUploadHandler(event) {

    let theFile = event.target.files[0];
    let reader = new FileReader();
    let temp = this;

    reader.onload = function() {

        let theContents = this.result.split(/,/)[1]; 

        temp.invoiceUploads = temp.invoiceUploads.slice();
        temp.invoiceUploads.push('Carrier Invoice');

        doUploadFile({ loadId: temp.recordId, filename: 'Carrier Invoice', contentType: theFile.type, contents: theContents })

        .then(result => {             
            LightningUtil.processMessagesAndErrors2(temp, result);   
        })
        .catch(error => {
            LightningUtil.logError(temp, error);
        });    
    }

    reader.readAsDataURL(theFile);
}

Thanks.

I have an LWC that updloads a file:

<lightning-input type="file" label={label.AttachInvoice} accept="image/png, image/jpg, image/gif, application/pdf" onchange={invoiceUploadHandler}></lightning-input>

Question: How do you set up a JEST test for this onchange handler? Where do you inject a test file?

Thanks.

I have an LWC that updloads a file:

<lightning-input type="file" label={label.AttachInvoice} accept="image/png, image/jpg, image/gif, application/pdf" onchange={invoiceUploadHandler}></lightning-input>

Question: How do you set up a JEST test for this onchange handler? Where do you inject a test file?

Adding my handler:

    invoiceUploadHandler(event) {

    let theFile = event.target.files[0];
    let reader = new FileReader();
    let temp = this;

    reader.onload = function() {

        let theContents = this.result.split(/,/)[1]; 

        temp.invoiceUploads = temp.invoiceUploads.slice();
        temp.invoiceUploads.push('Carrier Invoice');

        doUploadFile({ loadId: temp.recordId, filename: 'Carrier Invoice', contentType: theFile.type, contents: theContents })

        .then(result => {             
            LightningUtil.processMessagesAndErrors2(temp, result);   
        })
        .catch(error => {
            LightningUtil.logError(temp, error);
        });    
    }

    reader.readAsDataURL(theFile);
}

Thanks.

Source Link
Dave C
  • 467
  • 1
  • 6
  • 29

How to JEST test a lightning-input of type "file"

I have an LWC that updloads a file:

<lightning-input type="file" label={label.AttachInvoice} accept="image/png, image/jpg, image/gif, application/pdf" onchange={invoiceUploadHandler}></lightning-input>

Question: How do you set up a JEST test for this onchange handler? Where do you inject a test file?

Thanks.