@@ -20,84 +20,84 @@ void main() {
mockDocumentReference = MockDocumentReference ();
mockDocumentSnapshot = MockDocumentSnapshot ();
});
//Get data from firestore doc
test ('should return data when the call to remote source is successful.' , () async {
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.get ()).thenAnswer ((_) async => mockDocumentSnapshot);
when (mockDocumentSnapshot.data ()).thenReturn (responseMap);
//act
final result = await remoteDataSource.getData ('user_id' );
//assert
expect (result, userModel); //userModel is a object that is defined. Replace with you own model class object.
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.get ()).thenAnswer ((_) async => mockDocumentSnapshot);
when (mockDocumentSnapshot.data ()).thenReturn (responseMap);
//act
final result = await remoteDataSource.getData ('user_id' );
//assert
expect (result, userModel); //userModel is a object that is defined. Replace with you own model class object.
});
//Add data to firestore doc
//This demonstrates chained call like (instance.collection('path1').doc('doc1').collection('path2').doc('doc2').add(data));
//We return the same mocked objects for each call.
test ('should get data from nested document.' , () async {
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.add (any)).thenAnswer ((_) async => mockDocumentReference);
when (mockDocumentReference.id).thenReturn (messageID);
when (mockDocumentSnapshot.data ()).thenReturn (messageData);
//act
final result = await remoteDataSource.addData (userID, notificationID);
//assert
expect (result, notificationID);
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.add (any)).thenAnswer ((_) async => mockDocumentReference);
when (mockDocumentReference.id).thenReturn (messageID);
when (mockDocumentSnapshot.data ()).thenReturn (messageData);
//act
final result = await remoteDataSource.addData (userID, notificationID);
//assert
expect (result, notificationID);
});
//Get list of documents from firestore.
test ('should get list of all the documents.' , () async {
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.get ()).thenAnswer ((_) async => mockDocumentSnapshot);
when (mockDocumentSnapshot.data ()).thenReturn (roomsMap);
when (mockDocumentReference.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.get ()).thenAnswer ((_) async => mockQuerySnapshot);
when (mockQuerySnapshot.docs).thenReturn ([mockQueryDocumentSnapshot]);
when (mockQueryDocumentSnapshot.data ()).thenReturn (membersMap);
//act
final result = await remoteDataSource.fetchUsers ();
//assert
expect (result, users);
});
//Get list of documents from firestore.
test ('should get list of all the documents.' , () async {
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.get ()).thenAnswer ((_) async => mockDocumentSnapshot);
when (mockDocumentSnapshot.data ()).thenReturn (roomsMap);
when (mockDocumentReference.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.get ()).thenAnswer ((_) async => mockQuerySnapshot);
when (mockQuerySnapshot.docs).thenReturn ([mockQueryDocumentSnapshot]);
when (mockQueryDocumentSnapshot.data ()).thenReturn (membersMap);
//act
final result = await remoteDataSource.fetchUsers ();
//assert
expect (result, users);
//Delete doc in firestore.
test ('should delete a doc and return true.' , () async {
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.delete ()).thenAnswer ((_) async => Future .value ());
//act
final result = await remoteDataSource.joinSquad ();
//assert
expect (result, true );
});
//Mock query methods like orderBy(), limit().
test ('should return previous messages when getting data is successful.' , () async {
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.collection ('messages' )).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (lastMessageID)).thenReturn (mockDocumentReference);
when (mockDocumentReference.get ()).thenAnswer ((realInvocation) async => mockDocumentSnapshot);
when (mockCollectionReference.orderBy ('timestamp' , descending: true )).thenReturn (mockQuery);
when (mockQuery.startAfterDocument (mockDocumentSnapshot)).thenReturn (mockQuery);
when (mockQuery.limit (20 )).thenReturn (mockQuery);
when (mockQuery.get ()).thenAnswer ((_) async => mockQuerySnapshot);
when (mockQuerySnapshot.docs).thenReturn ([mockQueryDocumentSnapshot]);
when (mockQueryDocumentSnapshot.data ()).thenReturn (message);
//act
final result = await remoteDataSource.loadPreviousMessages (roomID: roomID, lastFetchedMessageID: lastMessageID);
//assert
expect (result, messages);
});
//Delete doc in firestore.
test ('should delete a doc and return true.' , () async {
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.delete ()).thenAnswer ((_) async => Future .value ());
//act
final result = await remoteDataSource.joinSquad ();
//assert
expect (result, true );
});
//Mock query methods like orderBy(), limit().
test ('should return previous messages when getting data is successful.' , () async {
// arrange
when (instance.collection (any)).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (any)).thenReturn (mockDocumentReference);
when (mockDocumentReference.collection ('messages' )).thenReturn (mockCollectionReference);
when (mockCollectionReference.doc (lastMessageID)).thenReturn (mockDocumentReference);
when (mockDocumentReference.get ()).thenAnswer ((realInvocation) async => mockDocumentSnapshot);
when (mockCollectionReference.orderBy ('timestamp' , descending: true )).thenReturn (mockQuery);
when (mockQuery.startAfterDocument (mockDocumentSnapshot)).thenReturn (mockQuery);
when (mockQuery.limit (20 )).thenReturn (mockQuery);
when (mockQuery.get ()).thenAnswer ((_) async => mockQuerySnapshot);
when (mockQuerySnapshot.docs).thenReturn ([mockQueryDocumentSnapshot]);
when (mockQueryDocumentSnapshot.data ()).thenReturn (message);
//act
final result = await remoteDataSource.loadPreviousMessages (roomID: roomID, lastFetchedMessageID: lastMessageID);
//assert
expect (result, messages);
});
}
}