npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@matrix-widget-toolkit/testing

v3.0.1

Published

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

Downloads

705

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 vitest, 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 vi.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 vi.fn(), you can change their default behavior by mocking them:

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