elementary_test 2.1.0 copy "elementary_test: ^2.1.0" to clipboard
elementary_test: ^2.1.0 copied to clipboard

A testing library which makes it easy to test elementary used apps. Built to be used with the elementary architecture package.

Elementary-test #

#

Elementary Logo

#

Owner Pub Version Pub points Pub Likes Pub popularity

Description #

A handy testing library for apps built with Elementary.

Overview #

This library offers some handy tools to test WidgetModel from the Elementary package.

testWidgetModel method #

The main testing tool is the testWidgetModel function, which executes a test. The parameters it requires are the name of the test, the function preparing the WidgetModel for testing, and the test function itself.

a testFunction parameter #

This is a function of the test itself. When passed the WidgetModel, this function describes its behavior and verifies the result. The function also uses a tester to manipulate the phases of the WidgetModel lifecycle and a BuildContext mock.

How to use #

void main() {
  late TestPageModelMock model;

  TestPageWidgetModel setUpWm() {
    model = TestPageModelMock();
    when(() => model.value).thenReturn(0);
    when(() => model.increment()).thenAnswer((invocation) => Future.value(1));

    return TestPageWidgetModel(model);
  }

  testWidgetModel<TestPageWidgetModel, TestPageWidget>(
    'calculatingState should return true while incrementing before the answer was recieved',
    setUpWm,
    (wm, tester, context) async {
      tester.init();

      when(() => model.increment()).thenAnswer(
        (invocation) => Future.delayed(
          const Duration(milliseconds: 30),
          () => 1,
        ),
      );

      unawaited(wm.increment());

      await Future<void>.delayed(
        const Duration(milliseconds: 10),
      );

      expect(wm.calculatingState.value, isTrue);
    },
  );

  testWidgetModel<TestPageWidgetModel, TestPageWidget>(
    'calculatingState should return false after get the answer of incrementing',
    setUpWm,
    (wm, tester, context) async {
      tester.init();

      when(() => model.increment()).thenAnswer(
        (invocation) => Future.delayed(
          const Duration(milliseconds: 30),
          () => 1,
        ),
      );

      unawaited(wm.increment());

      await Future<void>.delayed(
        const Duration(milliseconds: 31),
      );

      expect(wm.calculatingState.value, isFalse);
    },
  );

  testWidgetModel<TestPageWidgetModel, TestPageWidget>(
    'should happen smth depend on lifecycle',
    setUpWm,
    (wm, tester, context) async {
      tester.init();

      // Emulate didChangeDependencies happened.
      tester.didChangeDependencies();

      // Test what ever we expect happened in didChangeDependencies;
      // ...
    },
  );
}

class TestPageModelMock extends Mock with MockElementaryModelMixin implements TestPageModel {}

Maintainer #

Maintainer avatar

Mikhail Zotyev

Contributors thanks #

Big thanks to all these people, who put their effort into helping the project.

contributors

Special thanks to:

Dmitry Krutskikh, Konoshenko Vlad, and Denis Grafov for the early adoption and the first production feedback;

Alex Bukin for IDE plugins;

All members of the Surf Flutter Team for actively using and providing feedback.

Sponsorship #

Special sponsor of the project:

Surf

For all questions regarding sponsorship/collaboration connect with Mikhail Zotyev.

25
likes
150
pub points
72%
popularity

Publisher

verified publisherelementaryteam.dev

A testing library which makes it easy to test elementary used apps. Built to be used with the elementary architecture package.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

elementary, flutter, flutter_test, meta, mocktail

More

Packages that depend on elementary_test