bloc_presentation_test 0.3.0 copy "bloc_presentation_test: ^0.3.0" to clipboard
bloc_presentation_test: ^0.3.0 copied to clipboard

A testing library for Blocs/Cubits which mixin BlocPresentationMixin. To be used with bloc_presentation package.

example/lib/main.dart

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:bloc_presentation/bloc_presentation.dart';
import 'package:bloc_presentation_test/bloc_presentation_test.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:test/test.dart';

class CounterCubit extends Cubit<int>
    with BlocPresentationMixin<int, CounterCubitEvent> {
  CounterCubit() : super(0);
}

sealed class CounterCubitEvent {}

final class CounterPresentationEvent implements CounterCubitEvent {
  const CounterPresentationEvent();
}

class MockCounterPresentationCubit
    extends MockPresentationCubit<int, CounterCubitEvent>
    implements CounterCubit {}

class MockCounterCubit extends MockCubit<int> implements CounterCubit {}

void main() {
  mainMockPresentationCubit();
  mainWhenListenPresentation();
}

void mainMockPresentationCubit() {
  late MockCounterPresentationCubit mockCubit;

  setUp(() {
    mockCubit = MockCounterPresentationCubit();
  });

  tearDown(() {
    mockCubit.close();
  });

  test(
    'presentation stream emits events properly',
    () async {
      mockCubit.emitMockPresentation(const CounterPresentationEvent());

      await expectLater(
        mockCubit.presentation,
        emitsInOrder(
          <Matcher>[
            equals(const CounterPresentationEvent()),
          ],
        ),
      );
    },
  );
}

void mainWhenListenPresentation() {
  late MockCounterCubit mockCubit;
  late StreamController<CounterCubitEvent> controller;

  setUp(() {
    mockCubit = MockCounterCubit();
    controller = whenListenPresentation(
      mockCubit,
      initialEvents: [const CounterPresentationEvent()],
    );
  });

  tearDown(() {
    mockCubit.close();
  });

  test(
    'presentation stream emits events properly',
    () async {
      controller.add(const CounterPresentationEvent());

      await expectLater(
        mockCubit.presentation,
        emitsInOrder(
          <Matcher>[
            equals(const CounterPresentationEvent()),
            equals(const CounterPresentationEvent()),
          ],
        ),
      );
    },
  );
}
11
likes
150
points
7.15k
downloads

Publisher

verified publisherleancode.co

Weekly Downloads

A testing library for Blocs/Cubits which mixin BlocPresentationMixin. To be used with bloc_presentation package.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

bloc, bloc_presentation, flutter, mocktail

More

Packages that depend on bloc_presentation_test