platform/packages/client/test/list_test.dart
2024-10-12 18:45:27 -07:00

63 lines
1.7 KiB
Dart

import 'dart:async';
import 'package:async/async.dart';
import 'dart:io';
import 'package:protevus_client/io.dart' as c;
import 'package:protevus_framework/protevus_framework.dart' as s;
import 'package:protevus_framework/http.dart' as s;
import 'package:protevus_container/mirrors.dart';
import 'package:test/test.dart';
void main() {
late HttpServer server;
late c.Protevus app;
late c.ServiceList list;
late StreamQueue queue;
setUp(() async {
var serverApp = s.Protevus(reflector: MirrorsReflector());
var http = s.ProtevusHttp(serverApp);
serverApp.use('/api/todos', s.MapService(autoIdAndDateFields: false));
server = await http.startServer();
var uri = 'http://${server.address.address}:${server.port}';
app = c.Rest(uri);
list = c.ServiceList(app.service('api/todos'));
queue = StreamQueue(list.onChange);
});
tearDown(() async {
await server.close(force: true);
unawaited(list.close());
unawaited(list.service.close());
unawaited(app.close());
});
test('listens on create', () async {
unawaited(list.service.create({'foo': 'bar'}));
await list.onChange.first;
expect(list, [
{'foo': 'bar'}
]);
});
test('listens on modify', () async {
unawaited(list.service.create({'id': 1, 'foo': 'bar'}));
await queue.next;
await list.service.update(1, {'id': 1, 'bar': 'baz'});
await queue.next;
expect(list, [
{'id': 1, 'bar': 'baz'}
]);
});
test('listens on remove', () async {
unawaited(list.service.create({'id': '1', 'foo': 'bar'}));
await queue.next;
await list.service.remove('1');
await queue.next;
expect(list, isEmpty);
});
}