Skip to content

Commit

Permalink
feat: add getBottomRight method in board info repo (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
B0berman authored May 9, 2024
1 parent 0ad75fc commit c29b874
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/board_info_repository/lib/src/board_info_repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:rxdart/rxdart.dart';

Expand Down Expand Up @@ -112,6 +114,26 @@ class BoardInfoRepository {
}
}

/// returns bottom right section's position
Future<Point<int>> getBottomRight() async {
try {
final results = await boardInfoCollection
.where('type', isEqualTo: 'bottom_right')
.get();

final data = results.docs.first.data();
final string = data['value'] as String;
final position = string.split(',');
final (x, y) = (
int.tryParse(position.first),
int.tryParse(position.first),
);
return Point(x!, y!);
} catch (error, stackStrace) {
throw BoardInfoException(error, stackStrace);
}
}

/// Returns the hints enabled status.
Stream<bool> isHintsEnabled() {
if (_hintsEnabled != null) return _hintsEnabled!.stream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: prefer_const_constructors, subtype_of_sealed_class

import 'dart:math';

import 'package:board_info_repository/board_info_repository.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -45,6 +47,9 @@ void main() {
when(
() => collection.where('type', isEqualTo: 'section_size'),
).thenReturn(collection);
when(
() => collection.where('type', isEqualTo: 'bottom_right'),
).thenReturn(collection);

when(collection.get).thenAnswer((_) async => query);
when(collection.snapshots).thenAnswer((_) => Stream.value(query));
Expand Down Expand Up @@ -131,6 +136,24 @@ void main() {
});
});

group('getBottomRight', () {
test("returns bottom right section's position from firebase", () async {
mockQueryResult('16,16');
final result = await boardInfoRepository.getBottomRight();
expect(result, equals(Point(16, 16)));
});

test('throws BoardInfoException when fetching the info fails', () {
when(
() => collection.where('type', isEqualTo: 'bottom_right'),
).thenThrow(Exception('oops'));
expect(
() => boardInfoRepository.getBottomRight(),
throwsA(isA<BoardInfoException>()),
);
});
});

group('isHintsEnabled', () {
test('returns hints enabled status from firebase', () {
final doc = _MockQueryDocumentSnapshot<Map<String, dynamic>>();
Expand Down

0 comments on commit c29b874

Please sign in to comment.