From 3d51b36103b342d6f0ad2d58a642fbc79eff637d Mon Sep 17 00:00:00 2001 From: wakamenod Date: Mon, 24 Jul 2023 16:24:50 +0900 Subject: [PATCH] =?UTF-8?q?[Mobile]=20[Test]=20=E5=86=86=E3=82=B0=E3=83=A9?= =?UTF-8?q?=E3=83=95=E3=83=AD=E3=82=B8=E3=83=83=E3=82=AF=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E8=BF=BD=E5=8A=A0=20#360?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../charts/services/pie_chart_service.dart | 2 +- .../services/pie_chart_service_test.dart | 145 ++++++++++++++++++ mobile/test/src/mocks.dart | 4 + 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 mobile/test/src/features/charts/services/pie_chart_service_test.dart diff --git a/mobile/lib/src/features/charts/services/pie_chart_service.dart b/mobile/lib/src/features/charts/services/pie_chart_service.dart index 17b946e..68e8b2d 100644 --- a/mobile/lib/src/features/charts/services/pie_chart_service.dart +++ b/mobile/lib/src/features/charts/services/pie_chart_service.dart @@ -11,7 +11,7 @@ part 'pie_chart_service.g.dart'; class PieChartController extends _$PieChartController { @override Future build() async { - final DateTime now = DateTime.now(); + final DateTime now = ref.read(currentTimeProvider); final DateTime firstDayOfMonth = DateTime(now.year, now.month, 1); final initialRange = DateTimeRange(start: firstDayOfMonth, end: now); diff --git a/mobile/test/src/features/charts/services/pie_chart_service_test.dart b/mobile/test/src/features/charts/services/pie_chart_service_test.dart new file mode 100644 index 0000000..0891626 --- /dev/null +++ b/mobile/test/src/features/charts/services/pie_chart_service_test.dart @@ -0,0 +1,145 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:openapi/openapi.dart'; +import 'package:suito/src/features/charts/repositories/pie_chart_data_repository.dart'; +import 'package:suito/src/features/charts/services/pie_chart.dart'; +import 'package:suito/src/features/charts/services/pie_chart_service.dart'; +import 'package:suito/src/utils/datetime_utils.dart'; + +import '../../../mocks.dart'; + +void main() { + late MockPieChartDataRepository pieChartDataRepository; + setUp(() { + pieChartDataRepository = MockPieChartDataRepository(); + }); + + ProviderContainer makeProviderContainer({DateTime? now}) { + final container = ProviderContainer( + overrides: [ + pieChartDataRepositoryProvider + .overrideWithValue(pieChartDataRepository), + if (now != null) currentTimeProvider.overrideWithValue(now), + ], + ); + addTearDown(container.dispose); + return container; + } + + group('PieChartController', () { + test('initial value', () async { + final start = DateTime(2023, 7, 1); + final now = DateTime(2023, 7, 24); + final container = makeProviderContainer(now: now); + final categoryData = [ + PieChartData((b) => b + ..name = 'Pie chart category data 1' + ..amount = 400), + PieChartData((b) => b + ..name = 'Pie chart category data 2' + ..amount = 700) + ]; + final locationData = [ + PieChartData((b) => b + ..name = 'Pie chart location data name 1' + ..amount = 400), + PieChartData((b) => b + ..name = 'Pie chart location data name 2' + ..amount = 900) + ]; + final res = GetPieChartDataRes((r) => r + ..categoryData.replace(categoryData) + ..locationData.replace(locationData)); + when(() => pieChartDataRepository.fetchPieChartData( + start.toYMD(), now.toYMD())).thenAnswer((_) => Future.value(res)); + // run + final PieChart data = + await container.read(pieChartControllerProvider.future); + // check + expect(data.res, res); + expect(data.type, PieChartType.category); + expect(data.dateRange, DateTimeRange(start: start, end: now)); + expect(data.currentTypeData, categoryData); + }); + + test('update pie chart type', () async { + final now = DateTime(2023, 7, 24); + final container = makeProviderContainer(now: now); + final categoryData = [ + PieChartData((b) => b + ..name = 'Pie chart category data 1' + ..amount = 400), + PieChartData((b) => b + ..name = 'Pie chart category data 2' + ..amount = 700) + ]; + final locationData = [ + PieChartData((b) => b + ..name = 'Pie chart location data name 1' + ..amount = 400), + PieChartData((b) => b + ..name = 'Pie chart location data name 2' + ..amount = 900) + ]; + final res = GetPieChartDataRes((r) => r + ..categoryData.replace(categoryData) + ..locationData.replace(locationData)); + when(() => pieChartDataRepository.fetchPieChartData(any(), any())) + .thenAnswer((_) => Future.value(res)); + // run + await container.read(pieChartControllerProvider.future); + await container + .read(pieChartControllerProvider.notifier) + .updateType(PieChartType.location.index); + // check + final data = await container.read(pieChartControllerProvider.future); + expect(data.currentTypeData, locationData); + }); + }); + + test('update date range', () async { + final start = DateTime(2023, 7, 1); + final now = DateTime(2023, 7, 24); + final container = makeProviderContainer(now: now); + final categoryData = [ + PieChartData((b) => b + ..name = 'Pie chart category data 1' + ..amount = 400), + PieChartData((b) => b + ..name = 'Pie chart category data 2' + ..amount = 700) + ]; + final locationData = [ + PieChartData((b) => b + ..name = 'Pie chart location data name 1' + ..amount = 400), + PieChartData((b) => b + ..name = 'Pie chart location data name 2' + ..amount = 900) + ]; + final res = GetPieChartDataRes((r) => r + ..categoryData.replace(categoryData) + ..locationData.replace(locationData)); + when(() => pieChartDataRepository.fetchPieChartData( + start.toYMD(), now.toYMD())).thenAnswer((_) => Future.value(res)); + final updateRes = GetPieChartDataRes((r) => r + ..categoryData.replace([]) + ..locationData.replace([])); + final updateStart = DateTime(1999, 8, 6); + final updateEnd = DateTime(1999, 10, 6); + when(() => pieChartDataRepository.fetchPieChartData( + updateStart.toYMD(), updateEnd.toYMD())) + .thenAnswer((_) => Future.value(updateRes)); + // run + await container.read(pieChartControllerProvider.future); + await container + .read(pieChartControllerProvider.notifier) + .updateDateRange(DateTimeRange(start: updateStart, end: updateEnd)); + // check + final data = await container.read(pieChartControllerProvider.future); + expect(data.res, updateRes); + expect(data.dateRange, DateTimeRange(start: updateStart, end: updateEnd)); + }); +} diff --git a/mobile/test/src/mocks.dart b/mobile/test/src/mocks.dart index a8184e3..176f03c 100644 --- a/mobile/test/src/mocks.dart +++ b/mobile/test/src/mocks.dart @@ -1,6 +1,7 @@ import 'package:go_router/go_router.dart'; import 'package:mocktail/mocktail.dart'; import 'package:suito/src/features/charts/repositories/column_chart_data_repository.dart'; +import 'package:suito/src/features/charts/repositories/pie_chart_data_repository.dart'; import 'package:suito/src/features/schedules/repositories/expense/delete_expense_schedule_repository.dart'; import 'package:suito/src/features/schedules/repositories/expense/expense_schedule_detail_repository.dart'; import 'package:suito/src/features/schedules/repositories/expense/register_expense_schedule_repository.dart'; @@ -89,6 +90,9 @@ class MockSchedulesRepository extends Mock implements SchedulesRepository {} class MockColumnChartDataRepository extends Mock implements ColumnChartDataRepository {} +class MockPieChartDataRepository extends Mock + implements PieChartDataRepository {} + class Listener extends Mock { void call(T? previous, T next); }