Repository: tauri-apps/tauri
File: packages/api/src/mocks.ts (TypeScript)

Code before:
 *    payload: { foo: 'bar' }
 *  });
 * })
 * ```
 * `emitTo` is currently **not** supported by this mock implementation.
 *
 * @since 1.0.0
 */
export function mockIPC(
  cb: (cmd: string, payload?: InvokeArgs) => unknown
): void {
  mockInternals()

  function isEventPluginInvoke(cmd: string): boolean {
    return cmd.startsWith('plugin:event|')
  }

  function handleEventPlugin(cmd: string, args?: InvokeArgs): unknown {
    switch (cmd.split('|')[1]) {
      case 'listen':
        return handleListen(args as { event: EventName; handler: number })
      case 'emit':
        return handleEmit(args as { event: EventName; payload?: unknown })
      case 'unlisten':
        return handleRemoveListener(args as { event: EventName; id: number })
    }
  }

  const listeners = new Map<string, number[]>()
  function handleListen(args: { event: EventName; handler: number }) {
    if (!listeners.has(args.event)) {
      listeners.set(args.event, [])
    }
    listeners.get(args.event)!.push(args.handler)
    return args.handler
  }

  function handleEmit(args: { event: EventName; payload?: unknown }) {
    const eventListeners = listeners.get(args.event) || []
    for (const handler of eventListeners) {
      runCallback(handler, args)
    }
    return null
  }
  function handleRemoveListener(args: { event: EventName; id: number }) {
    const eventListeners = listeners.get(args.event)
    if (eventListeners) {
      const index = eventListeners.indexOf(args.id)
      if (index !== -1) {
        eventListeners.splice(index, 1)
      }

Review comment (suggestion):
```suggestion
    switch (cmd) {
      case 'plugin:event|listen':
        return handleListen(args as { event: EventName; handler: number })
      case 'plugin:event|emit':
        return handleEmit(args as { event: EventName; payload?: unknown })
      case 'plugin:event|unlisten':
        return handleRemoveListener(args as { event: EventName; id: number })
    }
```

I know the intention was to reduce duplications, but I think this makes it easier to understand

Code after:
 * afterEach(() => {
 *    clearMocks()
 * })
 *
 * test("mocked event", () => {
 *  mockIPC(() => {}, { shouldMockEvents: true }); // enable event mocking
 *
 *  const eventHandler = vi.fn();
 *  listen('test-event', eventHandler); // typically in component setup or similar
 *
 *  emit('test-event', { foo: 'bar' });
 *  expect(eventHandler).toHaveBeenCalledWith({
 *    event: 'test-event',
 *    payload: { foo: 'bar' }
 *  });
 * })
 * ```
 * `emitTo` is currently **not** supported by this mock implementation.
 *
 * @since 1.0.0
 */
export function mockIPC(
  cb: (cmd: string, payload?: InvokeArgs) => unknown,
  options?: MockIPCOptions
): void {
  mockInternals()

  function isEventPluginInvoke(cmd: string): boolean {
    return cmd.startsWith('plugin:event|')
  }

  function handleEventPlugin(cmd: string, args?: InvokeArgs): unknown {
    switch (cmd) {
      case 'plugin:event|listen':
        return handleListen(args as { event: EventName; handler: number })
      case 'plugin:event|emit':
        return handleEmit(args as { event: EventName; payload?: unknown })
      case 'plugin:event|unlisten':
        return handleRemoveListener(args as { event: EventName; id: number })
    }
  }

  const listeners = new Map<string, number[]>()
  function handleListen(args: { event: EventName; handler: number }) {
    if (!listeners.has(args.event)) {
      listeners.set(args.event, [])
    }
    listeners.get(args.event)!.push(args.handler)
    return args.handler
  }