createPresentationBlocTest method

void createPresentationBlocTest(
  1. String pathTestPage,
  2. String featureName,
  3. String pageName,
  4. List<Map<String, String>> resultModelUnitTest,
)

Implementation

void createPresentationBlocTest(
  String pathTestPage,
  String featureName,
  String pageName,
  List<Map<String, String>> resultModelUnitTest,
) {
  final path = join(pathTestPage, 'presentation', 'bloc');
  DirectoryHelper.createDir(path);

  for (var e in resultModelUnitTest) {
    final apiName = e['apiName'];
    final className = apiName?.pascalCase;

    join(path, '${apiName?.snakeCase}_bloc_test.dart').write(
        '''// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables

import 'package:$featureName/$pageName/data/models/body/${apiName?.snakeCase}_body.dart' as body_${e['apiName']?.snakeCase};
import 'package:$featureName/$pageName/domain/entities/${apiName?.snakeCase}_entity.dart' as entity_${e['apiName']?.snakeCase};
import 'package:$featureName/$pageName/domain/usecases/${apiName?.snakeCase}_use_case.dart';
import 'package:$featureName/$pageName/presentation/bloc/${apiName?.snakeCase}/${apiName?.snakeCase}_bloc.dart';
import 'package:core/core.dart';
import 'package:dev_dependency_manager/dev_dependency_manager.dart';

class MockUseCase extends Mock implements ${className}UseCase {}

Future<void> main() async {
late ${className}Bloc bloc;
late MockUseCase mockUseCase;

setUp(() {
  mockUseCase = MockUseCase();
  bloc = ${className}Bloc(useCase: mockUseCase);
});

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

test('initial state should be Initial', () {
  expect(bloc.state, equals(${className}Initial()));
});

group('$className Bloc', () {
  ${getConstOrFinalValue(e['body'] ?? '')} body${e['apiName']?.pascalCase} = ${e['body']}
  ${getConstOrFinalValue(e['entity'] ?? '')} entity${e['apiName']?.pascalCase} = ${e['entity']}

  const timeoutFailed = TimeoutFailure('TimoutFailure');
  const internalFailed = InternalFailure('InternalFailure');

  const redirectionFailed = RedirectionFailure(
    'RedirectionFailure',
    statusCode: 300,
    jsonBody: '{}',
  );

  const clientFailed = ClientFailure(
    'ClientFailure',
    statusCode: 400,
    jsonBody: '{}',
  );

  const serverFailed = ServerFailure(
    'ServerFailure',
    statusCode: 500,
    jsonBody: '{}',
  );

  const unauthorizedFailed = UnauthorizedFailure(
    'UnauthorizedFailure',
    statusCode: 401,
    jsonBody: '{}',
  );

  blocTest<${className}Bloc, ${className}State>(
    'should get data from the subject use case',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer((_) async => Right(entity$className));
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
  );

  blocTest<${className}Bloc, ${className}State>(
    'should emit [Loading, Success] when data is gotten successfully',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer((_) async => Right(entity$className));
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
    expect: () => [
      ${className}Loading(body$className, null, null),
      ${className}Success(body$className, null, entity$className, null),
    ],
  );

  blocTest<${className}Bloc, ${className}State>(
    'should emit [Loading, Failed] when getting timeout failed',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer(
        (_) async => Left(timeoutFailed),
      );
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
    expect: () => [
      ${className}Loading(body$className, null, null),
      ${className}Failed(body$className, null, timeoutFailed, null),
    ],
  );

  blocTest<${className}Bloc, ${className}State>(
    'should emit [Loading, Failed] when getting unauthorized failed',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer(
        (_) async => Left(unauthorizedFailed),
      );
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
    expect: () => [
      ${className}Loading(body$className, null, null),
      ${className}Failed(body$className, null, unauthorizedFailed, null),
    ],
  );

  blocTest<${className}Bloc, ${className}State>(
    'should emit [Loading, Failed] when getting internal failed',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer(
        (_) async => Left(internalFailed),
      );
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
    expect: () => [
      ${className}Loading(body$className, null, null),
      ${className}Failed(body$className, null, internalFailed, null),
    ],
  );

  blocTest<${className}Bloc, ${className}State>(
    'should emit [Loading, Failed] when getting redirection failed',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer(
        (_) async => Left(redirectionFailed),
      );
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
    expect: () => [
      ${className}Loading(body$className, null, null),
      ${className}Failed(body$className, null, redirectionFailed, null),
    ],
  );

  blocTest<${className}Bloc, ${className}State>(
    'should emit [Loading, Failed] when getting client failed',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer(
        (_) async => Left(clientFailed),
      );
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
    expect: () => [
      ${className}Loading(body$className, null, null),
      ${className}Failed(body$className, null, clientFailed, null),
    ],
  );

  blocTest<${className}Bloc, ${className}State>(
    'should emit [Loading, Failed] when getting server failed',
    setUp: () {
      when(() => mockUseCase(body$className)).thenAnswer(
        (_) async => Left(serverFailed),
      );
    },
    verify: (bloc) {
      mockUseCase(body$className);
    },
    build: () => bloc,
    act: (bloc) => bloc.add(Fetch$className(body$className)),
    expect: () => [
      ${className}Loading(body$className, null, null),
      ${className}Failed(body$className, null, serverFailed, null),
    ],
  );
});
}''');

    StatusHelper.generated(
        join(path, '${apiName?.snakeCase}_bloc_test.dart'));
  }
}