-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9a2533
commit e43474c
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
packages/firebase_database/firebase_database/test/firebase_sorted_list_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:firebase_database/firebase_database.dart'; | ||
import 'package:firebase_database/ui/firebase_sorted_list.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import 'firebase_list_test.dart'; | ||
|
||
void main() { | ||
|
||
group('FirebaseSortedList', () { | ||
late StreamController<DatabaseEvent> onChildAddedStreamController; | ||
late StreamController<DatabaseEvent> onChildRemovedStreamController; | ||
late StreamController<DatabaseEvent> onChildChangedStreamController; | ||
late StreamController<DatabaseEvent> onChildMovedStreamController; | ||
late StreamController<DatabaseEvent> onValue; | ||
late MockQuery query; | ||
late FirebaseSortedList list; | ||
late Completer<ListChange> callbackCompleter; | ||
|
||
setUp(() { | ||
onChildAddedStreamController = StreamController<DatabaseEvent>(); | ||
onChildRemovedStreamController = StreamController<DatabaseEvent>(); | ||
onChildChangedStreamController = StreamController<DatabaseEvent>(); | ||
onChildMovedStreamController = StreamController<DatabaseEvent>(); | ||
onValue = StreamController<DatabaseEvent>(); | ||
query = MockQuery( | ||
onChildAddedStreamController.stream, | ||
onChildRemovedStreamController.stream, | ||
onChildChangedStreamController.stream, | ||
onChildMovedStreamController.stream, | ||
onValue.stream, | ||
); | ||
callbackCompleter = Completer<ListChange>(); | ||
|
||
void completeWithChange(int index, DataSnapshot snapshot) { | ||
callbackCompleter.complete(ListChange.at(index, snapshot)); | ||
} | ||
|
||
list = FirebaseSortedList( | ||
query: query, | ||
onChildAdded: completeWithChange, | ||
onChildRemoved: completeWithChange, | ||
onChildChanged: completeWithChange, | ||
comparator: (DataSnapshot a, DataSnapshot b) { | ||
return b.key.toString().compareTo(a.key.toString()); | ||
}, | ||
); | ||
}); | ||
|
||
tearDown(() { | ||
onChildAddedStreamController.close(); | ||
onChildRemovedStreamController.close(); | ||
onChildChangedStreamController.close(); | ||
onChildMovedStreamController.close(); | ||
onValue.close(); | ||
}); | ||
|
||
Future<ListChange> resetCompleterOnCallback() async { | ||
final ListChange result = await callbackCompleter.future; | ||
callbackCompleter = Completer<ListChange>(); | ||
return result; | ||
} | ||
|
||
Future<ListChange> processChildAddedEvent(DatabaseEvent event) { | ||
onChildAddedStreamController.add(event); | ||
|
||
|
||
|
||
final future = resetCompleterOnCallback(); | ||
|
||
expect(list.indexOf(event.snapshot), isNot(equals(-1))); | ||
|
||
|
||
return future; | ||
|
||
} | ||
|
||
test('can push to sorted list', () async { | ||
final DataSnapshot snapshot = MockDataSnapshot('key10', 10); | ||
expect( | ||
await processChildAddedEvent( | ||
MockEvent( | ||
DatabaseEventType.childAdded, | ||
null, | ||
snapshot, | ||
), | ||
), | ||
ListChange.at(0, snapshot), | ||
); | ||
expect(list, <DataSnapshot>[snapshot]); | ||
}); | ||
|
||
}); | ||
|
||
} | ||
|
||
|