state_notifier_test 0.0.10 copy "state_notifier_test: ^0.0.10" to clipboard
state_notifier_test: ^0.0.10 copied to clipboard

A testing library which makes it easy to test StateNotifier. Built to be used with the state_notifier, riverpod or flutter_riverpod packages.

example/main.dart

import 'package:mockito/mockito.dart';
import 'package:state_notifier/state_notifier.dart';
import 'package:state_notifier_test/state_notifier_test.dart';
import 'package:test/test.dart';

// Mock StateNotifier
class MockCounterStateNotifier extends Mock implements CounterStateNotifier {}

void main() {
  group(
    'CounterStateNotifier',
    () {
      stateNotifierTest<CounterStateNotifier, int>(
        'emits [] when nothing is added',
        actions: (_) {},
        build: () => CounterStateNotifier(),
        expect: () => const <int>[],
      );

      stateNotifierTest<CounterStateNotifier, int>('emits [1] when increment is called',
          build: () => CounterStateNotifier(),
          actions: (CounterStateNotifier notifier) => notifier.increment(),
          expect: () => const <int>[1]);

      stateNotifierTest<CounterStateNotifier, int>(
        'emits [0,1] when increment is called with expectInitialState set to true',
        expectInitialState: true,
        build: () => CounterStateNotifier(),
        actions: (CounterStateNotifier notifier) => notifier.increment(),
        expect: () => const <int>[0, 1],
      );

      stateNotifierTest<CounterStateNotifier, int>(
        'tests matchers for expected result',
        expectInitialState: true,
        build: () => CounterStateNotifier(),
        actions: (CounterStateNotifier notifier) => notifier.increment(),
        expect: () => isA<List>().having((list) => list.length, 'contains 2 elements', 2),
      );
    },
  );
}

class CounterStateNotifier extends StateNotifier<int> {
  CounterStateNotifier() : super(0);

  void increment() {
    state = state + 1;
  }
}
21
likes
150
points
2.17k
downloads

Publisher

verified publisherthat-dev.me

Weekly Downloads

A testing library which makes it easy to test StateNotifier. Built to be used with the state_notifier, riverpod or flutter_riverpod packages.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

diff_match_patch, meta, mockito, state_notifier, test

More

Packages that depend on state_notifier_test