diff --git a/lib/src/models/summary_model.dart b/lib/src/models/summary_model.dart index 8f273c3..81f9621 100644 --- a/lib/src/models/summary_model.dart +++ b/lib/src/models/summary_model.dart @@ -14,17 +14,26 @@ class SummaryModel { int addedFriday; SummaryModel({ - this.completedMonday, - this.addedMonday, - this.completedTuesday, - this.addedTuesday, - this.completedWednesday, - this.addedWednesday, - this.completedThursday, - this.addedThursday, - this.completedFriday, - this.addedFriday, - }); + int completedMonday, + int addedMonday, + int completedTuesday, + int addedTuesday, + int completedWednesday, + int addedWednesday, + int completedThursday, + int addedThursday, + int completedFriday, + int addedFriday, + }) : completedMonday = completedMonday ?? 0, + addedMonday = addedMonday ?? 0, + completedTuesday = completedTuesday ?? 0, + addedTuesday = addedTuesday ?? 0, + completedWednesday = completedWednesday ?? 0, + addedWednesday = addedWednesday ?? 0, + completedThursday = completedThursday ?? 0, + addedThursday = addedThursday ?? 0, + completedFriday = completedFriday ?? 0, + addedFriday = addedFriday ?? 0; /// Returns a [SummaryModel] from a map. SummaryModel.fromMap(Map map) { diff --git a/lib/src/models/user_model.dart b/lib/src/models/user_model.dart index 0ce2dc2..f1e4e84 100644 --- a/lib/src/models/user_model.dart +++ b/lib/src/models/user_model.dart @@ -1,3 +1,4 @@ +import 'package:meta/meta.dart'; import 'summary_model.dart'; /// An app user. @@ -6,43 +7,49 @@ import 'summary_model.dart'; class UserModel { final String id; final String username; - final List tasks; + final List tasks; final SummaryModel summary; - final int userId; final int pendingHigh; final int pendingMedium; final int pendingLow; UserModel({ this.id, - this.username, - this.tasks, - this.summary, - this.userId, - this.pendingHigh, - this.pendingMedium, - this.pendingLow, + @required this.username, + @required this.tasks, + @required this.summary, + @required this.pendingHigh, + @required this.pendingMedium, + @required this.pendingLow, }); ///Returns a [UserModel] from a map. UserModel.fromFirestore(Map firestoreMap, {String id}) : id = id, username = firestoreMap["username"], - tasks = firestoreMap["tasks"].cast(), + tasks = firestoreMap["tasks"].cast(), summary = SummaryModel.fromMap(firestoreMap["summary"].cast()), - userId = firestoreMap["userId"], pendingHigh = firestoreMap["pendingHigh"], pendingMedium = firestoreMap["pendingMedium"], pendingLow = firestoreMap["pendingLow"]; + Map toFirestoreMap() { + return { + "username": username, + "tasks": tasks, + "summary": summary.toMap(), + "pendingHigh": pendingHigh, + "pendingMedium": pendingMedium, + "pendingLow": pendingLow, + }; + } + @override int get hashCode => id.hashCode ^ username.hashCode ^ - tasks.hashCode ^ summary.hashCode ^ - userId.hashCode ^ pendingHigh.hashCode ^ pendingMedium.hashCode ^ pendingLow.hashCode; @@ -54,9 +61,7 @@ class UserModel { other is UserModel && id == other.id && username == other.username && - tasks == other.tasks && summary == other.summary && - userId == other.userId && pendingHigh == other.pendingHigh && pendingMedium == other.pendingMedium && pendingLow == other.pendingLow; diff --git a/test/src/resources/firestore_provider_test.dart b/test/src/resources/firestore_provider_test.dart index ba5c36d..ba06986 100644 --- a/test/src/resources/firestore_provider_test.dart +++ b/test/src/resources/firestore_provider_test.dart @@ -2,11 +2,12 @@ import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:do_more/src/models/event_model.dart'; +import 'package:do_more/src/models/summary_model.dart'; import 'package:do_more/src/models/task_model.dart'; +import 'package:do_more/src/models/user_model.dart'; import 'package:do_more/src/resources/firestore_provider.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; -import 'package:rxdart/rxdart.dart'; class MockFirestore extends Mock implements Firestore {} @@ -48,7 +49,38 @@ main() { lowPriority: 0, ); + final summary = SummaryModel(); + + final user = UserModel( + id: '123', + tasks: [], + pendingHigh: 0, + pendingMedium: 0, + pendingLow: 0, + username: 'testUser', + summary: summary, + ); + group('FirestoreProvider', () { + test('should listen to updates of a single user object', () { + final firestore = MockFirestore(); + final collection = MockCollectionReference(); + final query = MockQuery(); + final querySnapshot = MockQuerySnapshot(); + final snapshot = MockDocumentSnapshot(user.toFirestoreMap()); + final snapshots = Stream.fromIterable([querySnapshot]); + final provider = FirestoreProvider(firestore); + + when(firestore.collection('users')).thenReturn(collection); + when(collection.where('username', isEqualTo: user.username)) + .thenReturn(query); + when(query.snapshots()).thenAnswer((_) => snapshots); + when(querySnapshot.documents).thenReturn([snapshot]); + when(snapshot.documentID).thenReturn(user.id); + + expect(provider.getUser(user.username), emits(user)); + }); + test('should create task in firestore', () { final firestore = MockFirestore(); final collection = MockCollectionReference();