JSPM

@beeper/matrix-widget-toolkit-testing

2.3.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q29659F
  • License Apache-2.0

Testing support for @matrix-widget-toolkit/api.

Package Exports

  • @beeper/matrix-widget-toolkit-testing
  • @beeper/matrix-widget-toolkit-testing/build/cjs/index.js
  • @beeper/matrix-widget-toolkit-testing/build/esm/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@beeper/matrix-widget-toolkit-testing) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@matrix-widget-toolkit/testing

@matrix-widget-toolkit/testing

Testing support for @matrix-widget-toolkit/api.

Usage

When testing widgets that are using @matrix-widget-toolkit/api you quickly run into the situation where you have to mock WidgetApi. While this can be done using jest, this can be a lot of repeated work and you might miss edge cases that makes the mock behave differently than the original. The package provides a mockWidgetApi helper to cover this case. The returned mock can be used to emulate a Widget host like Element, so that the widget can interact with the Matrix room, like sending and receiving events.

Install it with:

yarn add @matrix-widget-toolkit/testing

Setup

It's recommended to use a fresh mock instance in every test. After each test, make sure to stop the mock by calling stop() to free resources:

import { MockedWidgetApi, mockWidgetApi } from '@matrix-widget-toolkit/testing';

let widgetApi: MockedWidgetApi;

afterEach(() => widgetApi.stop());

beforeEach(() => (widgetApi = mockWidgetApi()));

Simple Event Mock

In test, you can use the mock to prepopulate the current Matrix room with events:

// Prepopulate the power levels event in the room:
widgetApi.mockSendStateEvent({
  type: 'm.room.power_levels',
  sender: '@user-id',
  content: {
    users: {
      '@my-user': 100,
    },
  },
  state_key: '',
  origin_server_ts: 0,
  event_id: '$event-id-0',
  room_id: '!room-id',
});

// You can receive it using any of the methods of the widget api:
const powerLevels = widgetApi.receiveSingleStateEvent('m.room.power_levels');

Verifying Sending of Events

As the methods of the mock are using jest.fn(), you can verify all of them:

// In your code, send an event:
widgetApi.sendStateEvent(
  'm.room.topic',
  { topic: 'A brief description' },
  { roomId: '!my-room' }
);

// Verify that the event was send in your test:
expect(widgetApi.sendStateEvent).toBeCalledWith(
  'm.room.topic',
  { topic: 'A brief description' },
  { roomId: '!my-room' }
);

Overriding Behavior

As the methods of the mock are using jest.fn(), you can change their default behavior by mocking them:

// Returns true by default, override to return false:
widgetApi.hasCapabilities.mockReturnValue(false);