Writing tests

The following widget test verifies the flag evaluation of "increment" flag key.

void main() {
  group('Counter Page tests ||', () {
    testWidgets(
      'counter increment by 3 each time when user logs in with email ending with "@feature.test"',
      (tester) async {
        await tester.pumpWidget(
          MaterialApp(
            home: FeatureScope<TestFeatureProvider>(
              register: () => TestFeatureProvider(),
              loader: (featureProvider) async => featureProvider.load(),
              child: const CounterPage(
                email: 'user@feature.test',
              ),
            ),
          ),
        );

        expect(find.text('0'), findsOneWidget);

        final fabFinder = find.byType(FloatingActionButton);
        expect(fabFinder, findsOneWidget);

        await tester.tap(fabFinder);
        await tester.pumpAndSettle();

        expect(find.text('3'), findsOneWidget);

        await tester.tap(fabFinder);
        await tester.pumpAndSettle();

        expect(find.text('6'), findsOneWidget);
      },
    );

    testWidgets(
      'counter uses default variant if none of the conditions matches the context',
      (tester) async {
        await tester.pumpWidget(
          MaterialApp(
            home: FeatureScope<TestFeatureProvider>(
              register: () => TestFeatureProvider(),
              loader: (featureProvider) async => featureProvider.load(),
              child: const CounterPage(
                email: 'user@example.com',
              ),
            ),
          ),
        );

        expect(find.text('0'), findsOneWidget);

        final fabFinder = find.byType(FloatingActionButton);
        expect(fabFinder, findsOneWidget);

        await tester.tap(fabFinder);
        await tester.pumpAndSettle();

        expect(find.text('1'), findsOneWidget);

        await tester.tap(fabFinder);
        await tester.pumpAndSettle();

        expect(find.text('2'), findsOneWidget);
      },
    );
  });
}

class TestFeatureProvider extends JsonFeatureProvider {
  void load() {
    feed(
      {
        "newTitle": {"state": "enabled"},
        "color": {
          "returnType": "number",
          "variants": {
            "red": 4294901760,
            "green": 4278255360,
            "blue": 4278190335,
            "orange": 4294934352
          },
          "defaultVariant": "blue",
          "state": "enabled",
          "rules": [
            {
              "action": {"variant": "green"},
              "conditions": [
                {
                  "context": "email",
                  "op": "ends_with",
                  "value": "@feature.flag"
                }
              ]
            },
            {
              "action": {"variant": "red"},
              "conditions": [
                {
                  "context": "email",
                  "op": "ends_with",
                  "value": "@feature.test"
                }
              ]
            },
            {
              "action": {"variant": "orange"},
              "conditions": [
                {"context": "email", "op": "ends_with", "value": "@example.com"}
              ]
            }
          ]
        },
        "increment": {
          "returnType": "number",
          "variants": {"single": 1, "double": 2, "triple": 3},
          "defaultVariant": "single",
          "state": "enabled",
          "rules": [
            {
              "action": {"variant": "double"},
              "conditions": [
                {
                  "context": "email",
                  "op": "ends_with",
                  "value": "@feature.flag"
                }
              ]
            },
            {
              "action": {"variant": "triple"},
              "conditions": [
                {
                  "context": "email",
                  "op": "ends_with",
                  "value": "@feature.test"
                }
              ]
            }
          ]
        }
      },
    );
  }
}