platform/packages/route/test/params_test.dart

46 lines
1.2 KiB
Dart
Raw Normal View History

2021-05-14 07:02:50 +00:00
import 'package:angel3_route/angel3_route.dart';
2016-11-27 23:39:03 +00:00
import 'package:test/test.dart';
2021-02-21 02:47:23 +00:00
void main() {
2022-04-25 00:54:13 +00:00
final router = Router()
..get('/hello', '')
..get('/user/:id', '');
2016-11-27 23:39:03 +00:00
router.group('/book/:id', (router) {
router.get('/reviews', '');
router.get('/readers/:readerId', '');
});
2019-11-28 17:40:32 +00:00
router.mount('/color', Router()..get('/:name/shades', ''));
2016-11-27 23:39:03 +00:00
setUp(router.dumpTree);
void expectParams(String path, Map<String, dynamic> params) {
final p = {};
final resolved = router.resolveAll(path, path);
print('Resolved $path => ${resolved.map((r) => r.allParams).toList()}');
2019-11-28 17:40:32 +00:00
for (final result in resolved) {
p.addAll(result.allParams);
}
2016-11-27 23:39:03 +00:00
expect(p, equals(params));
}
group('top-level', () {
test('no params', () => expectParams('/hello', {}));
test('one param', () => expectParams('/user/0', {'id': '0'}));
});
group('group', () {
2017-10-08 22:44:11 +00:00
//test('root', () => expectParams('/book/1337', {'id': '1337'}));
2016-11-27 23:39:03 +00:00
test('path', () => expectParams('/book/1337/reviews', {'id': '1337'}));
test(
'two params',
() => expectParams(
'/book/1337/readers/foo', {'id': '1337', 'readerId': 'foo'}));
});
test('mount',
() => expectParams('/color/chartreuse/shades', {'name': 'chartreuse'}));
}