Elementary-test

Elementary Cover

Pub Version Pub Likes Pub popularity Flutter Platform

Description

A testing library which makes it easy to test Elementary used apps. Useful with the Elementary architecture package.

Overview

This library contains mechanisms for convenient testing of the WidgetModel of the Elementary package.

testWidgetModel method

The main testing mechanism is the function testWidgetModel that starts the test execution. The required parameters are the name of the test, the function of preparing WidgetModel for testing, and the function of the test body.

testFunction

This is test body function. This function uses the WidgetModel passed to it to describe the behavior and check the result, tester that allows you to regulate the stages of the WidgetModel life cycle, and the BuildContext mock.

How to use

void main() {
  late TestPageModelMock model;
  late ThemeWrapperMock theme;
  late TextThemeMock textTheme;

  TestPageWidgetModel setUpWm() {
    textTheme = TextThemeMock();
    when(() => textTheme.headline4).thenReturn(null);
    theme = ThemeWrapperMock();
    when(() => theme.getTextTheme(any())).thenReturn(textTheme);
    model = TestPageModelMock();
    when(() => model.value).thenReturn(0);
    when(() => model.increment()).thenAnswer((invocation) => Future.value(1));

    return TestPageWidgetModel(model, theme);
  }

  testWidgetModel<TestPageWidgetModel, TestPageWidget>(
    'counterStyle should be headline4',
    setUpWm,
        (wm, tester, context) {
      final style = TextStyleMock();
      when(() => textTheme.headline4).thenReturn(style);

      tester.init();

      expect(wm.counterStyle, style);
    },
  );
  
  testWidgetModel<TestPageWidgetModel, TestPageWidget>(
    'when call increment and before get answer valueState should be loading',
    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),
      );

      final value = wm.valueState.value;

      expect(value, isNotNull);
      expect(value!.isLoading, isTrue);
    },
  );
}

Our main sponsor is Surf.

Surf

Maintainer

Mikhail Zotyev

Libraries

elementary_test