65 lines
2.5 KiB
Dart
65 lines
2.5 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:mobile_app/data/repositories/match_repository.dart';
|
|
import 'package:mobile_app/ui/features/dashboard/view_models/match_results_view_model.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
test('loads match results and their events from bundled JSON', () async {
|
|
final matches = await MatchRepository().loadSourceMatches();
|
|
|
|
expect(matches, hasLength(21));
|
|
expect(matches.first.homeScore, 1);
|
|
expect(matches.first.goals, hasLength(2));
|
|
expect(matches.first.goals.first.seconds, 88);
|
|
expect(matches.first.goals.first.team.name, 'away');
|
|
expect(matches.first.cards.first.cardType.name, 'red');
|
|
});
|
|
|
|
test('simulates periodic receipt and persists received matches', () async {
|
|
final directory = await Directory.systemTemp.createTemp('match-results-');
|
|
addTearDown(() => directory.delete(recursive: true));
|
|
final repository = MatchRepository(
|
|
documentsDirectoryProvider: () async => directory,
|
|
);
|
|
final viewModel = MatchResultsViewModel(
|
|
repository: repository,
|
|
updateInterval: const Duration(milliseconds: 10),
|
|
);
|
|
addTearDown(viewModel.dispose);
|
|
|
|
await viewModel.loadMatches();
|
|
expect(viewModel.matches, hasLength(1));
|
|
|
|
await Future<void>.delayed(const Duration(milliseconds: 30));
|
|
|
|
expect(viewModel.matches.length, greaterThan(1));
|
|
final storedData = await repository.loadStoredData();
|
|
expect(storedData.matches, hasLength(viewModel.matches.length));
|
|
expect(storedData.sourceCursor, viewModel.matches.length);
|
|
});
|
|
|
|
test('resumes simulation from the persisted source cursor', () async {
|
|
final directory = await Directory.systemTemp.createTemp('match-results-');
|
|
addTearDown(() => directory.delete(recursive: true));
|
|
final repository = MatchRepository(
|
|
documentsDirectoryProvider: () async => directory,
|
|
);
|
|
final sourceMatches = await repository.loadSourceMatches();
|
|
await repository.saveStoredData([sourceMatches.first], 1);
|
|
final viewModel = MatchResultsViewModel(
|
|
repository: repository,
|
|
updateInterval: const Duration(hours: 1),
|
|
);
|
|
addTearDown(viewModel.dispose);
|
|
|
|
await viewModel.loadMatches();
|
|
|
|
expect(viewModel.matches, hasLength(2));
|
|
expect(viewModel.matches.first.datetime, sourceMatches.first.datetime);
|
|
expect(viewModel.matches.last.datetime, sourceMatches[1].datetime);
|
|
expect((await repository.loadStoredData()).sourceCursor, 2);
|
|
});
|
|
} |