TestUnitWidget class

Provide methods of testing unit widgets.

Example:

Branch -------------

class Branch extends UnitWidget<BranchPaper> {
  // ignore: use_key_in_widget_constructors
  Branch(super.parent, {required super.agent, super.listener});

  @override
  UnitWidgetState<BranchPaper, Branch> createState() => BranchState();

  @override
  Script<BranchPaper, BranchState> createScript() => BranchScript();
}

class BranchState extends UnitWidgetState<BranchPaper, Branch> {
  final bigLeaf = WidgetAgent<LeafPaper>();
  final smallLeaf = WidgetAgent<LeafPaper>();

  Color? smallLeafColor;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            children: [
              Leaf(this, agent: bigLeaf),
              Leaf(this, agent: smallLeaf, listener: smallLeafListener),
            ],
          ),
        ),
      ),
    );
  }

  PaperListener<LeafPaper, BranchPaper> get smallLeafListener => PaperListener(
        (r) => r.on<LeafColor>((p) {
          return BranchLeafColor(p.color);
        }),
      );
}

class BranchScript extends Script<BranchPaper, BranchState> {
  @override
  void map() => on<BranchLeafColor>(onLeafColor);

  void onLeafColor(
    BranchLeafColor p,
    BranchState s,
    SourceVerifier ifFrom,
  ) async {
    await s.bigLeaf.process(LeafColor(p.color));

    final childPaper = await s.smallLeaf.report<LeafColor>();
    s.smallLeafColor = childPaper?.color;
  }
}

class BranchPaper extends Paper {}

class BranchLeafColor extends BranchPaper {
  BranchLeafColor(this.color);

  final Color color;
}

Leaf -------------

class Leaf extends UnitWidget<LeafPaper> {
  // ignore: use_key_in_widget_constructors
  Leaf(super.parent, {required super.agent, super.listener});

  @override
  UnitWidgetState<LeafPaper, Leaf> createState() => LeafState();

  @override
 Script<LeafPaper, LeafState> createScript() => LeafScript();
}

class LeafState extends UnitWidgetState<LeafPaper, Leaf> {
  Color color = Colors.green;

  @override
  Widget build(BuildContext context) {
    return Container(width: 30, height: 30, color: color);
  }
}

class LeafScript extends Script<LeafPaper, LeafState> {}

class LeafPaper extends Paper {}

class LeafColor extends LeafPaper {
  LeafColor(this.color);

  final Color color;
}

Unit testing

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:paper/paper.dart';

void main(){
  TestUnitWidget.toProcessPaper<Branch, BranchState>(
    'testing [BranchLeafColor]',
    create: (s) => Branch(s, agent: WidgetAgent<BranchPaper>()),
    setup: (state, agentValidator) {
      when(agentValidator.report(state.smallLeaf, LeafColor))
          .thenAnswer((_) async => LeafColor(Colors.yellow));
    },
    paper: BranchLeafColor(Colors.red),
    test: (tester, state, agentValidator) {
      expect(find.byType(Branch), findsOneWidget);
      expect(find.byType(Leaf), findsNWidgets(2));

      verify(
       agentValidator.process(
          state.bigLeaf,
          argThat(isA<LeafColor>().having((e) => e.color, 'color', Colors.red)),
        ),
      ).called(1);
      expect(state.smallLeafColor, Colors.yellow);
    },
  );

  TestUnitWidget.toReceivePaper<Branch, BranchState>(
    'testing [LeafColor] from [state.smallLeaf]',
    create: (s) => Branch(s, agent: WidgetAgent<BranchPaper>()),
    paper: LeafColor(Colors.red),
    from: (state) => state.smallLeaf,
    expected: isA<BranchLeafColor>().having(
      (e) => e.color,
      'color',
      Colors.red,
    ),
  );
}

Constructors

TestUnitWidget.new()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

toProcessPaper<U extends UnitWidget<Paper>, S extends UnitWidgetState<Paper, U>>(String description, {required U create(UnitWidgetState<Paper, UnitWidget<Paper>> s), void setup(S state, AgentValidator agentValidator)?, required Paper? paper, required void test(WidgetTester tester, S state, AgentValidator agentValidator), bool? skip, Timeout? timeout, bool semanticsEnabled = true, TestVariant<Object?> variant = const flutter_test.DefaultTestVariant(), dynamic tags, int? retry, LeakTesting? experimentalLeakTesting}) → void
Create a test to verify the operations when U unit widget handles paper with the given description. U is type the of target unit. S is the type of corresponding state of U.
toReceivePaper<U extends UnitWidget<Paper>, S extends UnitWidgetState<Paper, U>>(String description, {required U create(UnitWidgetState<Paper, UnitWidget<Paper>> s), void setup(S state, AgentValidator agentValidator)?, required Paper paper, required Agent<Paper> from(S state), required dynamic expected, bool? skip, Timeout? timeout, bool semanticsEnabled = true, TestVariant<Object?> variant = const flutter_test.DefaultTestVariant(), dynamic tags, int? retry, LeakTesting? experimentalLeakTesting}) → void
Create a test to verify the operations when U unit widget handles paper with the given description. U is type the of target unit. S is the type of corresponding state of U.