Skip to content

Make PerAccountStoreWidget usage testable #30

@gnprice

Description

@gnprice

We'll want to write tests for many of our widgets. A number of our widgets refer to PerAccountStoreWidget.of(context) to get their data, and we'll want to test many of those. So to do this, we'll need something a test can do to provide a PerAccountStoreWidget based on self-contained data chosen by the test.

I think the core of this will be what I mentioned in 882c8ae :

Widgets still hardcode the "live" GlobalStore, so we'll need
to make some further changes when we go to write tests for those
widgets.

That refers mainly to this line in the implementation of GlobalStoreWidget:

      final store = await LiveGlobalStore.load();

I think the solution here will probably be to take inspiration from the "binding" concept in Flutter itself, as in WidgetsFlutterBinding vs. TestWidgetsFlutterBinding, and explained more at BindingBase. I wouldn't try to integrate directly into that system, I think, but build something small and analogous to it.

This would look something like:

abstract class DataBinding {
  static DataBinding get instance => // …

  Future<GlobalStore> loadGlobalStore();
}

class LiveDataBinding extends DataBinding {
  static DataBinding ensureInitialized() {
    // …
  }

  // …
}

and then

  • the main function in lib/main.dart calls LiveDataBinding.ensureInitialized() before it calls runApp;
  • GlobalStoreWidget's implementation would say final store = await DataBinding.instance.loadGlobalStore();.

Meanwhile, in the test code, there'd be:

class TestDataBinding extends DataBinding // …

and our widget tests would call a TestDataBinding.ensureInitialized() instead.

Or something like that. Will have to start writing the code and see where it takes us.

Metadata

Metadata

Assignees

Labels

a-modelImplementing our data model (PerAccountStore, etc.)a-tests

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions