createDataModelResponseTest method

void createDataModelResponseTest(
  1. String pathTestPage,
  2. String featureName,
  3. String pageName,
  4. String apiName,
  5. dynamic jsonResponse,
  6. dynamic responseVariable,
  7. bool isResponseList,
)

Implementation

void createDataModelResponseTest(
  String pathTestPage,
  String featureName,
  String pageName,
  String apiName,
  dynamic jsonResponse,
  dynamic responseVariable,
  bool isResponseList,
) {
  final path = join(pathTestPage, 'data', 'models', 'response');
  DirectoryHelper.createDir(path, recursive: true);

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

${isResponseList ? "import 'dart:convert';" : ''}

import 'package:$featureName/$pageName/mapper.dart';
import 'package:$featureName/$pageName/data/models/response/${apiName.snakeCase}_response.dart' as response_${apiName.snakeCase};
import 'package:$featureName/$pageName/domain/entities/${apiName.snakeCase}_entity.dart' as entity_${apiName.snakeCase};
import 'package:core/core.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
initializeDateFormatting();

${getConstOrFinalValue(responseVariable)} response${apiName.pascalCase} = $responseVariable
final ${isResponseList ? 'List<Map<String, dynamic>>' : 'Map<String, dynamic>'} map = ${getChangeDateTimeFromMapJson(jsonResponse)};

${isResponseList ? '''test('mapper response model to ${apiName.pascalCase}Entity entity', () async {
  expect(response${apiName.pascalCase}.map((e) => e.toEntity()).toList(), isA<List<entity_${apiName.snakeCase}.${apiName.pascalCase}Entity>>());
});''' : '''test('mapper response model to ${apiName.pascalCase}Entity entity', () async {
  expect(response${apiName.pascalCase}.toEntity(), isA<entity_${apiName.snakeCase}.${apiName.pascalCase}Entity>());
});'''}


group('fromJson', () {
  test(
    'should return a valid model when the JSON is real data',
    () async {
      // arrange
      final json = readJsonFile('test/${pageName}_test/json/${apiName}_success.json');
      // act
      ${isResponseList ? '''final mapResponse = jsonDecode(json);
      final result = mapResponse is List
          ? List.from(
              mapResponse.map((e) => response_${apiName.snakeCase}.${apiName.pascalCase}Response.fromMap(e)))
          : [response_${apiName.snakeCase}.${apiName.pascalCase}Response.fromMap(mapResponse)];''' : 'final result = response_${apiName.snakeCase}.${apiName.pascalCase}Response.fromJson(json);'}
      // assert
      expect(result, response${apiName.pascalCase});
    },
  );
});

group('fromMap', () {
  test(
    'should return a valid model when the Map is an map of response model',
    () async {
      // act
      ${isResponseList ? '''final result = map
          .map((e) =>
              response_${apiName.snakeCase}.${apiName.pascalCase}Response.fromMap(e))
          .toList();''' : 'final result = response_${apiName.snakeCase}.${apiName.pascalCase}Response.fromMap(map);'}
      // assert
      expect(result, response${apiName.pascalCase});
    },
  );
});

group('toMap', () {
  test(
    'should return a map containing the proper model',
    () async {
      // act
      ${isResponseList ? 'final result = response${apiName.pascalCase}.map((e) => e.toMap()).toList();' : 'final result = response${apiName.pascalCase}.toMap();'}
      // assert
      expect(result, map);
    },
  );
});

group('toJson', () {
  test(
    'should return a JSON String containing the proper model',
    () async {
      // act
      ${isResponseList ? 'final result = jsonEncode(response${apiName.pascalCase});' : 'final result = response${apiName.pascalCase}.toJson();'}
      // assert
      expect(result, isA<String>());
    },
  );
});
}''');

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