+ Start a Discussion
Angel ZAngel Z 

checkbox click lwc jest

Hello,

I have a LWC record edit form which updates custom fields on contact.
 
<lightning-record-edit-form  
        record-id={contactrecordid}
        object-api-name="Contact">
        <lightning-messages></lightning-messages>
        <div class="textGeneric">
          <lightning-input-field  data-id="newsletter" onchange={detectCheckboxUpdate} value={customFieldVal} variant="standard" field-name="Custom_Field__c"></lightning-input-field>
        </div>
        <div class="textGeneric">
          <button data-id="formSubmitButton" onclick={updateContactSubscription} class="saveChangesButton" type="submit" name="submit">{labels.saveChanges}</button>
       </lightning-record-edit-form>
 
@api detectCheckboxUpdate(event) {
    this.changeSaved = false;
    switch (event.currentTarget.dataset.id) {
      default:
      case 'customValue1':
      this.newsletterChanged = true;
      break;
    }....
}
it('updates the checkbox', () => {
    const element = createElement('c-lwc', {
      is: lwc
    });
    document.body.appendChild(element);
    getRecordWireAdapter.emit(mockGetRecord);
    const newsletterCheckbox = element.shadowRoot.querySelector('lightning-input-field[data-id="newsletter"]');
    const newslEvent = new CustomEvent('click', { currentTarget: { dataset: { id: 'newsletter' } } });
    element.dispatchEvent(newslEvent);
    element.detectCheckboxUpdate(newslEvent);
    const buttonEl = element.shadowRoot.querySelector('button[data-id="formSubmitButton"]');
    buttonEl.dispatchEvent(new CustomEvent('click'));
    return Promise.resolve().then(() => {
      expect(newsletterCheckbox.value).toBe(false);
    });
  });

I get an error on the switch statement "TypeError: Cannot read property 'dataset' of null"

Any ideas how to fix that?
Thank you!