platform/packages/poll/test/all_test.dart
Tobe O aa092a4111 Add 'packages/poll/' from commit '14637ac137e083a90e1db397f0a90561888c3bde'
git-subtree-dir: packages/poll
git-subtree-mainline: a5f5661650
git-subtree-split: 14637ac137
2020-02-15 18:29:16 -05:00

105 lines
2.5 KiB
Dart

import 'package:angel_framework/angel_framework.dart' as srv;
import 'package:angel_poll/angel_poll.dart';
import 'package:angel_test/angel_test.dart';
import 'package:async/async.dart';
import 'package:logging/logging.dart';
import 'package:test/test.dart';
void main() {
srv.Service store;
TestClient client;
PollingService pollingService;
setUp(() async {
var app = new srv.Angel();
app.logger = new Logger.detached('angel_poll')
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) {
print(rec.error);
print(rec.stackTrace);
}
});
store = app.use(
'/api/todos',
new srv.MapService(
autoIdAndDateFields: false,
),
);
client = await connectTo(app);
pollingService = new PollingService(
client.service('api/todos'),
const Duration(milliseconds: 100),
);
});
tearDown(() => client.close());
group('events', () {
var created;
StreamQueue onCreated, onModified, onRemoved;
setUp(() async {
onCreated = new StreamQueue(pollingService.onCreated);
onModified = new StreamQueue(pollingService.onModified);
onRemoved = new StreamQueue(pollingService.onRemoved);
created = await store.create({
'id': '0',
'text': 'Clean your room',
'completed': false,
});
});
tearDown(() {
onCreated.cancel();
onModified.cancel();
onRemoved.cancel();
});
test('fires indexed', () async {
var indexed = await pollingService.index();
print(indexed);
expect(await pollingService.onIndexed.first, indexed);
});
test('fires created', () async {
var result = await onCreated.next;
print(result);
expect(created, result);
});
test('fires modified', () async {
await pollingService.index();
await store.modify('0', {
'text': 'go to school',
});
var result = await onModified.next;
print(result);
expect(result, new Map.from(created)..['text'] = 'go to school');
});
test('manual modify', () async {
await pollingService.index();
await pollingService.modify('0', {
'text': 'eat',
});
var result = await onModified.next;
print(result);
expect(result, new Map.from(created)..['text'] = 'eat');
});
test('fires removed', () async {
await pollingService.index();
var removed = await store.remove('0');
var result = await onRemoved.next;
print(result);
expect(result, removed);
});
});
}