flutter/packages/unit/test/widget/init_state_test.dart
Adam Barth 727ce65ffc Add package:flutter_test
This package contains WidgetTester, which is very useful when writing tests for
widgets.
2015-11-23 14:09:12 -08:00

35 lines
1.0 KiB
Dart

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';
List<String> ancestors = <String>[];
class TestComponent extends StatefulComponent {
TestComponentState createState() => new TestComponentState();
}
class TestComponentState extends State<TestComponent> {
void initState() {
super.initState();
context.visitAncestorElements((Element element) {
ancestors.add(element.widget.runtimeType.toString());
return true;
});
}
Widget build(BuildContext context) => new Container();
}
void main() {
test('initState() is called when we are in the tree', () {
testWidgets((WidgetTester tester) {
tester.pumpWidget(new Container(child: new TestComponent()));
expect(ancestors, equals(<String>['Container', 'RenderObjectToWidgetAdapter<RenderBox>']));
});
});
}