|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { ModulesConfigurator } from '@equinor/fusion-framework-module'; |
| 3 | + |
| 4 | +import type { IEventModuleProvider } from '../../provider'; |
| 5 | +import { module as realModule } from '../../module'; |
| 6 | +import { EventMockConfigurator, createEventMockModule, enableEventMock } from '../../mock'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Initializes the mock module through the real module system. |
| 10 | + * |
| 11 | + * @remarks |
| 12 | + * Deliberately avoids hand-building initialization arguments — the module |
| 13 | + * system's configure/initialize/post-initialize pipeline is exactly what a |
| 14 | + * test should exercise, since that pipeline (not a hand-rolled provider) is |
| 15 | + * what an application actually runs. |
| 16 | + * |
| 17 | + * @param configure - Optional callback receiving the mock configurator. |
| 18 | + * @returns The provider the module produced and the configurator it was |
| 19 | + * pinned to. |
| 20 | + */ |
| 21 | +const initializeMockWith = async ( |
| 22 | + configure?: (builder: EventMockConfigurator) => void, |
| 23 | +): Promise<{ provider: IEventModuleProvider; configurator: EventMockConfigurator }> => { |
| 24 | + const configurator = new ModulesConfigurator([]); |
| 25 | + const eventConfigurator = enableEventMock(configurator, configure); |
| 26 | + const instances = await configurator.initialize(); |
| 27 | + return { |
| 28 | + provider: (instances as unknown as { event: IEventModuleProvider }).event, |
| 29 | + configurator: eventConfigurator, |
| 30 | + }; |
| 31 | +}; |
| 32 | + |
| 33 | +describe('eventMockModule', () => { |
| 34 | + it('changes nothing but the configurator', () => { |
| 35 | + const { module: eventMockModule } = createEventMockModule(); |
| 36 | + expect(eventMockModule.name).toBe(realModule.name); |
| 37 | + expect(eventMockModule.version).toBe(realModule.version); |
| 38 | + expect(eventMockModule.initialize).toBe(realModule.initialize); |
| 39 | + expect(eventMockModule.postInitialize).toBe(realModule.postInitialize); |
| 40 | + expect(eventMockModule.dispose).toBe(realModule.dispose); |
| 41 | + }); |
| 42 | + |
| 43 | + it('records every dispatched event, in dispatch order', async () => { |
| 44 | + const { provider, configurator } = await initializeMockWith(); |
| 45 | + |
| 46 | + await provider.dispatchEvent('first', { detail: 1 }); |
| 47 | + await provider.dispatchEvent('second', { detail: 2 }); |
| 48 | + |
| 49 | + expect(configurator.events.map((e) => e.type)).toEqual([ |
| 50 | + // `onModulesLoaded` is dispatched by the module's own `postInitialize` |
| 51 | + 'onModulesLoaded', |
| 52 | + 'first', |
| 53 | + 'second', |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('filters recorded events by type', async () => { |
| 58 | + const { provider, configurator } = await initializeMockWith(); |
| 59 | + |
| 60 | + await provider.dispatchEvent('myEvent', { detail: 1 }); |
| 61 | + await provider.dispatchEvent('myEvent', { detail: 2 }); |
| 62 | + await provider.dispatchEvent('otherEvent', { detail: 3 }); |
| 63 | + |
| 64 | + expect(configurator.getEvents('myEvent').map((e) => e.detail)).toEqual([1, 2]); |
| 65 | + expect(configurator.lastEvent('myEvent')?.detail).toBe(2); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns undefined from lastEvent when nothing of that type dispatched', async () => { |
| 69 | + const { configurator } = await initializeMockWith(); |
| 70 | + expect(configurator.lastEvent('neverDispatched')).toBeUndefined(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('clears recorded history without touching interceptors', async () => { |
| 74 | + const { provider, configurator } = await initializeMockWith(); |
| 75 | + |
| 76 | + await provider.dispatchEvent('myEvent', { detail: 1 }); |
| 77 | + configurator.clear(); |
| 78 | + expect(configurator.events).toEqual([]); |
| 79 | + |
| 80 | + const intercept = vi.fn(); |
| 81 | + configurator.intercept('myEvent', intercept); |
| 82 | + configurator.clear(); |
| 83 | + await provider.dispatchEvent('myEvent', { detail: 2 }); |
| 84 | + expect(intercept).toHaveBeenCalledTimes(1); |
| 85 | + }); |
| 86 | + |
| 87 | + it('runs a registered interceptor before listeners', async () => { |
| 88 | + const { provider, configurator } = await initializeMockWith(); |
| 89 | + const order: string[] = []; |
| 90 | + |
| 91 | + configurator.intercept('myEvent', () => { |
| 92 | + order.push('intercepted'); |
| 93 | + }); |
| 94 | + provider.addEventListener('myEvent', () => { |
| 95 | + order.push('listened'); |
| 96 | + }); |
| 97 | + |
| 98 | + await provider.dispatchEvent('myEvent', { detail: null }); |
| 99 | + |
| 100 | + expect(order).toEqual(['intercepted', 'listened']); |
| 101 | + }); |
| 102 | + |
| 103 | + it('lets an interceptor cancel a cancelable event before listeners run', async () => { |
| 104 | + const { provider, configurator } = await initializeMockWith(); |
| 105 | + const listener = vi.fn(); |
| 106 | + |
| 107 | + configurator.intercept('myEvent', (event) => event.preventDefault()); |
| 108 | + provider.addEventListener('myEvent', listener); |
| 109 | + |
| 110 | + await provider.dispatchEvent('myEvent', { detail: null, cancelable: true }); |
| 111 | + |
| 112 | + expect(listener).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('removes an interceptor through its teardown', async () => { |
| 116 | + const { provider, configurator } = await initializeMockWith(); |
| 117 | + const intercept = vi.fn(); |
| 118 | + |
| 119 | + const teardown = configurator.intercept('myEvent', intercept); |
| 120 | + teardown(); |
| 121 | + await provider.dispatchEvent('myEvent', { detail: null }); |
| 122 | + |
| 123 | + expect(intercept).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('configures the recorder through enableEventMock', async () => { |
| 127 | + const { configurator } = await initializeMockWith((builder) => { |
| 128 | + builder.intercept('myEvent', (event) => event.preventDefault()); |
| 129 | + }); |
| 130 | + |
| 131 | + expect(configurator).toBeInstanceOf(EventMockConfigurator); |
| 132 | + }); |
| 133 | +}); |
0 commit comments