3

I would like to add a simple jest test to my lightning web component in order to test the functionality of the following on submit event handler.

handleSubmit(event) {
    event.preventDefault();
    const fields = event.detail.fields;
    fields.Some_field__c = 'Some value';
    this.template.querySelector('lightning-record-edit-form').submit(fields);
}

It seems to me that I should be able to test that Some_field__c has been appropriately updated. The handle submit will fire in the jest test, but I can't find any documentation on how to read the subsequently resubmitted fields.

2
  • Hi there, have you gotten an answer to this? Commented Mar 18, 2020 at 17:10
  • @LourdesMontero No, unfortunately not
    – Kasper
    Commented Mar 18, 2020 at 23:11

1 Answer 1

3

You need to mock the form submit function

//Need to submit the form and verify that the record was submitted successfully
const form = element.shadowRoot.querySelector('lightning-record-edit-form');

//Mocks the submit function for lightning-record-edit-form
form.submit = jest.fn();

//Submit EVENT gets dispatched, which triggers a handler function that will call the submit FUNCTION
form.dispatchEvent(new CustomEvent('submit', {detail:{fields:{}}}));

//Checking that the submit FUNCTION was called with the correct rating value(4)
expect(form.submit).toHaveBeenCalledWith(EXPECTED_SUBMIT_VALUE);
1
  • Thanks you, that works like a charm!
    – Kasper
    Commented Mar 20, 2020 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .