createFrom<St, T extends Widget?, Model extends Vm> static method

  1. @visibleForTesting
Model createFrom<St, T extends Widget?, Model extends Vm>(
  1. Store<St> store,
  2. VmFactory<St, T, Model> factory
)

To test the view-model generated by a Factory, use createFrom and pass it the store and the factory. Note this method must be called in a recently created factory, as it can only be called once per factory instance.

The method will return the view-model, which you can use to:

  • Inspect the view-model properties directly, or

  • Call any of the view-model callbacks. If the callbacks dispatch actions, you use await store.waitActionType(MyAction), or await store.waitAllActionTypes([MyAction, OtherAction]), or await store.waitCondition((state) => ...), or if necessary you can even record all dispatched actions and state changes with Store.record.start() and Store.record.stop().

Example:

var store = Store(initialState: User("Mary"));
var vm = Vm.createFrom(store, MyFactory());

// Checking a view-model property.
expect(vm.user.name, "Mary");

// Calling a view-model callback and waiting for the action to finish.
vm.onChangeNameTo("Bill"); // Dispatches SetNameAction("Bill").
await store.waitActionType(SetNameAction);
expect(store.state.name, "Bill");

// Calling a view-model callback and waiting for the state to change.
vm.onChangeNameTo("Bill"); // Dispatches SetNameAction("Bill").
await store.waitCondition((state) => state.name == "Bill");
expect(store.state.name, "Bill");

Implementation

@visibleForTesting
static Model createFrom<St, T extends Widget?, Model extends Vm>(
  Store<St> store,
  VmFactory<St, T, Model> factory,
) {
  internalsVmFactoryInject(factory, store.state, store);
  return internalsVmFactoryFromStore(factory) as Model;
}