salesforce / sfdx-lwc-jest

Run Jest against LWC components in SFDX workspace environment
MIT License
162 stars 81 forks source link

feat: add toast details in platformShowToastEvent stub #331

Closed pozil closed 1 year ago

pozil commented 1 year ago

When testing a component that fires multiple toast with platformShowToastEvent we need to access the toast configuration in tests like this (full source):

// Mock handler for toast event
const handler = jest.fn();
// Add event listener to catch toast event
element.addEventListener(ShowToastEventName, handler);

// Some test logic

// Testing fired event
expect(handler).toHaveBeenCalled();
expect(handler.mock.calls[0][0].detail.title).toBe(TOAST_TITLE);
expect(handler.mock.calls[0][0].detail.message).toBe(TOAST_MESSAGE);
expect(handler.mock.calls[0][0].detail.variant).toBe(TOAST_VARIANT);

The previous version of the stub did not allow to pass the toast configuration so I made a PR to enable it.

For reference, we've been using this updated stub in our LWC Recipes tests for years.