Added first test for firestore provider
This commit is contained in:
parent
ee7b5748c0
commit
0ff2da5f54
2 changed files with 78 additions and 30 deletions
78
test/src/resources/firestore_provider_test.dart
Normal file
78
test/src/resources/firestore_provider_test.dart
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
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/task_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 {}
|
||||
|
||||
class MockCollectionReference extends Mock implements CollectionReference {}
|
||||
|
||||
class MockDocumentReference extends Mock implements DocumentReference {}
|
||||
|
||||
class MockDocumentSnapshot extends Mock implements DocumentSnapshot {
|
||||
final Map<String, dynamic> data;
|
||||
|
||||
MockDocumentSnapshot([this.data]);
|
||||
|
||||
dynamic operator [](String key) => data[key];
|
||||
}
|
||||
|
||||
class MockQuerySnapshot extends Mock implements QuerySnapshot {}
|
||||
|
||||
main() {
|
||||
final task = TaskModel(
|
||||
id: '1',
|
||||
text: 'A task',
|
||||
done: false,
|
||||
ownerUsername: 'testUser',
|
||||
event: 'testEvent',
|
||||
priority: 1,
|
||||
);
|
||||
|
||||
final event = EventModel(
|
||||
id: '1',
|
||||
name: 'An event',
|
||||
pendigTasks: 0,
|
||||
media: <String>[],
|
||||
tasks: <String>[],
|
||||
when: <bool>[],
|
||||
highPriority: 0,
|
||||
mediumPriority: 0,
|
||||
lowPriority: 0,
|
||||
);
|
||||
|
||||
group('FirestoreProvider', () {
|
||||
test('should send task to firestore', () {
|
||||
final firestore = MockFirestore();
|
||||
final collection = MockCollectionReference();
|
||||
final provider = FirestoreProvider(firestore);
|
||||
|
||||
when(firestore.collection('tasks')).thenReturn(collection);
|
||||
|
||||
provider.addTask(task);
|
||||
|
||||
verify(collection.add(task.toFirestoreMap()));
|
||||
});
|
||||
|
||||
test('should listen for updates to a single Task', () {
|
||||
final firestore = MockFirestore();
|
||||
final collection = MockCollectionReference();
|
||||
final snapshot = MockDocumentSnapshot(task.toFirestoreMap());
|
||||
final snapshots = Stream.fromIterable([snapshot]);
|
||||
final document = MockDocumentReference();
|
||||
final provider = FirestoreProvider(firestore);
|
||||
|
||||
when(firestore.collection('tasks')).thenReturn(collection);
|
||||
when(collection.document(task.id)).thenReturn(document);
|
||||
when(document.snapshots()).thenAnswer((_) => snapshots);
|
||||
when(snapshot.documentID).thenReturn(task.id);
|
||||
|
||||
expectLater(provider.getTask('1'), emits(task));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
// This is a basic Flutter widget test.
|
||||
//
|
||||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||
// utility that Flutter provides. For example, you can send tap and scroll
|
||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||
// tree, read text, and verify that the values of widget properties are correct.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:do_more/main.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(MyApp());
|
||||
|
||||
// Verify that our counter starts at 0.
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
expect(find.text('1'), findsNothing);
|
||||
|
||||
// Tap the '+' icon and trigger a frame.
|
||||
await tester.tap(find.byIcon(Icons.add));
|
||||
await tester.pump();
|
||||
|
||||
// Verify that our counter has incremented.
|
||||
expect(find.text('0'), findsNothing);
|
||||
expect(find.text('1'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue