createDataRepositoryTest method

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

Implementation

void createDataRepositoryTest(
  String pathTestPage,
  String featureName,
  String pageName,
  List<Map<String, String>> resultModelUnitTest,
) {
  final path = join(pathTestPage, 'data', 'repositories');
  DirectoryHelper.createDir(path);
  join(path, '${pageName}_repository_impl_test.dart').write(
      '''// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables

import 'package:$featureName/$pageName/data/datasources/${pageName}_remote_data_source.dart';
import 'package:$featureName/$pageName/data/repositories/${pageName}_repository_impl.dart';
${resultModelUnitTest.map((e) => '''import 'package:$featureName/$pageName/data/models/body/${e['apiName']?.snakeCase}_body.dart' as body_${e['apiName']?.snakeCase};
import 'package:$featureName/$pageName/data/models/response/${e['apiName']?.snakeCase}_response.dart' as response_${e['apiName']?.snakeCase};
import 'package:$featureName/$pageName/domain/entities/${e['apiName']?.snakeCase}_entity.dart' as entity_${e['apiName']?.snakeCase};''').join('\n')}
import 'package:core/core.dart';
import 'package:dev_dependency_manager/dev_dependency_manager.dart';

class MockRemoteDataSource extends Mock implements ${pageName.pascalCase}RemoteDataSource {}

Future<void> main() async {
late MockRemoteDataSource mockRemoteDatasource;
late ${pageName.pascalCase}RepositoryImpl repository;

setUp(() {
  mockRemoteDatasource = MockRemoteDataSource();
  repository = ${pageName.pascalCase}RepositoryImpl(
    remoteDataSource: mockRemoteDatasource,
  );
});

${resultModelUnitTest.map((e) {
    final className = e['apiName']?.pascalCase;
    final methodName = e['apiName']?.camelCase;

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

  test(
      'should return response data when the call to remote data source is successful',
      () async {
    // arrange
    when(() => mockRemoteDatasource.$methodName(body$className)).thenAnswer((_) async => response$className);
    // act
    final result = await repository.$methodName(body$className);
    // assert
    verify(() => mockRemoteDatasource.$methodName(body$className));
    expect(result, equals(Right(entity$className)));
  });

  test(
    'should return redirection exception when the call to remote data source is unsuccessful',
    () async {
      final exception = RedirectionException(statusCode: 300, jsonBody: '{}');
      final failure = RedirectionFailure(
        exception.toString(),
        statusCode: 300,
        jsonBody: '{}',
      );
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );

  test(
    'should return client failure when the call to remote data source is unsuccessful',
    () async {
      final exception = ClientException(statusCode: 400, jsonBody: '{}');
      final failure = ClientFailure(
        exception.toString(),
        statusCode: 400,
        jsonBody: '{}',
      );
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );

  test(
    'should return server failure when the call to remote data source is unsuccessful',
    () async {
      final exception = ServerException(statusCode: 500, jsonBody: '{}');
      final failure = ServerFailure(
        exception.toString(),
        statusCode: 500,
        jsonBody: '{}',
      );
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );

  test(
    'should return unauthorized failure when the call to remote data source is unsuccessful',
    () async {
      final exception =
          UnauthorizedException(statusCode: 401, jsonBody: '{}');
      final failure = UnauthorizedFailure(
        exception.toString(),
        statusCode: 401,
        jsonBody: '{}',
      );
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );

  test(
    'should return timeout failure when the call to remote data source is unsuccessful',
    () async {
      final exception = TimeoutException();
      final failure = TimeoutFailure(exception.toString());
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );

  test(
    'should return internal failure when the call to remote data source is unsuccessful',
    () async {
      final exception = InternalException();
      final failure = InternalFailure(exception.toString());
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );

  test(
    'should return no internet failure when the call to remote data source is unsuccessful',
    () async {
      final exception = NoInternetException();
      final failure = NoInternetFailure(exception.toString());
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );

  test(
    'should return internal failure when the call to remote data source is unknown exception',
    () async {
      final exception = Exception('unknown');
      final failure = InternalFailure(exception.toString());
      // arrange
      when(() => mockRemoteDatasource.$methodName(body$className)).thenThrow(exception);
      // act
      final result = await repository.$methodName(body$className);
      // assert
      verify(() => mockRemoteDatasource.$methodName(body$className));
      expect(result, equals(Left(failure)));
    },
  );
});
''';
  }).join('\n')}
}''');

  StatusHelper.generated(join(path, '${pageName}_repository_impl_test.dart'));
}