From c29b87469ba7a46ab1ce705979ac97ebdcfca2cc Mon Sep 17 00:00:00 2001 From: Hugo Walbecq Date: Thu, 9 May 2024 18:38:21 +0200 Subject: [PATCH] feat: add getBottomRight method in board info repo (#458) --- .../lib/src/board_info_repository.dart | 22 ++++++++++++++++++ .../test/src/board_info_repository_test.dart | 23 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/packages/board_info_repository/lib/src/board_info_repository.dart b/packages/board_info_repository/lib/src/board_info_repository.dart index cb9570a69..6bd102f89 100644 --- a/packages/board_info_repository/lib/src/board_info_repository.dart +++ b/packages/board_info_repository/lib/src/board_info_repository.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:rxdart/rxdart.dart'; @@ -112,6 +114,26 @@ class BoardInfoRepository { } } + /// returns bottom right section's position + Future> 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 isHintsEnabled() { if (_hintsEnabled != null) return _hintsEnabled!.stream; diff --git a/packages/board_info_repository/test/src/board_info_repository_test.dart b/packages/board_info_repository/test/src/board_info_repository_test.dart index cbcc31821..92ea7442e 100644 --- a/packages/board_info_repository/test/src/board_info_repository_test.dart +++ b/packages/board_info_repository/test/src/board_info_repository_test.dart @@ -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'; @@ -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)); @@ -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()), + ); + }); + }); + group('isHintsEnabled', () { test('returns hints enabled status from firebase', () { final doc = _MockQueryDocumentSnapshot>();