Skip to content
On this page

ActMaster test-utils

ActTest

ActTest - is a class that helps write tests for act-master.

ActTest - is a singleton class with static methods.

To write tests, use the method ActTest.getInstance(options?: ActMasterOptions).

It creates an instance of the act-master class, which can be easily used afterwards.

Below, instead of an example action, you will be testing your action.

Testing Act

Example 1:

ts
import { ActTest } from 'act-master';

it('Example result', async () => {
  const $act = ActTest.getInstance();

  // Arrange
  const action: ActMasterAction = {
    name: 'SomeName',
    exec() {
      return 42;
    },
  };

  $act.addActions([action]);

  // Act
  await $act.exec('SomeName');

  // Assert
  expect(ActTest.getLastResult()).toBe(42);
});

Testing subscription

You can also call some event and check your subscription.

Example 2:

ts
import { ActTest } from 'act-master';

it('Example check subscription', async () => {
  // Arrange
  const $act = ActTest.getInstance();

  const action: ActMasterAction = {
    name: 'SomeName',
    exec() {
      return 42;
    },
  };

  $act.addActions([action]);

  const mockFn = jest.fn();

  $act.subscribe('SomeName', mockFn);

  // Act
  await ActTest.exec('SomeName');

  // Assert
  expect(mockFn).toBeCalledTimes(1);
});

List of available methods

Method NameDescription
getInstanceReturns the ActMaster instance
resetAllResets the ActMaster settings
getLastResultReturns the last value
addActionsAdds actions
makeActionStubCreate empty action for testing
execExecute action
subscribeSubscribes to action
entityCountReturns the number of entities ('actions' | 'waiters' | 'listeners' | 'di') *
removeSingletonRemoves singleton ActMaster *

* -Use if you know what it's for

More examples in repo