stateful_component_test.dart 2.1 KB
Newer Older
1 2
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
3 4 5
import 'package:test/test.dart';

import 'widget_tester.dart';
6
import 'test_widgets.dart';
7

8 9
void main() {
  test('Stateful component smoke test', () {
10 11 12 13
    testWidgets((WidgetTester tester) {

      void checkTree(BoxDecoration expectedDecoration) {
        OneChildRenderObjectElement element =
14
            tester.findElement((Element element) => element is OneChildRenderObjectElement);
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        expect(element, isNotNull);
        expect(element.renderObject is RenderDecoratedBox, isTrue);
        RenderDecoratedBox renderObject = element.renderObject;
        expect(renderObject.decoration, equals(expectedDecoration));
      }

      tester.pumpWidget(
        new FlipComponent(
          left: new DecoratedBox(decoration: kBoxDecorationA),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );

      checkTree(kBoxDecorationA);

      tester.pumpWidget(
        new FlipComponent(
          left: new DecoratedBox(decoration: kBoxDecorationB),
          right: new DecoratedBox(decoration: kBoxDecorationA)
        )
      );

      checkTree(kBoxDecorationB);

      flipStatefulComponent(tester);

      tester.pump();

      checkTree(kBoxDecorationA);

      tester.pumpWidget(
        new FlipComponent(
          left: new DecoratedBox(decoration: kBoxDecorationA),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );

      checkTree(kBoxDecorationB);
    });
54 55
  });

56
  test('Don\'t rebuild subcomponents', () {
57 58 59 60 61 62 63 64
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
        new FlipComponent(
          key: new Key('rebuild test'), // this is so we don't get the state from the TestComponentConfig in the last test, but instead instantiate a new element with a new state.
          left: new TestBuildCounter(),
          right: new DecoratedBox(decoration: kBoxDecorationB)
        )
      );
65

66
      expect(TestBuildCounter.buildCount, equals(1));
67

68
      flipStatefulComponent(tester);
69

70
      tester.pump();
71

72 73
      expect(TestBuildCounter.buildCount, equals(1));
    });
74
  });
75
}