Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:meta/meta.dart'; 4 : import 'package:rx_bloc/rx_bloc.dart'; 5 : import 'package:test/test.dart' as tester; 6 : 7 : /// Creates a new test for a specific bloc [state]. 8 : /// [rxBlocTest] will create the specific test and initialize it's state 9 : /// as well perform all the necessary operations based on provided parameters 10 : /// before [expect]ing the desired values. 11 : /// [rxBlocTest] will ensure that no additional states are emitted by closing 12 : /// the states stream just before the evaluation. 13 : /// 14 : /// [build] returns the bloc that will be used for testing as a Future. It 15 : /// should be used for bloc initialization such as creation and dependency 16 : /// injection. 17 : /// 18 : /// [state] is the desired state of the bloc that is being tested. It is a 19 : /// Stream of a specific testing type. 20 : /// 21 : /// [act] is an optional callback that passes the bloc on which additional 22 : /// actions can be applied. It should be used for adding events to the bloc. 23 : /// 24 : /// [expect] is an `Iterable` of matchers which the bloc is expected to emit 25 : /// after the [act] is executed. 26 : /// 27 : /// [wait] is an optional `Duration` which is used to wait for async operations 28 : /// within the `bloc` that take time to complete. 29 : /// 30 : /// [skip] is an optional `int` that is used to skip a specific number of values 31 : /// emitted by the [state]. The default value is 1 which skips the initial value 32 : /// If the [skip] value is set to 0, it will include the initial value. 33 : /// 34 1 : @isTest 35 : void rxBlocTest<B extends RxBlocTypeBase, StateOutputType>( 36 : String message, { 37 : required Future<B> Function() build, 38 : required Stream<StateOutputType> Function(B) state, 39 : required Iterable expect, 40 : Future<void> Function(B)? act, 41 : Duration? wait, 42 : int skip = 0, 43 : }) => 44 2 : tester.test(message, () async { 45 1 : final bloc = await build(); 46 2 : final checkingState = state(bloc).skip(skip).asBroadcastStream(); 47 : 48 2 : if (wait != null) await Future<void>.delayed(wait); 49 : 50 1 : tester.expect( 51 : checkingState, 52 1 : tester.emitsInOrder(expect), 53 : ); 54 : 55 1 : await act?.call(bloc); 56 : });