Skip to content

Commit

Permalink
Merge pull request #361 from wakamenod/360-mobile-test-円グラフロジックテスト追加
Browse files Browse the repository at this point in the history
[Mobile] [Test] 円グラフロジックテスト追加 #360
  • Loading branch information
wakamenod authored Jul 24, 2023
2 parents 220d7a1 + 3d51b36 commit 147693c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ part 'pie_chart_service.g.dart';
class PieChartController extends _$PieChartController {
@override
Future<PieChart> 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);

Expand Down
145 changes: 145 additions & 0 deletions mobile/test/src/features/charts/services/pie_chart_service_test.dart
Original file line number Diff line number Diff line change
@@ -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));
});
}
4 changes: 4 additions & 0 deletions mobile/test/src/mocks.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -89,6 +90,9 @@ class MockSchedulesRepository extends Mock implements SchedulesRepository {}
class MockColumnChartDataRepository extends Mock
implements ColumnChartDataRepository {}

class MockPieChartDataRepository extends Mock
implements PieChartDataRepository {}

class Listener<T> extends Mock {
void call(T? previous, T next);
}

0 comments on commit 147693c

Please sign in to comment.