diff --git a/client/src/controllers/InitController.test.js b/client/src/controllers/InitController.test.js index 88b161b97f..088abb8d72 100644 --- a/client/src/controllers/InitController.test.js +++ b/client/src/controllers/InitController.test.js @@ -103,4 +103,49 @@ describe('InitController', () => { expect(handleEvent).toHaveBeenCalledTimes(1); }); }); + + describe('when using custom event names', () => { + const handleEvent = jest.fn(); + document.addEventListener('w-init:ready', handleEvent); + document.addEventListener('custom:event', handleEvent); + document.addEventListener('other-custom:event', handleEvent); + + beforeAll(() => { + jest.clearAllMocks(); + + application?.stop(); + + // intentionally adding extra spaces in the event-value below + const events = 'custom:event other-custom:event '; + + document.body.innerHTML = ` +
+ `; + + application = Application.start(); + }); + + it('should dispatch additional events', async () => { + expect(handleEvent).not.toHaveBeenCalled(); + + application.register('w-init', InitController); + + await Promise.resolve(); // no delay, just wait for the next tick + + expect(handleEvent).toHaveBeenCalledTimes(3); + + expect(handleEvent.mock.calls.map(([event]) => event.type)).toEqual([ + 'w-init:ready', + 'custom:event', + 'other-custom:event', + ]); + }); + }); }); diff --git a/client/src/controllers/InitController.ts b/client/src/controllers/InitController.ts index 090bcbb781..336de58750 100644 --- a/client/src/controllers/InitController.ts +++ b/client/src/controllers/InitController.ts @@ -5,21 +5,28 @@ import { debounce } from '../utils/debounce'; * Adds the ability for a controlled element to add or remove classes * when ready to be interacted with. * - * @example + * @example - Dynamic classes when ready * + * + * @example - Custom event dispatching + * */ export class InitController extends Controller