6

I've been looking at example Jest tests here trailheadapps / lwc-recipes but don't see examples of e.g.:

(This Jest Test Patterns and Mock Dependencies documentation is worth a read.)

So I am wondering if there are other publicly accessible repositories with more examples of Jest tests for LWCs? (There is plenty of material on Jest: it is the specific patterns of use of LWC and Jest together that I'm looking for.)

2 Answers 2

1

I can find these:-

  1. wire-service-jest-util - A utility so Lightning Web Component unit tests can control the data provisioned with @wire.
  2. lwc directory of lwc-recipes repo - Many recipes have a tests directory with commented jest tests.

enter image description here

2
  • Thanks Sanket, I already had the second one listed in the question.
    – Keith C
    Mar 24, 2020 at 12:19
  • ahh, I thought you were just referring to the documentation there. Mar 24, 2020 at 12:23
0

Here is a "best guess" at some of these questions that appears to work, but I couldn't figure out how to dispatch a keyboard event that automatically went to the focussed element. Note that await on a non-promise automatically adds a wrapping promise.

it('has keyboard capability', async () => {

    const element = createElement('c-datepicker', { is: Datepicker });
    element.startYear = 2020;
    element.startMonth = 1;
    element.numberOfMonths = 2;

    element.scheduleStart = SCHEDULE_START;
    element.scheduleMinutes = SCHEDULE_MINUTES;
    element.holidayDates = [];
    element.alreadyRequesteds = new Map();
    element.selectionMode = 'dates';

    document.body.appendChild(element);

    const tueJan14 = element.shadowRoot.querySelector('td[data-day-number="7317"]');
    await tueJan14.focus();
    await tueJan14.dispatchEvent(new KeyboardEvent('keydown', {'keyCode': ENTER_KEY_CODE}));
    expect(element.selectedDates).toStrictEqual([
        new Date(2020, 0, 14)
    ]);

    const wedJan15 = element.shadowRoot.querySelector('td[data-day-number="7318"]');
    await wedJan15.focus();
    await wedJan15.dispatchEvent(new KeyboardEvent('keydown', {'keyCode': ENTER_KEY_CODE}));
    expect(element.selectedDates).toStrictEqual([
        new Date(2020, 0, 14),
        new Date(2020, 0, 15)
    ]);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.