platform/packages/poll/test/all_test.dart

106 lines
2.6 KiB
Dart
Raw Normal View History

2017-12-21 19:24:49 +00:00
import 'package:angel_framework/angel_framework.dart' as srv;
2021-06-20 12:37:20 +00:00
import 'package:angel_container/mirrors.dart';
2017-12-21 19:24:49 +00:00
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() {
2021-06-20 12:37:20 +00:00
late srv.Service store;
late TestClient client;
late PollingService pollingService;
var created;
late StreamQueue onCreated;
late StreamQueue onModified;
late StreamQueue onRemoved;
2017-12-21 19:24:49 +00:00
setUp(() async {
2021-06-20 12:37:20 +00:00
var app = srv.Angel(reflector: MirrorsReflector());
app.logger = Logger.detached('angel_poll')
2017-12-21 19:24:49 +00:00
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) {
print(rec.error);
print(rec.stackTrace);
}
});
store = app.use(
'/api/todos',
2021-06-20 12:37:20 +00:00
srv.MapService(
2017-12-21 19:24:49 +00:00
autoIdAndDateFields: false,
),
);
client = await connectTo(app);
2021-06-20 12:37:20 +00:00
pollingService = PollingService(
2017-12-21 19:24:49 +00:00
client.service('api/todos'),
const Duration(milliseconds: 100),
);
2021-06-20 12:37:20 +00:00
onCreated = StreamQueue(pollingService.onCreated);
onModified = StreamQueue(pollingService.onModified);
onRemoved = StreamQueue(pollingService.onRemoved);
2017-12-21 19:24:49 +00:00
2021-06-20 12:37:20 +00:00
created = await store.create({
'id': '0',
'text': 'Clean your room',
'completed': false,
2017-12-21 19:24:49 +00:00
});
2021-06-20 12:37:20 +00:00
});
2017-12-21 19:24:49 +00:00
2021-06-20 12:37:20 +00:00
tearDown(() {
onCreated.cancel();
onModified.cancel();
onRemoved.cancel();
client.close();
});
2017-12-21 19:24:49 +00:00
2021-06-20 12:37:20 +00:00
group('events', () {
2017-12-21 19:24:49 +00:00
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);
2021-06-20 12:37:20 +00:00
expect(result, Map.from({'': created})..['text'] = 'go to school');
2017-12-21 19:24:49 +00:00
});
test('manual modify', () async {
await pollingService.index();
await pollingService.modify('0', {
'text': 'eat',
});
var result = await onModified.next;
print(result);
2021-06-20 12:37:20 +00:00
expect(result, Map.from({'': created})..['text'] = 'eat');
2017-12-21 19:24:49 +00:00
});
test('fires removed', () async {
await pollingService.index();
var removed = await store.remove('0');
var result = await onRemoved.next;
print(result);
expect(result, removed);
});
});
}